Tag Archives: log4j

Using SLF4J (The Simple Logging Facade for Java)

SLF4J is useful tool for logging universally, i.e. independently on logger implementation. Awesome framework and simple to integrate.

To install just add files to build path slf4j-api-1.6.4.jar and logger adapter, for ex. slf4j-log4j12-1.6.4.jar
And that’s all!

Use it with:

Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");

Configuration and using Log4j

Log4j is a very simple and convenient logging service for your java application. To add Log4j you’ve to download it from http://logging.apache.org/log4j and put to project.

Log4j can be configured in 2 ways: with log4j.properties or log4j.xml file. IMHO, xml is more clear for understanding.

Settings initiation from Properties file can be made with org.apache.log4j.PropertyConfigurator class, but for Xml one you’ve to use org.apache.log4j.xml.DOMConfigurator class.

To test and learn Log4j you can use the following log4j.xml config:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="consoleAppender1">
 <param name="Threshold" value="INFO" />
 <param name="ConversionPattern"
 value="root - %d{dd-MM-yyyy HH:mm:ss,SSS} [%p] %t - %c [Line:%L] - %m%n" />

 <appender name="consoleAppender2">
 <param name="Threshold" value="INFO" />
 <param name="ConversionPattern"
 value="com.foo - %d{dd MM yyyy HH:mm:ss,SSS} [%p] %t %c - %m%n" />

 <appender name="consoleAppender3">
 <param name="Threshold" value="INFO" />
 <param name="ConversionPattern"
 value="com.foo.Bar - %d{dd-MM-yyyy HH:mm:ss,SSS} [%p] %t - %c [Line:%L] - %m%n" />

 <appender name="fileAppender">
 <param name="Threshold" value="INFO" />
 <param name="File" value="logs/common.log" />
 <param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss,SSS} [%p] %t - %c [Line:%L] - %m%n" />

 <logger name="com.foo.Bar" additivity="true">
 <level value="INFO" />
 <appender-ref ref="consoleAppender3" />
 <appender-ref ref="fileAppender" />

 <logger name="com.foo" additivity="true">
 <level value="OFF" />
 <appender-ref ref="consoleAppender2" />
 <appender-ref ref="fileAppender" />

 <level value="WARN" />
 <appender-ref ref="consoleAppender1" />


And here is class Bar:

package com.foo;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Bar
	private static Logger logger = Logger.getLogger(Bar.class);

	public static void main(String[] args)
		// Load settings
		// Test
		logger.info("Hello World!");

After executing we’ll see the output:

com.foo.Bar - 19 01 2012 12:56:42,848 [INFO] main com.foo.Bar - Hello World!
com.foo - 19 01 2012 12:56:42,848 [INFO] main com.foo.Bar - Hello World!
root - 19-01-2012 12:56:42,848 [INFO] main - com.foo.Bar [Line:27] - Hello World!

As you see, Log4j logger’s messages are going up from init class com.foo.Bar, through package logger com.foo to root logger, and because


executes appenders in parent logger by hierarchy, regardless to log level of parent. But Threshold of appenders will check log level.

Here are logging levels by increasing priority:

  • INFO
  • WARN

Pattern layout see here http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

Hope, it helps.