Log4j + Separate Logger for specific classes or package

 

We may seen writing logger for entire application or separate DEBUG or ERROR log files. In this tutorial we will see how to set separate logger for specific package or specific class using log4j API.

Log4j + Separate Logger for specific classes or package

In above snapshot we can see 2 packages called "com.SeparateLoggerTest" and "com.newpackage". Where "newpackage" having 2 classes and we are trying to route logs of these classes to separate log file. Lets see simple sample how to achieve this.

log4j.properties

#------------------------------------------------
# Root logger option
#------------------------------------------------

log4j.rootLogger=DEBUG, applogger

# Redirect log messages to a log file, support file rolling.
log4j.appender.applogger=org.apache.log4j.RollingFileAppender
log4j.appender.applogger.File=C:\\log\\mainlog.log
log4j.appender.applogger.MaxFileSize=1MB
log4j.appender.applogger.MaxBackupIndex=5
log4j.appender.applogger.layout=org.apache.log4j.PatternLayout
log4j.appender.applogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

#------------------------------------------------
# Package logger option
#------------------------------------------------

log4j.logger.com.newpackage=DEBUG, packageLogger
log4j.additivity.com.newpackage=false

log4j.appender.packageLogger=org.apache.log4j.RollingFileAppender
log4j.appender.packageLogger.File=C:\\log\\separatepackage.log
log4j.appender.packageLogger.MaxFileSize=1MB
log4j.appender.packageLogger.MaxBackupIndex=5
log4j.appender.packageLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.packageLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


In above log4j.properties file we can see section under "Package logger option" separate logger for complete package classes "com.newpackage". Also log4j.additivity.com.newpackage will used to manage logger write options.
If we set "false" then it will write logs only in separate log file which mentioned under package logger.
If we set "true" then logs will written in both root log file as well under package logger.

MainClass.java

package com.SeparateLoggerTest;

import org.apache.log4j.Logger;

import com.newpackage.SpecialPackageClass_1;
import com.newpackage.SpecialPackageClass_2;


public class MainClass {
 
 final static Logger logger = Logger.getLogger(MainClass.class);
 
    public static void main( String[] args ) {
     
     logger.info("Hello Logger ::::::::::::: "+new MainClass().getClass());
     
        new SpecialPackageClass_1().myMethod();
        new SpecialPackageClass_2().myMethod();
    }
}


SpecialPackageClass_1.java

package com.newpackage;

import org.apache.log4j.Logger;

public class SpecialPackageClass_1 {
 
 final static Logger logger = Logger.getLogger(SpecialPackageClass_1.class);
 
    public void myMethod() {
     
     logger.info("Hello Logger ::::::::::::: "+new SpecialPackageClass_1().getClass());
     
    }
}


SpecialPackageClass_2.java

package com.newpackage;

import org.apache.log4j.Logger;

public class SpecialPackageClass_2 {
 
 final static Logger logger = Logger.getLogger(SpecialPackageClass_2.class);
 
    public void myMethod() {
     
     logger.info("Hello Logger ::::::::::::: "+new SpecialPackageClass_2().getClass());
     
    }
}



OUTPUT:

mainlog.log

Log4j + Separate Logger for specific classes or package


separatepackage.log

Log4j + Separate Logger for specific classes or package

No comments:
Write comments