Log4j 2 Example

This tutorial explains how to configure the Log4j 2 in your standalone application. Note that, this example will work only of your application is running as standalone. I will write another post for the web application configuration.

The following steps explains how the Log4j 2 library loading the suitable configurations.

  1. Log4j will check the system property “log4j.configurationFile” for the configuration file path.
  2. If system property is not configured, then JSON configuration factory will look for the files log4j2-test.json or log4j2-test.jsn in the classpath.
  3. If JSON test files are not found, then it XML configuration factory will search for the files log4j2-test.xml in the classpath.
  4. Then JSON configuration factory will look for the files log4j2.json or log4j2.jsn in the classpath.
  5. Then XML configuration factory will search for the files log4j2.xml in the classpath.
  6. If all the above steps could not find the configuration details, then default configuration will be configured. Default configuration will output the messages to the console.

Log4j 2 And Maven

If you are using the maven build in your project, please use the following dependency entry in your pom.xml file. As part of the distribution, you have to download the Log4j API and Log4j Core JAR files. The name of the JAR files would be log4j-api-2.0-rc1.jar and log4j-core-2.0-rc1.jar.

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-rc1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-rc1</version>
  </dependency>
</dependencies>

Log4j 2 Example Application

Log4j2Example.java

If you have noticed LogManager.getLogger(), this was used as Logger.getLogger() in the Log4j 1.x version. When you migrate to the latest version, it is one of the important change you have to consider.

package javabeat.net.util;

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

public class Log4j2Example {
	static final Logger logger = LogManager.getLogger(Log4j2Example.class.getName());
	public static void main(String[] args) {
		logger.trace("Entering Log4j Example.");
		Hello hello = new Hello();
		if (!hello.callMe()) {
			logger.error("Ohh!Failed!");
		}
		logger.trace("Exiting Log4j Example.");
	}
}

Hello.java

package javabeat.net.util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Hello {
  static final Logger logger = LogManager.getLogger(Hello.class.getName());

  public boolean callMe() {
    logger.entry();
    logger.error("Inside Hello Logger!");
    return logger.exit(false);
  }
}

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
	<Appenders>
		<Console name="CONSOLE" target="SYSTEM_OUT">
			<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
		</Console>
	</Appenders>
	<Loggers>
		<Root level="error">
			<AppenderRef ref="Console" />
		</Root>
		<logger name="javabeat.net.util" level="TRACE" />
		<Root level="ERROR">
			<AppenderRef ref="CONSOLE"/>
		</Root>
	</Loggers>
</Configuration>

Output

13:55:41.323 [main] TRACE javabeat.net.util.Log4j2Example - Entering Log4j Example.
13:55:41.372 [main] TRACE javabeat.net.util.Hello - entry
13:55:41.372 [main] ERROR javabeat.net.util.Hello - Inside Hello Logger!
13:55:41.372 [main] TRACE javabeat.net.util.Hello - exit with(false)
13:55:41.372 [main] ERROR javabeat.net.util.Log4j2Example - Ohh!Failed!
13:55:41.372 [main] TRACE javabeat.net.util.Log4j2Example - Exiting Log4j Example.

Exception

If you did not configure the appender properly, you may get the following exception. The below exception thrown when you call the “CONSOLE” appender in the logger and did not delcare that in the configuration file.

2014-03-04 12:56:45,241 ERROR Unable to locate appender CONSOLE for logger

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. I copied the files as you posted them and created a Maven Java Application in NetBeans. But the only log statements I get are the errors:

    13:55:41.372 [main] ERROR javabeat.net.util.Hello – Inside Hello Logger!
    13:55:41.372 [main] ERROR javabeat.net.util.Log4j2Example – Ohh!Failed!

    I don’t know what to do to get the non-error messages to output nor how to rework the appender to write to a File.

  2. Manish Agarwal says:

    Showing error in importing org.apache.logging.log4j.LogManager. I think it is not configuring jr in the class path pls suggest.

  3. Manish Agarwal says:

    Showing error in importing org.apache.logging.log4j.LogManager. I think it is not configuring jr in the class path pls suggest.

Speak Your Mind

*