Log4j2 Description:
Apache Log4j2 is a framework which provides the capability to log messages in a variety of formats to the local console, local files, streamed over a socket or even launch an email based on a hierarchy of notification levels.
Log Level | Description |
FATAL | Severe errors that cause premature termination. |
ERROR | Other run-time errors or unexpected conditions. |
WARN | Run-time situations that are undesirable or unexpected, but not necessarily "wrong". |
INFO | Interesting run-time events (start-up/shutdown). |
DEBUG | Detailed information on the flow through the system. |
TRACE | More detailed information. |
Also defined are
ALL and
OFF.
Log4j2 Installation:
Download a prebuilt Log4j2 jar file: https://logging.apache.org/log4j/2.x/download.html eg. apache-log4j-2.x.x-bin.tar.gz
Installation: cd /opt; sudo tar xzf ~/Downloads/apache-log4j-2.6.2-bin.tar.gz
Includes these two jar files to be placed in your CLASSPATH:
- log4j-api-2.6.2.jar
- log4j-core-2.6.2.jar
Complete list:
[prompt]$ tar tzf apache-log4j-2.6.2-bin.tar.gz
apache-log4j-2.6.2-bin/LICENSE.txt
apache-log4j-2.6.2-bin/NOTICE.txt
apache-log4j-2.6.2-bin/RELEASE-NOTES.txt
apache-log4j-2.6.2-bin/log4j-api-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-api-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-api-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-core-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-core-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-core-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-core-2.6.2-tests.jar
apache-log4j-2.6.2-bin/log4j-iostreams-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-iostreams-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-iostreams-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-jcl-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-jcl-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-jcl-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-jul-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-jul-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-jul-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-flume-ng-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-flume-ng-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-flume-ng-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-1.2-api-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-1.2-api-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-1.2-api-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-slf4j-impl-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-slf4j-impl-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-slf4j-impl-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-to-slf4j-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-to-slf4j-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-to-slf4j-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-jmx-gui-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-jmx-gui-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-jmx-gui-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-taglib-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-taglib-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-taglib-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-web-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-web-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-web-2.6.2-javadoc.jar
apache-log4j-2.6.2-bin/log4j-nosql-2.6.2.jar
apache-log4j-2.6.2-bin/log4j-nosql-2.6.2-sources.jar
apache-log4j-2.6.2-bin/log4j-nosql-2.6.2-javadoc.jar
Add to CLASSPATH:
~/.bashrc
...
...
if [ -d /opt/apache-log4j-2.6.2-bin ]
then
export CLASSPATH=/opt/apache-log4j-2.6.2-bin/log4j-api-2.6.2.jar:/opt/apache-log4j-2.6.2-bin/log4j-core-2.6.2.jar:$CLASSPATH
fi
...
...
Log4j2 Simple Example:
Example Java Class to log:
File:
HelloWorld.java
01 | import org.apache.logging.log4j.LogManager; |
02 | import org.apache.logging.log4j.Logger; |
04 | public class HelloWorld { |
05 | private static final Logger logger = LogManager.getLogger( "HelloWorld" ); |
07 | public static void main(String[] args) { |
09 | logger.trace( "Hello, World!" ); |
10 | logger.debug( "Debug example!" ); |
11 | logger.info( "Info example!" ); |
12 | logger.warn( "Example of a warning!" ); |
13 | logger.error( "My name is {}" , name); |
File:
log4j2.xml (Log4j2 default configuration file name)
01 | <?xml version= "1.0" encoding= "UTF-8" ?> |
02 | <Configuration status= "INFO" > |
04 | <Console name= "Console" target= "SYSTEM_OUT" > |
05 | <PatternLayout pattern= "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> |
09 | <!-- set this value to one of fatal, error, warn, info, debug or trace --> |
11 | <AppenderRef ref= "Console" /> |
If the log level is not specified, the log level is set to the default: "error"
For more on Log4j2 configuration file options see Apache Log4j2 configuration
File:
build.xml
01 | <?xml version= "1.0" encoding= "utf-8" ?> |
02 | <project name= "IO" default = "jar" basedir= "." > |
03 | <description>Builds, tests, and runs the project Test.</description> |
04 | <property name= "build.dir" value= "./" /> |
05 | <property name= "src.dir" location= "${build.dir}" /> |
07 | <!-- the current path is included to point to the config file log4j2.xml --> |
08 | <pathelement location= "./" /> |
09 | <pathelement location= "/usr/java/latest/lib/tools.jar" /> |
10 | <pathelement location= "/opt/apache-log4j-2.6.2-bin/log4j-api-2.6.2.jar" /> |
11 | <pathelement location= "/opt/apache-log4j-2.6.2-bin/log4j-core-2.6.2.jar" /> |
13 | <target name= "compile" > |
14 | <javac destdir= "${build.dir}" debug= "true" includeAntRuntime= "false" > |
15 | <src path= "${src.dir}" /> |
16 | <classpath refid= "classpath" /> |
19 | <target name= "jar" depends= "compile" > |
20 | <jar jarfile= "hello-world.jar" > |
22 | <attribute name= "Main-Class" value= "HelloWorld" /> |
23 | <attribute name= "Class-Path" value= "classpath" /> |
25 | <fileset dir= "${build.dir}" includes= "**/*.class" /> |
28 | <target name= "run" depends= "jar" > |
29 | <java classname= "HelloWorld" failonerror= "true" fork= "true" > |
31 | <path refid= "classpath" /> |
32 | <path location= "./hello-world.jar" /> |
Building and running example:
- Build: ant
- Run example: ant run
run:
[java] 00:12:07.759 [main] INFO HelloWorld - Info example!
[java] 00:12:07.761 [main] WARN HelloWorld - Example of a warning!
[java] 00:12:07.764 [main] ERROR HelloWorld - My name is Greg
BUILD SUCCESSFUL
Total time: 1 second
Log4j2 Multi-log Example:
This example shows how to log from multiple classes to multiple log file outputs and then control them independently.
It also shows how to define the Log4J2 XML configuration file programatically and control the log levels independently.
Example Java Class to log:
File:
HelloWorld.java
03 | import org.apache.logging.log4j.Logger; |
04 | import org.apache.logging.log4j.LogManager; |
05 | import org.apache.logging.log4j.core.LoggerContext; |
07 | public class HelloWorld { |
09 | System.setProperty( "log4j.configurationFile" , "Log4j2ConfigurationFile.xml" ); |
11 | private static final Logger logger = LogManager.getLogger(HelloWorld. class ); |
12 | private static final Logger loggerf2 = LogManager.getLogger(HelloWorld. class .getName() + "_File2" ); |
14 | public static class InnerClassExample { |
15 | private final Logger loggerInner = LogManager.getLogger( "InnerClassExample.class" ); |
17 | public InnerClassExample() { |
18 | loggerInner.info( "Hello from the inner class" ); |
22 | public static void main(String[] args) { |
25 | logger.trace( "Hello, World!" ); |
26 | logger.debug( "Debug example!" ); |
27 | logger.info( "Info example!" ); |
28 | logger.warn( "Example of a warning!" ); |
29 | logger.error( "My name is {}" , name); |
31 | InnerClassExample innerClassExample = new InnerClassExample(); |
33 | for ( int ii= 0 ; ii< 5 ; ii++) { |
34 | loggerf2.info( " count: " + ii); |
Note the use of a static block assignment to set the Log4J2 configuration file name.
This assignes the file name before anything executes in the application.
Alternatively, the configuration file can be specified with the java command line arguments
-Dlog4j.configuration=file:Log4j2ConfigurationFile.xml where the file is in the
CLASSPATH
or use a fully qualified path:
-Dlog4j.configuration=/path/to/Log4j2ConfigurationFile.xml.
Three loggers are assigned which can be independently configured.
File:
Log4j2ConfigurationFile.xml (Log4j2 configuration file)
01 | <?xml version= "1.0" encoding= "UTF-8" ?> |
02 | <Configuration status= "INFO" > |
04 | <Console name= "Console" target= "SYSTEM_OUT" > |
05 | <PatternLayout pattern= "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> |
07 | <File name= "File" fileName= "app.log" append= "false" > |
09 | <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p - %msg%n</Pattern> |
12 | <File name= "File2" fileName= "app2.log" append= "false" > |
14 | <Pattern>%d %p %c{ 1 .} [%t] %m %ex%n</Pattern> |
19 | <!-- set this value to one of all, off, fatal, error, warn, info, debug or trace --> |
21 | <AppenderRef ref= "Console" /> |
22 | <AppenderRef ref= "File" level= "WARN" /> |
24 | <Logger name= "HelloWorld.InnerClassExample" level= "all" additivity= "false" > |
25 | <AppenderRef ref= "File" /> |
27 | <Logger name= "HelloWorld_File2" level= "all" additivity= "false" > |
28 | <AppenderRef ref= "File2" /> |
This Log4J2 configuration file shows three outup configurations:
- console terminal: output all logs which are "info" or worse (warn, error, fatal)
- output file app.log: for all logs which are "warn" or worse (error, fatal)
- output file app2.log: for all logs which are writen using the log handle "loggerf2" (for loop)
For more on Log4j2 configuration file options see Apache Log4j2 configuration
Building and running example:
- Build: ant
- Run example: ant run
run:
[java] 02:14:10.794 [main] INFO HelloWorld - Info example!
[java] 02:14:10.797 [main] WARN HelloWorld - Example of a warning!
[java] 02:14:10.799 [main] ERROR HelloWorld - My name is Greg
[java] 02:14:10.801 [main] INFO InnerClassExample.class - Hello from the inner class
BUILD SUCCESSFUL
Total time: 1 second
Output from console terminal.
Log file:
app.log
2018-04-07 02:28:01.352 [main] WARN - Example of a warning!
2018-04-07 02:28:01.353 [main] ERROR - My name is Greg
Log file:
app2.log
2018-04-07 02:28:01,354 INFO HelloWorld_File2 [main] count: 0
2018-04-07 02:28:01,354 INFO HelloWorld_File2 [main] count: 1
2018-04-07 02:28:01,354 INFO HelloWorld_File2 [main] count: 2
2018-04-07 02:28:01,354 INFO HelloWorld_File2 [main] count: 3
2018-04-07 02:28:01,354 INFO HelloWorld_File2 [main] count: 4
Using Log4j2 with the "Chainsaw" log file viewer:
Apache Chainsaw is a Java GUI application for viewing log files.
It can accept streaming logs or view a log file.
Chainsaw will filter, color code and display log messages.
The screenshot below is showing two applications streaming log messages to two "receivers" configured in Chainsaw.
Controls on the left panel set the logging focus to the log pointers of your choice.
Right click on the logger pointer and select "Focus on logger-ptr-name" to turn off all other logger pointers you don't want to see including logging from the Chainsaw application itself.
The controls on the right panel define the log levels for the application.
Other Logger Display Programs:
Apache Chainsaw is my personal preference as it can display streaming logs from multiple programs, can ingest log files and works.
Many of the logger programs have their personal strengths but often omit a key basic feature possessed by Chainsaw.
Listed below are two of the Apache groups log file viewers. For a complete list see our list of logger display software.
Log Viewer | Description |
Apache Chainsaw | Display log files, socket streams, filters, supports multiple applications. |
Apache LogFactor5 | Display log files, socket streams, filters, supports multiple applications. LogFactor5 ships with Apache Log4j. Download from http://logging.apache.org/log4j/ RHEL6 install: yum install log4j RHEL6 script: /usr/bin/logfactor5 Run: java -cp log4j-1.2.8.jar org.apache.log4j.lf5.StartLogFactor5 |
Links:

Books: