We can configure log4j by setting the Log Level, defining Appender, and specifying Layout objects in a configuration file.
The log4j.properties
file is a log4j
configuration file and it keeps properties in key-value pairs.
By default, the LogManager
looks for a file named log4j.properties in the CLASSPATH.
The following code shows the syntax of log4j.properties file for an appender X:
# Define the root logger with appender X
log4j.rootLogger = DEBUG, X
# 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
log4j supports UNIX-style variable substitution such as ${variableName}.
The following example shows how to create a log4j.properties file.
It sets the level of the root logger as DEBUG and attaches appender named FILE to it.
The appender FILE is defined as org.apache.log4j.FileAppender and writes to a file named "log.out" located in the log directory.
The layout pattern defined is %m%n, which means the printed logging message will be followed by a newline character.
# 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
We have used DEBUG with both the appenders. All the possible options are:
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
ALL
These levels would be explained in Log4j Logging Levels.
Apache log4j provides Appender objects 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.
Property | Description |
---|---|
layout | Appender uses the Layout objects and the conversion pattern to format the logging information. |
target | The target may be a console, a file, or another item depending on the appender. |
level | The level filters the log messages. |
threshold | Appender can have a threshold level and ignores any logging messages that have a level lower than the threshold level. |
filter | The Filter objects decide whether logging requests should be handled by a particular Appender or ignored. |
We can add an Appender object to a Logger as in following the configuration file with the following method:
log4j.logger.[logger-name]=level, appender1,appender..n
We can write same configuration in XML format.
<logger name="com.apress.logging.log4j" additivity="false"> <appender-ref ref="appender1"/> <appender-ref ref="appender2"/> </logger>
To add Appender object, use following method:
public void addAppender(Appender appender);
The addAppender() method adds an Appender to the Logger object. We can add many Appender objects to a logger in a comma-separated list.
All the possible appender options are:
AppenderSkeleton
AsyncAppender
ConsoleAppender
DailyRollingFileAppender
ExternallyRolledFileAppender
FileAppender
JDBCAppender
JMSAppender
LF5Appender
NTEventLogAppender
NullAppender
RollingFileAppender
SMTPAppender
SocketAppender
SocketHubAppender
SyslogAppender
TelnetAppender
WriterAppender
We can use the following list of Layout.
The following
log4j.properties
shows how to log information to the 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
The following xml code rewrites the configuration listed above.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="console" /> </root> </log4j:configuration>
The following
log4j.properties
shows how to log information to a file.
# Root logger option log4j.rootLogger=INFO, file # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender #Redirect to Tomcat logs folder #log4j.appender.file.File=${catalina.home}/logs/logging.log log4j.appender.file.File=C:\\my.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 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
The following xml code rewrites the configuration listed above.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="file" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false" /> <param name="maxFileSize" value="10KB" /> <param name="maxBackupIndex" value="5" /> <!-- For Tomcat --> <param name="file" value="${catalina.home}/logs/my.log" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <root> <level value="ERROR" /> <appender-ref ref="file" /> </root> </log4j:configuration>
The following
log4j.properties
shows how to log information to a file and to the 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:\\my.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 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
The following xml code rewrites the configuration listed above.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <appender name="file" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false" /> <param name="maxFileSize" value="10MB" /> <param name="maxBackupIndex" value="10" /> <param name="file" value="${catalina.home}/logs/my.log" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="console" /> <appender-ref ref="file" /> </root> </log4j:configuration>