Sunday, May 17, 2015

Log4j configuration in Java applications

Hello Guys,
This time I come with a very simple, useful and very common requirement that all of our Java developers have.

I used System.out.println extensively when I was new to Java world but after few years(1 or 2 may be) :) I realized that it is not really a good idea to use it in production ready applications. Then slowly moved to Log4j for all logging requirements and made my life simple.

So here I am giving you the configuration file that I am currently using for all my applications. I don't change this file unless my application demands it. I know it is very simple and everyone knows about what I am writing here but I still feel that it would be handy when you guys really need it. I configure this based on my regular requirements and feel free to change this on need basis

Small notes:
  • You can see log4j internal debug statements by enabling  log4j.debug property
  • It basically writes all (INFO, DEBUG, ERROR) level logs into separate files
  • They role the files on daily basis
  • You can change the file paths based on your folder structures
  • Change the package name to enable DEBUG level to your application logging

#log4j.debug=true
# Root logger option
log4j.rootLogger=INFO, infoappend, debugappend, errorappend
log4j.logger.<Give your package name>=DEBUG

# Only DEBUG
log4j.appender.debugappend=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debugappend.File=/opt/apache-tomcat-7.0.40/logs/app_DEBUG.log
log4j.appender.debugappend.DatePattern='.'yyyy-MM-dd
log4j.appender.debugappend.layout=org.apache.log4j.PatternLayout
log4j.appender.debugappend.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.debugappend.filter.A=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.debugappend.filter.A.LevelMin=DEBUG   
log4j.appender.debugappend.filter.A.LevelMax=DEBUG
log4j.appender.debugappend.filter.A.AcceptOnMatch=true

# Only INFO
log4j.appender.infoappend=org.apache.log4j.DailyRollingFileAppender
log4j.appender.infoappend.File=/opt/apache-tomcat-7.0.40/logs/app_INFO.log
log4j.appender.infoappend.DatePattern='.'yyyy-MM-dd
log4j.appender.infoappend.layout=org.apache.log4j.PatternLayout
log4j.appender.infoappend.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.infoappend.filter.A=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.infoappend.filter.A.LevelMin=INFO   
log4j.appender.infoappend.filter.A.LevelMax=INFO
log4j.appender.infoappend.filter.A.AcceptOnMatch=true

# Only ERROR
log4j.appender.errorappend=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorappend.DatePattern='.'yyyy-MM-dd
log4j.appender.errorappend.File=/opt/apache-tomcat-7.0.40/logs/app_ERROR.log
log4j.appender.errorappend.layout=org.apache.log4j.PatternLayout
log4j.appender.errorappend.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.errorappend.filter.A=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.errorappend.filter.A.LevelMin=ERROR   
log4j.appender.errorappend.filter.A.LevelMax=ERROR
log4j.appender.errorappend.filter.A.AcceptOnMatch=true

#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log

# Direct log messages to a log file and role it based on it's size
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C://app.log
#log4j.appender.file.MaxFileSize=10MB
#log4j.appender.file.MaxBackupIndex=10

# Direct log messages to console
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


No comments:

Post a Comment