What is Log4j2?

Introduction

Log4j is a Java logging framework used for tracking purpose. It’s a very popular and It’s used by a lot of Java projects.

It’s a part of the Apache Logging Services project of the Apache Software Foundation.

You can download it for free from https://logging.apache.org/log4j/

In a software development project is very important to know what’s happening and debugging code for errors is a must-have for any developer.

Tracking issues in our code is key aspect and it should be mandatory for improving of productivity in any piece of software we develop.

For this reason, using Log4j you can have a more versatile tool than a simple System.out.println().

Configure Log4J

To configure Log4J you can use Maven, Gradle, etc or download the jar from the official page and put it in the project library path.

For maven-user, you have to add the following dependencies in your pom.xml

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>

Log4j has the ability to automatically configure itself during initialization or you can create and use a custom configuration file with differents options. Log4j will look in the classpath searching filenames in some commons flexible text format like xml, json, etc. 

If no configuration file could be located the DefaultConfiguration will be used. This will display the output on the console.

For example, create a file called log4j.properties

name=PropertiesConfig
property.filename = logs

appenders = console, file

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/log4j.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

loggers=file
logger.file.name=com.javacrosszone.demo.log4j
#logger.file.level = info
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

rootLogger.level = all
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

Put it in a src/main/resources inside your maven project:

log4j2 properties

Create a main class App.java:

package com.javacrosszone.demo.log4j;

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

/**
* Hello world!
*
*/
public class App 
{
final static Logger logger = LogManager.getLogger(App.class);

public static void main( String[] args )
{
System.out.println( "Hello World!" );

//logs a debug message
if(logger.isDebugEnabled()){
logger.debug("This is debug");
}

//logs an trace message
logger.info("Hi, I'm there!");

//logs an error message
logger.error("This is error");

logger.warn("This is warn");
logger.fatal("This is fatal");

} 
}

Output

My helpful screenshot

If we change the property rootLogger.level we can filter all our log messages.

For the standard levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

Also, if we setup and add the appender.file property, log4j will create a file to write log messages:

log4j2 logfile