Login

Log4j2 basic configuration

Rodrigo

By Rodrigo

Created 2014-09-17 22:57:23 Modified 2014-09-17 23:24:00

Apache log4j2 is an upgrade to log4j which is a library which allow you to output log statements to a variety of output targets, mostly in a plain text file alongside the console log

In this tutorial we are going to use this library to output some logs statements

this tutorial was written using the following gear:

  • Hardware:           laptop Thinkpad E430 Intel I5, 4gb Ram, 500gb HDD
  • OS:                    x86_64 GNU/Linux Ubuntu 14.10
  • Kernel:               3.13.0-35-generic
  • IDE/Text editor:   Eclipse Java EE IDE for Web Developers Version: Kepler Service Release 2
  • JDK:                   Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
  • log4j:                  apache-log4j-2.0.1

 

1.- Setting up the environment:

First we need to download the library in order to add it to our project and to use it to output some logs, so go to http://logging.apache.org/log4j/2.x/download.html and download the latest version

2.- Creating the log4j2example project:

Once downloaded the library, let's create our project which will be called log4j2example, so in eclipse go to file => new => java project and click next

now set the name project "log4j2example" and click ok

3.- Adding the library:

Now we have to add the downloaded library to our project, so first create the folder "lib" inside the project, in the root of the project, go to file => new => folder and create the "lib" folder

once the folder is created, copy the following libraries into the lib folder:

  • log4j-api-2.0.1.jar
  • log4j-core-2.0.1.jar

now do right click on the project build path => configure build path => java build path => add jars, then select the 2 jars that you copied before and click ok

 

 

4.- Creating the main class:

We have setted up the whole configuration, now we need to create the main class in order to test our logs output, so go to file => new => class and create the Logexample class inside the "com.rrivero.log" package

 

then create the main method and add some logs outputs using the lbrary

package com.rrivero.log;

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


public class Logexample {
	
	private static final Logger log = LogManager.getLogger(Logexample.class.getName());

	public static void main(String[] args) {
		
		log.trace("trace message");
		log.debug("debug message");
		log.warn("warn message");
		log.info("info message");
		log.error("error message");
		log.fatal("fatal message");

	}

}

now we need the log4j configuration file so let's create the "log4j2.xml" file on the root of "src" folder:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
	<Properties>
		<Property name="log-path">/home/rodrigo/logs</Property>
	</Properties>

	<Appenders>
		<RollingFile name="file-log" fileName="${log-path}/HelloLog4j2.log" filePattern="${log-path}/HelloLog4j-%d{yyyy-MM-dd}.log">
			<PatternLayout pattern="[%5p] - %c{1}:%L %d{yyyy-MM-dd HH:mm:ss} - %m%n"/>
			<Policies>
				<TimeBasedTriggeringPolicy interval="1"	modulate="true" />
			</Policies>
		</RollingFile>

		<Console name="console" target="SYSTEM_OUT">
			<PatternLayout pattern="[%5p] - %c{1}:%L %d{yyyy-MM-dd HH:mm:ss} - %m%n" />
		</Console>
	</Appenders>
	<Loggers>
	
		<!-- here we restrict the log level by package 
		<Logger name="com.rrivero.log" level="info" additivity="true">
			<appender-ref ref="file-log" level="fatal" />
		</Logger>
		-->
		
		<Root level="debug">
			<appender-ref ref="console" />
			<appender-ref ref="file-log" />
		</Root>
	</Loggers>
</Configuration>

once added the libraries, created the config file and created the main class we should have the following structure of the project:

 

and the following .classpath:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
	<classpathentry kind="lib" path="lib/log4j-api-2.0.1.jar"/>
	<classpathentry kind="lib" path="lib/log4j-core-2.0.1.jar"/>
	<classpathentry kind="output" path="bin"/>
</classpath>

5.- Testing the outputs:

Up to this point we are done, configure your path to the output on the config file and the name of the log:

<Property name="log-path">/home/rodrigo/logs</Property>

 

<RollingFile name="file-log" fileName="${log-path}/HelloLog4j2.log" filePattern="${log-path}/HelloLog4j-%d{yyyy-MM-dd}.log">

and execute the main class, if thing go well we'll have our output in the folder that we specified in the config file:

 

and we'll have the following output:

[DEBUG] - Logexample:14 2014-09-17 17:02:09 - debug message
[ WARN] - Logexample:15 2014-09-17 17:02:09 - warn message
[ INFO] - Logexample:16 2014-09-17 17:02:09 - info message
[ERROR] - Logexample:17 2014-09-17 17:02:09 - error message
[FATAL] - Logexample:18 2014-09-17 17:02:09 - fatal message

notice that we don't have any trace log, because we defined it in the config file for both outputs:

<Root level="debug">
			<appender-ref ref="console" />
			<appender-ref ref="file-log" />
		</Root></pre>

so if we want to have the full output just change the config and set "trace" level, for production enviroment is recommended to use logs in info level

Download it in https://drive.google.com/file/d/0BwOqSob-RrBOT1l4UXdLZ095clE/edit?usp=sharing

6.- Conclusion:

Logs are always needed and I dare to say mandatory to have it in any application, to see what's going on along the application itself, in case to have an error, logs always save your life and your job...

7.- Reference links:




comments powered by Disqus