By kswaughs | Wednesday, January 27, 2016

log4j 2 custom log level example

Creating custom log levels have become common in enterprise applications for various business purposes. In this article I am explaining you how to create and use them with log4j 2.3 version. In log4j 1.x version, we have to create a new class that extends Level class where as with log4j 2.3 version, we dont require any new java class creations.

In the below example, we will see how to create a log level 'VERIFY' whose priority is greater than INFO level.

Step 1: Configure custom Level VERIFY in log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" strict="true">
    <!-- Define custom levels before using them for filtering below. -->
    <CustomLevels>
        <CustomLevel name="VERIFY" intLevel="190" />
    </CustomLevels>
    <Appenders>
        <RollingFile name="RollingFile" fileName="D:/logs/log.out" 
        filePattern="D:/logs/log-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <pattern>%d{MM-dd-yyyy HH:mm:ss,SSS}|%p|%m%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="5 MB" />
            </Policies>
            <DefaultRolloverStrategy max="180"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="RollingFile" level="info"/>
        </Root>
    </Loggers>
</Configuration>

Step 2: Use this VERIFY log level in application

In log4j 1.x versions, Log4j framework used to look up log4j.xml or log4j.properties in classpath for configuration. But log4j 2.x versions, we have to pass configuration file path as system property "log4j.configurationFile". This also provides place holder based logging.

Log4JTest class
package com.kswaughs;

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

public class Log4JTest {

    static {
        System.setProperty("log4j.configurationFile", "props/log4j-dev.xml");
    }

    static Logger logger = LogManager.getLogger();

    public static void main(String[] args) {

        logger.debug("This is a debug message");
        logger.info("This is an info message");
        logger.log(Level.getLevel("VERIFY"), "This is a sample verify message");
  
        String user = "kswaughs";
        String role = "Java Developer";
  
        logger.info("My name is '{}' and I am a {}", user, role);
    }
}

Output is:

01-27-2016 17:42:57,681|INFO|This is an info message
01-27-2016 17:42:57,682|VERIFY|This is a sample verify message
01-27-2016 17:42:57,683|INFO|My name is 'kswaughs' and I am a Java Developer

Use Below maven dependency in your pom.xml to get this log4j framework

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

Useful Links

Create custom log level with log4j 1.x

Recommend this on


No comments:

Post a Comment