Logging Different Priority Level Messages To Seperate Files

Log4j uses appender to publish logging information to various destinations.

To publish logging information to files log4j provides the following appenders:
  • FileAppender: The simplest file appender provided by log4j to log information to files.
  • RollingFileAppender: Logging all the information to a single file might result in an enormous file. This would make it very difficult to search anything in the file and may risk corruption of the file. To avoid this, log4j provides the rolling file appender that logs information to multiple files and rolls over to another file whenever a certain file size is reached.
  • DailyRollingFileAppender: This appender takes a date pattern and uses it to roll over to another file.

We will use the simple FileAppender to demonstrate our example.


Configuration File

The log4j configuration file used to capture logging information in multiple files in shown below. This configuration would log message with different priority levels in separate files.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
    
    <appender name="file_debug" class="org.apache.log4j.FileAppender">
<param name="file" value="debug.log" />
<param name="threshold" value="debug" /> <layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="DEBUG" /> </filter> </appender> <appender name="file_info" class="org.apache.log4j.FileAppender">
<param name="file" value="info.log" />
<param name="threshold" value="info" /> <layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <appender name="file_error" class="org.apache.log4j.FileAppender">
<param name="file" value="error.log" />
<param name="threshold" value="error" /> <layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%m%n" /> </layout> </appender> <logger name="com.cubearticle.examples" additivity="false"> <level value="debug" /> <appender-ref ref="file_debug" /> <appender-ref ref="file_info" /> <appender-ref ref="file_error" /> </logger> <root> <level value="debug" /> <appender-ref ref="file_debug" /> </root> </log4j:configuration>

We have defined 3 different appenders, each of which is responsible to publish logging information at different priority levels into different files (debug.log, info.log and error.log). The logger is provided with a reference of all these appenders.

Note that we have used filters in the file_debug and file_info appenders. These are used to filter the log information published by these appenders. By default the appenders with priority level debug or info would publish information of higher priorities. We have filtered out these information using filters.


Java Class

We will create a simple java class as shown below to captures some logging messages and print them to different files:

package examples;

import org.apache.log4j.Logger;

public class TestLogging {
	
    public static Logger logger = Logger.getLogger(TestLogging.class);
	
    public static void main(String args[]){
	if(logger.isDebugEnabled()){
    	    logger.debug("Logging Debug Messages");
	}
		
	if(logger.isInfoEnabled()){
	    logger.info("Logging Info Messages");
	}
	
	logger.error("Logging Error messages");
    }
}

The above java class captures logging messages at three different priority levels: debug, info and error and publish them to three different files: debug.log, info.log and error.log.

RELATED ARTICLES

Introduction to Logging with Log4J

Log4j is an open-source logging framework that helps developers to capture logging statements in java applications. It is fully configurable and can be configured using external configuration files.

View Article

Integrating Log4J with Perf4J for Performance Logging

Perf4j is an open source logging framework used primarily for monitoring performance statistics in java applications. Log4j has the ability to integrate with perf4j to capture performance data.

View Article

Using Slf4J Facade with Log4j

Slf4j is a high level logging framework. Its serves as a facade and the actual logging is performed by the underlying logging framework which can be determined at runtime.

View Article