Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

7.13. Configuring Log4J with a Properties File

7.13.1. Problem

You need to use Log4J, and you would like to configure it with a properties file.

7.13.2. Solution

Use the BasicConfigurator to read a log4j.properties file resource from the classpath. The following code configures Log4J from a resource named log4j.properties, and logs two messages:

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
URL log4Jresource = this.getClass( ).getResource("log4j.properties");
PropertyConfigurator.configure( log4Jresource );
Logger log = Logger.getLogger( "com.discursive.SomeApp" );
log.info( "This is a log message" );
log.error( "This is an error message" );

The log4j.properties file contains a basic Log4J configuration that sets the root category logging level to WARN and the application's logging level to DEBUG:

# All logging output sent to standard out and a file
# WARN is default logging level
log4j.rootCategory=WARN, STDOUT, FILE
# Application logging level is DEBUG
log4j.logger.com.discursive=DEBUG
# Configure the Standard Out Appender
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p (%F:%L) %m%n
# Configure a rolling file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=output.log
log4j.appender.FILE.MaxFileSize=2000KB
log4j.appender.FILE.MaxBackupIndex=5
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c - %m%n

This example prints a single info message to the console with the following format:

 INFO (Sample.java:24) This is a log message
ERROR (Sample.java:25) This is an error message

The rootCategory is configured to send all log messages to the console and a RollingFileAppender. A file named output.log contains the following content after this code has been executed:

2004-06-14 00:12:22,324  INFO Sample - This is a log message
2004-06-14 00:12:22,326 ERROR Sample - This is an error message

7.13.3. Discussion

PropertyConfigurator.configure() takes a URL referencing a resource to be loaded from the classpath. This properties file is read and Log4J is configured to send all messages to both the console and a file. Content is written to a file using a RollingFileAppender, which writes to a file until it reaches a configurable maximum size (2 MB). Once this size has been reached, a RollingFileAppender will move the existing output.log file to a file named output.log.1 and create a new output.log file. As configured in the previous example, the RollingFileAppender will keep five backup log files, moving output.log.1 to output.log.2 and output.log to output.log.1 the next time a log file's maximum size has been reached.

The Solution configures the default logging level to be WARN, meaning that all log messages lower on the level hierarchy will not be sent to appenders. Log4J has five default levels, and they are listed in order of importance: DEBUG, INFO, WARN, ERROR, and FATAL. If a category is configured with a logging level of ERROR, only ERROR and FATAL messages are sent to appenders, and if a category is configured with a logging level of DEBUG, all logging messages are sent to appenders. If you are only interested in the debugging output from your own program, set the rootCategory to a high logging level, and override that level for your application's classes. log4j.logger.com.discursive=DEBUG overrides the rootCategory's logging level for every topic at or below the com.discursive logging category.

7.13.4. See Also

The properties file shown in the Solution should be used as a starting point for Log4J configuration. For more information about various implementations of Appender or syntax for ConversionPattern, see the Log4J API documentation at http://logging.apache.org/log4j/docs/api/index.html.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.