Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Choosing logging module Log::Log4perl with subcategories

by imp (Priest)
on May 25, 2007 at 03:55 UTC ( #617396=note: print w/replies, xml ) Need Help??

in reply to Choosing logging module

Using the easy interface is a good way to start with Log::Log4perl, but to get much out of it you need to put a little more work into customizing it.

The approach I like is to use the class name as the generic logger, and to create subcategories for the certain types of logs in that class. A recent module I wrote used HTML::Parser for event based parsing, and I wanted to select which events were logged. Here is a very stripped down snippet that shows the logging definitions:

package SomeParser; use strict; use warnings; use Log::Log4perl; my $logger = Log::Log4perl->get_logger('parser'); my $logexport = Log::Log4perl->get_logger('parser.export'); my $logtext = Log::Log4perl->get_logger(''); my $logstart = Log::Log4perl->get_logger(''); my $logend = Log::Log4perl->get_logger(''); # stuff sub tag_opened { my ($self, $parser, $tag, $attr, $text) = @_; $logstart->is_debug && $logstart->debug(sprintf "<%s>", $tag); } sub tag_closed { my ($self, $parser, $tag) = @_; $logend->is_debug && $logend->debug(sprintf "</%s>", $tag); } 1;
I follow similar approaches throughout the code so I can selectively enable categories such as ''.

Another area that I find useful is to have a base class for my test cases (or a library to load), and have the following snippet in it:

if ($ENV{TEST_VERBOSE}) { Log::Log4perl->easy_init({ level => $DEBUG, layout => '#%5p %F(%L) + - %m%n'}); } else { Log::Log4perl->easy_init({ level => $WARN, layout => '#%5p %F(%L) +- %m%n'}); }
This will set the log level to debug when the tests are run in verbose mode (e.g. prove -v t/*.t), otherwise it will be set to warn. In both cases it will have a # before the log message to fit in with the TAP output.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://617396]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-01-16 13:08 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (150 votes). Check out past polls.