By kswaughs | Monday, December 21, 2015

How to create custom Log Level in log4J

Log4J provides different log levels like DEBUG, INFO, WARN, ERROR, TRACE which are helpful in debugging the applications. Sometimes we might need some custom log levels for various other business purposes. Log4J provides the flexibility of adding new custom log levels.

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

Step 1 : Create a new class VerifyLevel that extends org.apache.log4j.Level and override toLevel() methods of its class.

VerifyLevel class
 
package com.kswaughs.levels;

import org.apache.log4j.Level;

public class VerifyLevel extends Level {

    private static final long serialVersionUID = 8935350210797995740L;

    public static final int VERIFY_INT = Level.INFO_INT + 10;

    public static final Level VERIFY = new VerifyLevel(VERIFY_INT, "VERIFY", 7);
 
    protected VerifyLevel(int arg0, String arg1, int arg2) {
        super(arg0, arg1, arg2);
    }
 
    public static Level toLevel(String sArg) {
  
        if("VERIFY".equalsIgnoreCase(sArg)) {
            return VERIFY;
        }
        return (Level) toLevel(sArg, Level.DEBUG);
    }
 
    public static Level toLevel(int val) {
 
        if (val == VERIFY_INT) {
            return VERIFY;
        }
        return (Level) toLevel(val, Level.DEBUG);
    }
 
    public static Level toLevel(int val, Level defaultLevel) {
  
        if (val == VERIFY_INT) {
            return VERIFY;
        }
        return Level.toLevel(val, defaultLevel);
    }

    public static Level toLevel(String sArg, Level defaultLevel) {
  
        if("VERIFY".equalsIgnoreCase(sArg)) {
            return VERIFY;
        }
        return Level.toLevel(sArg, defaultLevel);
    }
}

Step 2: Configure VerifyLevel in log4j.properties

# Root logger option
log4j.rootLogger=debug, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.File=D:/logs/log.out
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.category.VERIFY=VERIFY#com.kswaughs.levels.VerifyLevel

Step 3: Use this VERIFY log level in application

Log4JTest class
package com.kswaughs;

import org.apache.log4j.Logger;

import com.kswaughs.levels.VerifyLevel;

public class Log4JTest {

    static Logger logger = Logger.getLogger(Log4JTest.class.getName());
   
    public static void main(String[] args) {
   
        logger.debug("This is a debug message");
        logger.info("This is an info message");
        logger.log(VerifyLevel.VERIFY, "This is a sample verify message");
    }
}

Output is:

2015-12-22 11:09:25 DEBUG Log4JTest:14 - This is a debug message
2015-12-22 11:09:25 INFO  Log4JTest:15 - This is an info message
2015-12-22 11:09:25 VERIFY Log4JTest:16 - This is a sample verify message

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

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency> 

Recommend this on


No comments:

Post a Comment