From: <log...@pe...> - 2003-03-18 05:52:36
|
Welcome to the Log::Log4perl recipe of the week. Today: ============================================================ Log::Log4perl Recipe of the Week (#20): I want to suppress certain messages based on their content! ============================================================ Let's assume you've plastered all your functions with Log4perl statements like sub some_func { INFO("Begin of function"); # ... Stuff happens here ... INFO("End of function"); } to issue two log messages, one at the beginning and one at the end of each function. Now you want to suppress the message at the beginning and only keep the one at the end, what can you do? You can't use the category mechanism, because both messages are issued from the same package. Log::Log4perl's custom filters (0.30 or better) provide an interface for the Log4perl user to step in right before a message gets logged and decide if it should be written out or suppressed, based on the message content or other parameters: use Log::Log4perl qw(:easy); Log::Log4perl::init( \ <<'EOT' ); log4perl.logger = INFO, A1 log4perl.appender.A1 = Log::Dispatch::Screen log4perl.appender.A1.layout = \ Log::Log4perl::Layout::PatternLayout log4perl.appender.A1.layout.ConversionPattern = %m%n log4perl.filter.M1 = Log::Log4perl::Filter::StringMatch log4perl.filter.M1.StringToMatch = Begin log4perl.filter.M1.AcceptOnMatch = false log4perl.appender.A1.Filter = M1 EOT The last four statements in the configuration above are defining a custom filter "M1" of type "Log::Log4perl::Filter::StringMatch", which comes with Log4perl right out of the box and allows you to define a text pattern to match (as a perl regular expression) and a flag "AcceptOnMatch" indicating if a match is supposed to suppress the message or let it pass through. The last line then assigns this filter to the "A1" appender, which will call it every time it receives a message to be logged and throw all messages out *not* matching the regular expression "Begin". Instead of using the standard "Log::Log4perl::Filter::StringMatch" filter, you can define your own, simply using a perl subroutine: log4perl.filter.ExcludeBegin = sub { !/Begin/ } log4perl.appender.A1.Filter = ExcludeBegin For details on custom filters, check Log::Log4perl::Filter. Have fun! Until next week. -- Mike ################################### # Mike Schilli # # log...@pe... # # http://perlmeister.com # # http://log4perl.sourceforge.net # ################################### |