Sunday, April 10, 2011

log4j config

Which app server are you using? Each one puts its logging config in a different place, though most nowadays use Commons-Logging as a wrapper around either Log4J or java.util.logging.
Using Tomcat as an example, this document explains your options for configuring logging using either option. In either case you need to find or create a config file that defines the log level for each package and each place the logging system will output log info (typically console, file, or db).
In the case of log4j this would be the log4j.properties file, and if you follow the directions in the link above your file will start out looking like:

#------------------------------------------------------------------------------
#
#  The following properties set the logging levels and log appender.  The
#  log4j.rootCategory variable defines the default log level and one or more
#  appenders.  For the console, use 'S'.  For the daily rolling file, use 'R'.
#  For an HTML formatted log, use 'H'.
#
#  To override the default (rootCategory) log level, define a property of the
#  form (see below for available values):
#
#        log4j.logger. =
#
#    Available logger names:
#      TODO
#
#    Possible Log Levels:
#      FATAL, ERROR, WARN, INFO, DEBUG, TRACE
#
#------------------------------------------------------------------------------

log4j.rootCategory=DEBUG, S, ALL, ERROR

log4j.logger.com.dappit.Dapper.parser=ERROR
log4j.logger.org.w3c.tidy=FATAL

#------------------------------------------------------------------------------
#
#  The following properties configure the console (stdout) appender.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.S = org.apache.log4j.ConsoleAppender
log4j.appender.S.layout = org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = [%d{dd-MM-yyyy HH:mm:ss.SSS}] [%p] [%c]: %m%n

#------------------------------------------------------------------------------
#
#  The following properties configure the Daily Rolling File appender which includes all the log level messages.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.ALL = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ALL.File = {ur_app_log_dir}/{fileName}.log
log4j.appender.ALL.Append = true
log4j.appender.ALL.DatePattern = '.'yyyy-MM-dd
log4j.appender.ALL.layout = org.apache.log4j.PatternLayout
log4j.appender.ALL.layout.ConversionPattern = [%d{dd-MM-yyyy HH:mm:ss.SSS}] [%p] [%c{1}]: %m%n

#------------------------------------------------------------------------------
#
#  The following properties configure the Daily Rolling File appender which includes all the log level messages.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File = {ur_app_log_dir}/error.log
log4j.appender.ERROR.Append = true
log4j.appender.ERROR.Threshold = ERROR
log4j.appender.ERROR.DatePattern = '.'yyyy-MM-dd
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = [%d{dd-MM-yyyy HH:mm:ss.SSS}] [%p] [%c{1}]: %m%n

#------------------------------------------------------------------------------
#
#  The following properties configure the package level log level filtering. you can restrict the useless log messages of ThirdParty API's.
#  Eg: SpringFramework's trace, debug, info messages are not much useful for us, so can be restricted like this.
#  log4j.logger.org.springframework=WARN
#
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.logger.{desired_package}={Level}
 
--cheers,
Varra 

No comments:

Post a Comment