From: <log...@pe...> - 2003-02-21 18:28:42
|
Welcome to the Log::Log4perl recipe of the week. Today: ============================================================ Log::Log4perl Recipe of the Week (#17): I want to log ERROR and WARN messages to different files! How can I do that? ============================================================ Let's assume you wanted to have each logging statement written to a different file, based on the statement's priority. Messages with priority "WARN" are supposed to go to "/tmp/app.warn", events prioritized as "ERROR" should end up in "/tmp/app.error". Now, if you define two appenders "AppWarn" and "AppError" and assign them both to the root logger, messages bubbling up from any loggers below will be logged by both appenders because of Log4perl's message propagation feature. If you limit their exposure via the appender threshold mechanism and set "AppWarn"'s threshold to "WARN" and "AppError"'s to "ERROR", you'll still get "ERROR" messages in "AppWarn", because "AppWarn"'s "WARN" setting will just filter out messages with a *lower* priority than "WARN" -- "ERROR" is higher and will be allowed to pass through. What we need for this is a Log4perl *Custom Filter*, newly available with Log::Log4perl 0.30, which, at the time of this writing, is available as a development release. Both appenders need to verify that the priority of the oncoming messages exactly *matches* the priority the appender is supposed to log messages of. To accomplish this task, let's define two custom filters, "MatchError" and "MatchWarn", which, when attached to their appenders, will limit messages passed on to them to those matching a given priority: log4perl.logger = WARN, AppWarn, AppError # Filter to match level ERROR log4perl.filter.MatchError = Log::Log4perl::Filter::LevelMatch log4perl.filter.MatchError.LevelToMatch = ERROR log4perl.filter.MatchError.AcceptOnMatch = true # Filter to match level WARN log4perl.filter.MatchWarn = Log::Log4perl::Filter::LevelMatch log4perl.filter.MatchWarn.LevelToMatch = WARN log4perl.filter.MatchWarn.AcceptOnMatch = true # Error appender log4perl.appender.AppError = Log::Dispatch::File log4perl.appender.AppError.filename = /tmp/app.err log4perl.appender.AppError.layout = SimpleLayout log4perl.appender.AppError.Filter = MatchError # Warning appender log4perl.appender.AppWarn = Log::Dispatch::File log4perl.appender.AppWarn.filename = /tmp/app.warn log4perl.appender.AppWarn.layout = SimpleLayout log4perl.appender.AppWarn.Filter = MatchWarn The appenders "AppWarn" and "AppError" defined above are logging to "/tmp/app.warn" and "/tmp/app.err" respectively and have the custom filters "MatchWarn" and "MatchError" attached. This setup will direct all WARN messages, issued anywhere in the system, to /tmp/app.warn (and ERROR messages to /tmp/app.error) -- without any overlaps. Have fun! Until next week. -- Mike ################################### # Mike Schilli # # log...@pe... # # http://perlmeister.com # # http://log4perl.sourceforge.net # ################################### |