Java Log4j Configuration Sample Code

Most Java applications require a standard logging mechanism. Log4j is a Java based logging utility that is widely used among Java programmers. This tutorial explains how to configure the log4j xml and how to log to different log files based on different thresholds.

After you go through the log4j installation tutorial, you can then import the required log4j libraries.

package com.jcd.java.tutorials;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class JavaLog4JLoggingSetup 
{
	
	static Logger logger = LogManager.getRootLogger();

	public static void main(String[] args) 
	{
		logger.debug("Testing Debug Message");
		logger.error("Testing Error Message");
		logger.trace("Testing Trace Message");
		logger.warn("Testing Warning Message");
		logger.info("Testing Info Message");
	}

}



Now tell log4j how we want the log messages to be handled. The following basic log4j.xml tells log4j to output all log messages to the console (standard out).

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="Console">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="trace">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>



In Eclipse navigate to Run >> Run Configurations >> Arguments . Create a file called log4j.xml and copy it to your Project/conf/ folder. In the VM Arguments section you need to include an entry similar to the one below and click Apply.



Now compile and run the sample code above, you will see that all of the logging is sent to the screen by default.

07:38:31.022 [main] DEBUG  - Testing Debug Message
07:38:31.025 [main] ERROR  - Testing Error Message
07:38:31.025 [main] TRACE  - Testing Trace Message
07:38:31.025 [main] WARN   - Testing Warning Message
07:38:31.025 [main] INFO   - Testing Info Message



Log4j is very powerful and very configurable. One of the main confusions Java programmers have is with the log4j log levels. Take look at the chart below. The log levels fall into a hierarchy. If you set the log level of an appender in your log4j.xml file to FATAL, log4 is only going to log FATAL log messages. Now if you set log level to TRACE, it is going to log everything from TRACE to FATAL.



The log4j.xml file below has two appenders, one for standard out and one for logging to a file. If you look at AppenderRef section for FILE reference, we have set the level to DEBUG, which means we are going to log DEBUG, INFO, WARN, ERROR, and FATAL messages to application.log file.
If you look at the AppenderRef section for STDOUT reference, we have set level to INFO, which means all INFO, WARN, ERROR, and FATAL messages will be sent to standard out. Yes you can have the same log level message be sent to different locations.

?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
     
      <Appenders>
        <File name="FILE" fileName="log/application.log">
          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
        <Console name="STDOUT" target="SYSTEM_OUT">
          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
      </Appenders>
      
      <Loggers>
        <Root level="trace">
          <AppenderRef ref="FILE" level="DEBUG"/>
          <AppenderRef ref="STDOUT" level="INFO"/>
        </Root>
      </Loggers>
      
</Configuration>



You can see from the screenshot that DEBUG, ERROR, WARN, and INFO messages were sent to the application.log file and ERROR, WARN, INFO were also sent to standard out.



References:

Add a Comment

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