This is an old revision of the document!
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");
log4j.properties
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
… because it is easier than // System.out.println(...)
all over the code
Since JDK 1.4 there is a 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
Commons Logging seems to be a compatibility layer so that a library can work with different logging implementations.
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
and might not be able to run the program.
Here is a solution that works well for me:
log4j.properties
file is located in the root directory of the classpath./src
directorylog4j.properties
file is located in the working directorylog4j.properties
exists in the working directorylog4j.properties
from the working directory AND-Dlog4j.configuration=file:/home/alex/log4j.properties
)alex @ ieg: home about me publications research