It is sometimes required in a project to redirect log messages to different files based on their severity. This can be easily done in log4Perl using the below mentioned configuration file entries.
Above configuration redirects log messages to
If you need to redirect one and only one log level to certain log file the configuration will look like below.
Above configuration uses filters to match the mentioned Level. In above case only ERROR logs will be logged.
log4perl.rootLogger=TRACE, errorlog, consolelog , debuglog
log4perl.appender.errorlog=Log::Log4perl::Appender::File
log4perl.appender.errorlog.filename=c:/perl/log-dir/error.log
log4perl.appender.errorlog.mode=append
log4perl.appender.errorlog.Threshold = INFO
log4perl.appender.errorlog.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.errorlog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n
log4perl.appender.consolelog=Log::Log4perl::Appender::Screen
log4perl.appender.Screen.stderr = 0
log4perl.appender.consolelog.mode=append
log4perl.appender.consolelog.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.consolelog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n
log4perl.appender.debuglog=Log::Log4perl::Appender::File
log4perl.appender.debuglog.filename=c:/perl/debug-dir/debug.log
log4perl.appender.debuglog.mode=append
log4perl.appender.debuglog.Threshold = DEBUG
log4perl.appender.debuglog.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.debuglog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n
Above configuration redirects log messages to
errorlog - All log messages with severity >= INFO are logged in
c:/perl/log-dir/error.log
consolelog - All
log messages with severity >= TRACE are outputted to screen
debuglog -
All log messages with severity >= DEBUG are logged in c:/perl/debug-dir/debug.log
If you need to redirect one and only one log level to certain log file the configuration will look like below.
log4perl.rootLogger=TRACE, errorlog
log4perl.filter.ERRORFILTER = Log::Log4perl::Filter::LevelMatch
log4perl.filter.ERRORFILTER.LevelToMatch = ERROR
log4perl.filter.ERRORFILTER.AcceptOnMatch = true
log4perl.appender.errorlog=Log::Log4perl::Appender::File
log4perl.appender.errorlog.filename=c:/perl/log-dir/error.log
log4perl.appender.errorlog.mode=append
log4perl.appender.auditlog.Filter = ERRORFILTER
log4perl.appender.errorlog.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.errorlog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n
Above configuration uses filters to match the mentioned Level. In above case only ERROR logs will be logged.
Comments
Post a Comment