User Tools

Site Tools


java:log4j

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
java:log4j [2009/10/20 12:23]
Alexander Rind created
java:log4j [2012/01/04 10:30] (current)
Alexander Rind link newer logging packes
Line 1: Line 1:
 ====== Logging with log4j ====== ====== Logging with log4j ======
 +
 +
 +===== Code Snippets ​ =====
 +
 +
 +<code java>
 + private static Logger logger = Logger.getLogger(MyClass.class);​
 +...
 + logger.fatal("​text"​);​
 + logger.error("​text"​);​
 + logger.warn("​text"​);​
 + logger.info("​text"​);​
 + logger.debug("​text"​);​
 + logger.info("​text"​);​
 +...
 + if (logger.isTraceEnabled())
 + logger.trace("​lo:​ " + lo + " hi: " + hi + " min: " + min + " max: " + max + " rel: " + rel);
 +...
 + if ((new File("​log4j.properties"​)).canRead())
 + PropertyConfigurator.configure("​log4j.properties"​);​
 +</​code>​
 +
 +**log4j.properties**
 +<​file>​
 +log4j.rootLogger=TRACE,​ A1
 +log4j.appender.A1=org.apache.log4j.ConsoleAppender
 +log4j.appender.A1.layout=org.apache.log4j.PatternLayout
 +
 +# Print the date in ISO 8601 format
 +log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
 +
 +log4j.logger.gui.MainFrame=INFO
 +</​file>​
 +
 +<code java>
 +        try {
 +            if ((new File("​log4j.properties"​)).canRead())
 +                PropertyConfigurator.configure("​log4j.properties"​);​
 +        }
 +        catch (SecurityException e) {
 +        }
 +</​code>​
  
 ===== Rationale ===== ===== Rationale =====
  
-==== log4j vsJDK logging ====+... because it is easier than ''​%%//​ System.out.println(...)%%''​ all over the code 
  
-==== log4j vs. Apache ​Commons ====+ 
 +=== log4j vs. JDK logging === 
 + 
 +Since JDK 1.4 there is a [[http://​java.sun.com/​javase/​6/​docs/​api/​java/​util/​logging/​Logger.html | logging framework inside the Java core packages]]. 
 + 
 +AFAIK log4j provides more features and is at least as stable, so I keep using log4j. 
 + 
 +Read more opinions [[http://​www.google.at/​search?​q=log4j+java.util.logging]] 
 + 
 +Integrate JUL into log4j: 
 +  * http://​shrubbery.mynetgear.net/​c/​display/​W/​Routing%2Bjava.util.logging%2Bmessages%2Bto%2BLog4J 
 +  * http://​people.apache.org/​~psmith/​logging.apache.org/​sandbox/​jul-log4j-bridge/​examples.html 
 +  * use SLF4J 
 + 
 +=== log4j vs. Commons ​Logging ​=== 
 + 
 +[[http://​commons.apache.org/​logging/​ | Commons Logging]] seems to be a compatibility layer so that a library can work with different logging implementations. 
 + 
 +=== newer logging packages === 
 + 
 +  * [[http://​www.slf4j.org/​ | Simple Logging Facade for Java (SLF4J)]] 
 +  * [[http://​www.slf4j.org/​nlog4j/​ | NLOG4J]] --- discontinued 
 +  * [[http://​logback.qos.ch/​ | Logback]] --- planned as successor of log4j and nlog4j
  
  
 ===== Good Practice ===== ===== Good Practice =====
  
 +Whatever you do, don't **break** your program:
 +If the program reads a ''​log4.properties''​ file from the file system, ​
 +the file will likely get lost and users will be confronted with a ''​FileNotFoundException'' ​
 +or might not be able to run the program.
  
-===== Ressources =====+Here is a solution that works well for me:
  
 +  - a default ''​log4j.properties''​ file is located in the root directory of the **classpath**
 +    * if you work with Eclipse, this will be your ''​./​src''​ directory
 +    * if you create a jar-file, this will be the root directory of the jar-file
 +    * for the default settings use minimal logging (e.g., ConsoleAppender,​ WARN level)
 +  - a customized ''​log4j.properties''​ file can be located in the working directory
 +    * if you work with Eclipse, this will be the project directory
 +  - in the main method **check, whether ''​log4j.properties''​ exists** in the working directory
 +    * if it exist and is readable, then load this file
 +    * handle SecurityException (e.g., for Java Web Start) ​
 +    * if it does not exist, log4j will load the file from the classpath by default
 +  - if users want to override this behavior, they can 
 +    * delete the ''​log4j.properties''​ from the working directory AND
 +    * specify the url of the configuration as a Java system property (e.g., ''​-Dlog4j.configuration=file:/​home/​alex/​log4j.properties''​)
  
  
 +===== Ressources =====
 +
 +  * [[http://​logging.apache.org/​log4j/​1.2/​manual.html | Short introduction to log4j]] by Ceki Gülcü, March 2002. 
 +  * [[http://​logging.apache.org/​log4j/​1.2/​apidocs/​index.html | API documentation]]
java/log4j.1256034188.txt · Last modified: 2009/10/20 12:23 by Alexander Rind

alex @ ieg: home about me publications research