Log4j configuration example

Like most java developer I use the log4j library to log my applications. Although this library is widely used in the java world, there are only few configuration examples available in the Internet.

This posting will scope the following configuration topics:

  • Defining a (default) root logger for the application
  • Defining an appender to write the logs to console
  • Defining an appender to write the logs to a file
  • Defining an appender to write logs of certain classes to another log file than the default one

Configuring the root logger:

# Define the root logger
log4j.rootLogger=DEBUG, root

First, we define the root logger, which means that this is the default logger for the application, used by all classes if no other special configuration exists for the class.

The first command after the equal symbol defines the log level, the following log levels are usually used: DEBUG > INFO > ERROR.

‘root’ defines the appender which is used by this logger.

Next we will define two possible appender configurations for the ‘root’ logger. An appender defines the location, where any log entries of the logger should be stored. Usually there are two possibilities:

  1. Writing the log to the stdout console, useful during development and debugging in combination with a development platform like Eclipse.
  2. Writing the log entries to a file, usually used in production environments.

Writing log to stdout console:

# Application appender to stdout
log4j.appender.root=org.apache.log4j.ConsoleAppender
log4j.appender.root.Target=System.out
log4j.appender.root.layout=org.apache.log4j.PatternLayout
log4j.appender.root.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

The above listing configures the root appender to write the log to the stdout console. This is defined in line 2 by using the org.apache.log4j.ConsoleAppender as appender class. Line 3 defines System.out as target. Line 4 and 5 defines some layout issues, how the log entries should be displayed.

Writing log to file:

# Application appender to file
log4j.appender.root=org.apache.log4j.RollingFileAppender
log4j.appender.root.File=/SWE/Log/AppLog.log
log4j.appender.root.MaxFileSize=10MB
log4j.appender.root.MaxBackupIndex=10
log4j.appender.root.layout=org.apache.log4j.PatternLayout
log4j.appender.root.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

This listing configures the ‘root’ appender to write the log entries into a log file, AppLog.log. This is done by using the org.apache.log4j.RollingFileAppender class instead of the org.apache.log4j.ConsoleAppender. Line 3 defines the whole path including the name of the log file. The log file if not exists, will be created by log4j, the folder structure must be created by hand. Line 4 defines the maximum file size of the log file until a new one will be created, and the old one will be renamed by adding the date to the original file name. Line 6 and 7 are the same, like in the other listing.

Last but not least, we will define another appender which exclusivly writes the logs of a class to another file.

Writing log of certain classes to another log file:

# SpecialLogger appender
log4j.appender.SpecialLogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.SpecialLogger.datePattern='-'dd'.log'
log4j.appender.SpecialLogger.File=/SWE/Log/SpecialLog.log
log4j.appender.SpecialLogger.MaxFileSize=10MB
log4j.appender.SpecialLogger.MaxBackupIndex=10
log4j.appender.SpecialLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.SpecialLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Write the log of this class to the 'SpecialLogger'
log4j.logger.at.nettania.dev.logexample.core.TestDaemon=DEBUG, SpecialLogger
log4j.additivity.at.nettania.dev.logexample.core.TestDaemon=false

We define another appender, with the name SpecialLogger. Line 2 to 8 configures this logger and writes the output into the file SpecialLog. Line 10 defines that all log entries of the class at.nettania.dev.logexample.core.TestDaemon are written to the SpeicalLogger appender. Line 11 eliminates redundancy, which means, the log output of at.nettania.dev.logexample.core.TestDaemon are exclusivly written to the SpecialLogger appender and not as well to the ‘rootLogger’.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.