Log4j - The configuration file

log4j

Where should we keep the log4j.properties file?

I am not quite sure on this. This is perhaps dependent on the container. There may be a standard place that this file should be kept under, or in the same directory as your program. However, I had this file in the same directory as my program, and it didn't work so I had to use the -Dlog4j.configuration parameter (see below) to specify the location of the log4j.properties file. Inside your code, you can use PropertyConfigurator.configure(path) to use a specific log4j configuration file.

How can we change the location of the log4j.properties file?

"C:\bea\jdk1.7.0_45\bin\java" -Dlog4j.configuration=file:///C:/SaveOrClone/log4j.properties -jar SaveOrClone.jar

Note, that property value must be a URL.

Can the parent logger be created after the child logger has been created?

Yes. A "parent" logger will find and link to its descendants even if it is instantiated after them.

Does log4j restrict the possible set of loggers?

No. It easy to name loggers by software component. This can be accomplished by statically instantiating a logger in each class, with the logger name equal to the fully qualified name of the class. This is a useful and straightforward method of defining loggers. As the log output bears the name of the generating logger, this naming strategy makes it easy to identify the origin of a log message. However, this is only one possible, albeit common, strategy for naming loggers. Log4j does not restrict the possible set of loggers. The developer is free to name the loggers as desired.

How can we configure a logger?

log4j.rootLogger = DEBUG, A1
log4j.logger.com.foo=DEBUG, A1

The above configuration assign the DEBUG as the default log level for each logger. It also assign the appender name A1 to each logger. See elsewhere on this page for information on how to define or configure appenders.

How can we instruct the logger to log to multiple destinations?

We can assign multiple appenders to the same logger:

log4j.rootLogger = DEBUG, A1, A2
log4j.logger.com.foo=DEBUG, A1, A2

The above configuration assign the DEBUG as the default log level for each logger. It also assign the multiples appenders (named A1 and A2) to each logger. See elsewhere on this page for information on how to define or configure appenders.

Example configuration file:

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO,logfile1,logfile,debug1,syslog

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.debug1=org.apache.log4j.ConsoleAppender
log4j.appender.debug1.Threshold=INFO
log4j.appender.debug1.layout=org.apache.log4j.PatternLayout
log4j.appender.debug1.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.Threshold=INFO
log4j.appender.logfile.File=submitter.log
log4j.appender.logfile.ImmediateFlush=true
#log4j.appender.logfile.Append=false
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
log4j.appender.logfile.MaxFileSize=10000KB
log4j.appender.logfile.MaxBackupIndex=50

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.logfile1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile1.Threshold=FATAL
log4j.appender.logfile1.File=submitter1.log
log4j.appender.logfile1.ImmediateFlush=true
log4j.appender.logfile1.Append=false
log4j.appender.logfile1.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile1.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

log4j.appender.syslog=org.apache.log4j.net.SyslogAppender
log4j.appender.syslog.Facility=LOCAL7
log4j.appender.syslog.FacilityPrinting=false
log4j.appender.syslog.Header=true
log4j.appender.syslog.SyslogHost=paperTrailInstanceName.papertrailapp.com:portNumber
log4j.appender.syslog.layout=org.apache.log4j.PatternLayout
log4j.appender.syslog.layout.ConversionPattern=%p: %c{2} %x %m %n

Example configuration files:

log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%t %-5p %c{2} - %m%n
# Set the appender named X to be a File appender
log4j.appender.X=org.apache.log4j.FileAppender

# Define the layout for X appender
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.conversionPattern=%m%n
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
log4j.rootLogger = DEBUG,abc
log4j.appender.abc = org.apache.log4j.FileAppender
log4j.appender.abc.file = my.txt
log4j.appender.abc.layout = org.apache.log4j.SimpleLayout
# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

The first statement assign the log level DEBUG to the the root logger. It also assign the A1 appender to the root logger. The second line define the A1 appender as a ConsoleAppender. The rest of the above configuration file set various attributes for the A1 appender.

Sample log4.properties file to send logs to file system:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=logs/myLogs.txt

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true

# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=5KB

# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Sample log4.properties file to send logs to DB2 database on AS400:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, DB

# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender

# Set JDBC URL
log4j.appender.DB.URL=jdbc:as400://localhost;naming=system;errors=full;

# Set Database Driver
log4j.appender.DB.driver=com.ibm.as400.access.AS400JDBCDriver

# Set database user name and password
log4j.appender.DB.user=UserId
log4j.appender.DB.password=Password

# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO myLogs VALUES('%X{User}','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m')

# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

Sample log4.properties file to send logs to MySQL database:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, DB

# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender

# Set JDBC URL
log4j.appender.DB.URL=jdbc:mysql://localhost:3306/world

# Set Database Driver
log4j.appender.DB.driver=com.mysql.jdbc.Driver

# Set database user name and password
log4j.appender.DB.user=root
log4j.appender.DB.password=mysql

# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO myLogs VALUES('%X{User}','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m')

# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

You must download Java MySQL connector jar file and put that in the project classpath. SQL create Table statement for the LOG file:

CREATE TABLE myLogs (             
   USER_ID  varchar(20) NOT NULL,     
   LOG_DATE  date NOT NULL,           
   LOGGER  varchar(50) NOT NULL,      
   LEVEL  varchar(10) NOT NULL,       
   MESSAGE  varchar(1000) NOT NULL    
)

How can we ignore log message belonging to the com.foo package?

log4j.rootLogger=DEBUG, 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

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

As the logger com.foo.Bar does not have an assigned level, it inherits its level from com.foo, which was set to WARN in the configuration file. The log statement from the Bar.doIt method has the level DEBUG, lower than the logger level WARN. Consequently, doIt() method's log request is suppressed.

How can we use multiple appenders?

Here is another configuration file that uses multiple appenders.

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

In addition, as the root logger has been allocated a second appender, output will also be directed to the example.log file. This file will be rolled over when it reaches 100KB. When roll-over occurs, the old version of example.log is automatically moved to example.log.1.

How can we use FileAppender?

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:\\log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, overwrite
log4j.appender.FILE.Append=false

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Example configuration file that log to console:

# Root logger option  
log4j.rootLogger=INFO, stdout  

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

Example configuration file that log to a file using FileAppender:

# Define the root logger with appender file
log4j.rootLogger=DEBUG, spring

# Define the file appender
log4j.appender.spring=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.spring.File=log_spring.txt

# Set the immediate flush to true (default)
log4j.appender.spring.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.spring.Threshold=debug

# Set the append to false, overwrite
log4j.appender.spring.Append=false

# Define the layout for file appender
log4j.appender.spring.layout=org.apache.log4j.PatternLayout
log4j.appender.spring.layout.conversionPattern=%m%n

Example configuration file that log to a file using RollingFileAppender:

# Root logger option  
log4j.rootLogger=INFO, file  

# Direct log messages to a log file  
log4j.appender.file=org.apache.log4j.RollingFileAppender  
log4j.appender.file.File=C:\\logingFile.log  
log4j.appender.file.MaxFileSize=2MB  
log4j.appender.file.MaxBackupIndex=1  
log4j.appender.file.layout=org.apache.log4j.PatternLayout  
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Example configuration file that log both to a file and to console:

# Root logger option

log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logingFile.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

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

What are the properties that we can use to control the behavior of an appender?

Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc. Each Appender object has different properties associated with it, and these properties indicate the behavior of that object.

  • layout: Appender uses the Layout objects and the conversion pattern associated with them to format the logging information.
  • target: The target may be a console, a file, or another item depending on the appender.
  • level: The level is required to control the filtration of the log messages.
  • threshold: Appender can have a threshold level associated with it independent of the logger level. The Appender ignores any logging messages that have a level lower than the threshold level.
  • filter: The Filter objects can analyze logging information beyond level matching and decide whether logging requests should be handled by a particular Appender or ignored.

How can we configure an appender?

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n

How can we use the NTEventLogAppender?

log4j.rootLogger=DEBUG, WindowsEventLog
log4j.appender.WindowsEventLog=org.apache.log4j.nt.NTEventLogAppender
log4j.appender.WindowsEventLog.layout=org.apache.log4j.PatternLayout
log4j.appender.WindowsEventLog.layout.ConversionPattern=%d %-5p %c{3} %3x - %m [%t]%n
log4j.appender.WindowsEventLog.source=MSTRWeb
log4j.appender.WindowsEventLog.Threshold=WARN

We need to download the log4j .zip file, extract it, look for the .dll file, copy the appropriate .dll file to the %SystemRoot%\System32 folder. For the 64-bit platform, copy the NTEventLogAppender.amd64.dll file to the %SystemRoot%\System32 folder. For the 32-bit platform, copy the NTEventLogAppender.dll to the %SystemRoot%\System32 folder.

We also need to use regedit:

  1. Launch regedit
  2. HKLM\SYSTEM\CurrentControlSet\services\eventlog\Application
  3. Create a new key representing the name of your application
  4. Inside this new key, create a new String property and name it EventMessageFile and the value of this property should be the location of the dll file. For 64-bit platform, the value of this property should be %SystemRoot%\System32\NTEventLogAppender.amd64.dll and for 32-bit platform, the value of this property should be %SystemRoot%\System32\NTEventLogAppender.dll

How can we use the SocketAppender?

log4j.appender.REMHOST=org.apache.log4j.net.SocketAppender
log4j.appender.REMHOST.Port=4445
log4j.appender.REMHOST.RemoteHost=localhost

How can we use the SyslogAppender?

log4j.appender.syslog=org.apache.log4j.net.SyslogAppender
log4j.appender.syslog.Facility=LOCAL7
log4j.appender.syslog.FacilityPrinting=false
log4j.appender.syslog.Header=true
log4j.appender.syslog.SyslogHost=logsN.papertrailapp.com:XXXXX
log4j.appender.syslog.layout=org.apache.log4j.PatternLayout
log4j.appender.syslog.layout.ConversionPattern=%p: (%F:%L) %x %m %n

If we are logging to syslog, and we are using rsyslog, we have to comment out the following lines in /etc/rsyslog.conf:

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514

We also need to add 'syslog' to the rootCategory line in the log4j.properties file:

log4j.rootCategory=INFO,debug1,logfile,syslog

What are available layout?

  1. DateLayout
  2. HTMLLayout
  3. PatternLayout
  4. SimpleLayout
  5. XMLLayout

Another example configuration file:

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO,logfile
log4j.rootCategory=INFO,debug1,logfile

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.debug1=org.apache.log4j.ConsoleAppender
log4j.appender.debug1.Threshold=INFO
log4j.appender.debug1.layout=org.apache.log4j.PatternLayout
log4j.appender.debug1.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.Threshold=INFO
log4j.appender.logfile.File=submitter.log
#log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.ImmediateFlush=true
log4j.appender.logfile.Append=false
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n
log4j.appender.logfile.MaxFileSize=500KB
log4j.appender.logfile.MaxBackupIndex=1
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License