Home Navigation

Thursday 29 September 2016

log4j tips

Pattern tokens to avoid:


C For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".
WARNING Generating the caller class information is slow. Thus, use should be avoided unless execution speed is not an issue.
F Used to output the file name where the logging request was issued.
WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
L Used to output the line number from where the logging request was issued.
WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
M Used to output the method name where the logging request was issued.
WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.

Sample pattern:

[%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%t] - %m%n


Levels:

Levels used for identifying the severity of an event. Levels are organized from most specific to least:

  • OFF (most specific, no logging)
  • FATAL (most specific, little data)
  • ERROR
  • WARN
  • INFO
  • DEBUG
  • TRACE (least specific, a lot of data)
  • ALL (least specific, all data)

Typically, configuring a level in a filter or on a logger will cause logging events of that level and those that are more specific to pass through the filter. A special level, ALL, is guaranteed to capture all levels when used in logging configurations.

Two logger appender Example:

# First you set the level of logger and then followed by appender name
log4j.rootLogger=WARN, file
log4j.logger.progress=DEBUG, progressAppender

#disconnects the arrow from a logger up to its parent
log4j.additivity.progress=false

#disconnects the arrow from a logger up to its parent
log4j.additivity.progress=false

#Progress file appender
log4j.appender.progressAppender=org.apache.log4j.RollingFileAppender
log4j.appender.progressAppender.File=${logname}.txt
log4j.appender.progressAppender.MaxFileSize=6MB
log4j.appender.progressAppender.MaxBackupIndex=5
log4j.appender.progressAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.progressAppender.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%t] - %m%n

#Error file appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${logname}.log
log4j.appender.file.MaxFileSize=6MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%t] - %m%n


No comments:

Post a Comment