How to – log4j

1. Have log4j jar file to place into:

2. Have property file to place into :



3 .Example property file will :

log4j.rootLogger=WARN, Other
log4j.appender.Other=org.apache.log4j.RollingFileAppender
log4j.appender.Other.File=D:/Work/web/WEB-INF/logs/root.log
log4j.appender.Other.MaxFileSize=1000KB
log4j.appender.Other.MaxBackupIndex=3
log4j.appender.Other.layout=org.apache.log4j.PatternLayout
log4j.appender.Other.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

log4j.logger.my.com.leadingside=DEBUG, appLog
log4j.additivity.my.com.leadingside=false
log4j.appender.appLog=org.apache.log4j.RollingFileAppender
log4j.appender.appLog.File=D:/Work/web/WEB-INF/logs/app.log
log4j.appender.appLog.MaxFileSize=1000KB
log4j.appender.appLog.MaxBackupIndex=3
log4j.appender.appLog.layout=org.apache.log4j.PatternLayout
log4j.appender.appLog.layout.ConversionPattern=%d %-5p %c - %m%n

4. Copy below code for testing

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

public class TestLogger{
static Logger myLogger = Logger.getLogger(TestLogger.class.getName( ));

public static void main(String [] args){
    myLogger.setLevel(Level.DEBUG);
    myLogger.warn("SUccess Debug From LOGER");
}
}

5. NOTE / Attention !!

DEBUG, INFO, WARN, ERROR, FATAL
following sequense, debug is the lowest level, means, it will log for
INFO, WARN, ERROR, FATAL
BUT if we set level to WARN, logger will log only ERROR, FATAL.
eg: myLogger.setLevel(Level.ERROR);
myLogger.warn(“SUccess Debug From LOGER”);

 

dicksonkho

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.