Log4j Configurations – Controlling Logging to Multiple Files


The purpose of inserting log statements into the code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is often the case for distributed applications.
Features of Log4j

We can enable logging at runtime without modifying the application binary.

We can control the behavior of logging by editing only the configuration file, no need to touch the application binary.

Developer are always clear with detailed context for application failures.

Log4j has one of the distinctive features – the notion of inheritance. Using this logger hierarchy feature we are able to control the log statements output at arbitrarily fine granularity but also at great ease. This helps to reduce the volume of logged output and the cost of logging.
We can set many targets for the log output such as a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or many other output targets.

Loggin Levels

FATAL: shows messages at a FATAL level only
ERROR: Shows messages classified as ERROR and FATAL
WARNING: Shows messages classified as WARNING, ERROR, and FATAL
INFO: Shows messages classified as INFO, WARNING, ERROR, and FATAL
DEBUG: Shows messages classified as DEBUG, INFO, WARNING, ERROR, and FATAL
ALL – The ALL Level has the lowest possible rank and is intended to turn on all logging.
OFF – The OFF Level has the highest possible rank and is intended to turn off logging.

For more information please go through Apache Log4j http://logging.apache.org/log4j/2.x/

I will show you how we can control logging output to multiple log files. For example, we want to show logging for a package structure com.roytuts.* in a file called “log.log” but we want to show logging for a package structure com.roytuts.json in another file called “json.log”, then we want to create two different log files. We also want to exclude the logging from file “log.log” for package structure com.roytuts.json which is already appended to the “json.log” file.


The following configurations are required in order to run the application

JDK 1.7
Have maven installed and configured
slf4j dependency in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : roytuts

Step 2. Modify the pom.xml file as shown below.

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.7 from the next window. Click on Finish then Ok.

Step 4. For testing logger working correctly please create below two class in different packages

in package com.roytuts create below class

So logging should be printed in log.log file

in package com.roytuts.json create below class

So logging should be printed in json.log file but it won’t append to the log.log file because I have added additivity=”false” to the category in log4j.xml

Step 5. Now create log4j.xml file in src/main/resources with the below content

In this file we have created two RollingFileAppender for logging to two different log files.

Step 6. Now run the above main classes, you will find there are two log files with the logging.

json.log file contains following

log.log file contains following

Note: log files are created in the project root directory.

Thanks for reading.


Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.