How do I configure Log4J with properties?
Author: Deron Eriksson
Description: This Java tutorial describes how to configure Log4J via a properties file.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page: < 1 2 3 4 >

(Continued from page 1)

As a next step, let's create a log4j.properties file and put it in our source directory so that log4j can find it. Note that you can use a log4j.xml file too, but .properties files are a little simpler so I'll use them in this example.

Adding 'log4j.properties' to project

The log4j.properties file is shown below.

log4j.properties

# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

This is a standard JavaSW properties file. Comments are indicated by lines beginning with #. The first property (log4j.rootLogger) configures the log4j root logger, which is essentially the logger at the top of the log4j logging hierarchy. Since it's at the top of the hierarchy, setting the log level of the root logger applies to everything unless something lower in the hierarchy is specified to have a different logging level. In this example, the log level is set to INFO, meaning that log.debug() messages won't be logged, but log.info(), log.warn(), log.error(), and log.fatal() will be logged.

An appender is a class that sends the log4j logging messages to a particular place. The ConsoleAppender outputs log messages to our console window. I specified that we are using a console appender and named this appender 'theConsoleAppender'. If we wished to add another type of logging, such as to a log file or to an email server, we can specify these additional appenders on the same line separated by commas.

Notice that we can change the format of the log messages going to the console via the ConversionPattern property. This basically calls the setConversionPattern method on the PatternLayout class, so if you're interested in finding out the various conversion patterns, check out the PatternLayout javadocs.

Now that we've added the log4j.properties file to our source directory, let's run our class.

Execution of Log4JPropertiesTest

As you can see, log4j doesn't find log4j.xml, but it does find log4j.properties at file:/C:/projects/workspace/testing/bin/log4j.properties. Notice that log4j.properties was automatically copied from our src directory to our bin directory by EclipseSW. Since we still have -Dlog4j.debug=true as a VM argument, we can see what log4j is doing. We can see that it reads and parses the properties file to initialize log4j.

After logging is initialized, we can see that the ERROR and INFO messages that we set up are displayed in the console window. Notice that the DEBUG (log.debug()) message is not shown, since this is at a lower level of logging than INFO, which is the root logger logging level that we specified in log4j.properties.


(Continued on page 3)

Page: < 1 2 3 4 >