Log4j
References
- http://www.developer.com/open/article.php/10930_3097221_1
- http://logging.apache.org/log4j/docs/manual.html
- Sun's alternative logging infrastructure for jdk 1.4: http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/
- Email error messages with SMTPAppender: http://www.onjava.com/pub/a/onjava/2004/09/29/smtp-logging.html
- http://java2.5341.com/msg/32707.html
- http://logging.apache.org/log4j/docs/api/
- PatternLayout specs: http://logging.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html
Theory
- Logger - the object that takes messages for logging
- Appender - the object that outputs messages to some medium
- Layout - controls the format of the output message
Logging Levels
DEBUG < INFO < WARN < ERROR < FATAL
If a given logger has level WARN, the an INFO logging request will not be processed.
Hierarchy and Inheritance
There is always a root logger. Each logger is associated with some node in the package hierarchy, and its log level is either explicitly assigned or inherited from higher up in the hierarchy. Example: During coding, my root log level is DEBUG. I'm done working on the dc.util package and want to focus on classes in dc and dc.db. I might stifle excessive log messages from the dc.util classes by creating a dc.util logger (in the properties file) and assigning it a level of WARNING or ERROR.
Download
- Make sure log4j.jar is on your classpath. Get it from here:
http://logging.apache.org/log4j/docs/download.html
Properties File
Create a log4j.properties file:
Example 1:
# set root logger level to ERROR and give it an appender log4j.rootLogger = ERROR, RootAppender log4j.appender.RootAppender = org.apache.log4j.ConsoleAppender log4j.appender.RootAppender.layout = org.apache.log4j.PatternLayout log4j.appender.RootAppender.layout.ConversionPattern = %d{HH:mm:ss} %-5p [%c{1}] %m%n # set the top level org logger to WARN log4j.logger.org = WARN # set the top level Mirc logger to INFO log4j.logger.org.rsna = INFO
Example 2:
# set root logger level to ERROR and give it an appender log4j.rootLogger = ERROR, RootAppender log4j.appender.RootAppender = org.apache.log4j.ConsoleAppender log4j.appender.RootAppender.layout = org.apache.log4j.PatternLayout log4j.appender.RootAppender.layout.ConversionPattern = %d{HH:mm:ss} %-5p [%c{1}] %m%n # set the top level org logger to WARN log4j.logger.org = WARN # set the top level Mirc logger to INFO log4j.logger.org.rsna = INFO
Other kinds of Appenders
- FileAppender
- DailyRollingFileAppender
- RollingFileAppender
- SMTPAppender seems to depend on javax.Mail, which seems to be part of J2EE.
Load in main()
In the main() method, load the log4j properties like this:
initializeLog4J("config/log4j.properties");
...
/**
* Function to configure Log4J from the properties file.
*/
private static void initializeLog4J(String filename) {
File file = new File(filename);
if (file.exists()) {
PropertyConfigurator.configure(filename);
}
}
Logging
In each class that will include logging statements:
//Import necessary log4j API classes
import org.apache.log4j.*;
...
private static final Logger logger = Logger.getLogger(DicomObjectProcessor.class);
Use log statements wherever it makes sense:
logger.debug();
logger.info();
logger.warn();
logger.error();
logger.fatal();