Saturday, 21 November 2015

Generating multiple log file using log4j

It is a good practise to separate out the application log and error log for your application. With a distinguish error logs , monitoring of the project becomes easy and debugging process becomes easy.

Usually in application log, we write many useful information depending upon what the default logging level is set. 

With the latest development in Log4J , the set of built-in levels includes TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. 

If you want to create two different files , one for application and one for errors log, following configuration is required to add in the log4j.properties file kept in the classpath : 
----------------------------------------------------------
# Root logger option
log4j.rootLogger=INFO, app, error

# application log file
log4j.appender.app=org.apache.log4j.RollingFileAppender
log4j.appender.app.File=${catalina.home}/logs/application.log
log4j.appender.app.MaxFileSize=10MB
log4j.appender.app.MaxBackupIndex=10
log4j.appender.app.layout=org.apache.log4j.PatternLayout
log4j.appender.app.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n


#error logs
log4j.appender.error=org.apache.log4j.RollingFileAppender
log4j.appender.error.File=${catalina.home}/logs/error.log
log4j.appender.error.MaxFileSize=10MB
log4j.appender.error.MaxBackupIndex=10
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n
log4j.appender.error.Threshold=ERROR
-----------------------------------------------------

File path is used here is the default set value of the tomcat catalina home , if you are using some other application server , then you can use the ${SERVER_HOME} path  or whatever the path , you would like to add.

Above configuration generates two files : 

1 . application.log which contains all the logging information  of INFO and upper level  of logging levels.
2. error.log file only contains ERROR logging level information, so whatever you want keep in the error logs .

No comments:

Post a Comment