Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Choosing logging module

by dgaramond2 (Monk)
on May 25, 2007 at 02:17 UTC ( [id://617382]=perlquestion: print w/replies, xml ) Need Help??

dgaramond2 has asked for the wisdom of the Perl Monks concerning the following question:

I've used Log::Log4perl for quite a while. It turns out that most of the time the :easy interface is all I'm using. Also, "multiple categories" is something I've wanted and haven't exactly found. Maybe it's time to try something else.

Here's how I prefer to use the logging module: Pick a level using from one of the log statements (debug()/info()/warn()/error()/fatal()), give it zero or more "tags" (or "categories", or "keywords"), followed by the message. That's it. I don't want to have to explicitly create in every package a logger object with a certain category, or do any other unnecessary setup.

Later, in a log file or the configuration section, I can configure which levels, which set of tags, and which package/subroutine pattern, should go to which output (stdout/stderr, file, etc).

I want maximum level of flexibility in filtering log messages in output. Sometimes I want to stay at level warn() globally, but turn on debugging for only one or two subroutines which I'm interested in. Or, just shows debug() messages in a package which include the tag "dump", to let them dump data structures. Or maybe, in an SMTP server code, sometimes I want to temporarily turn on debugging for just a client IP/IP range, in Sundays, and then log all the DATA being sent by the client _if_ said mail body is larger than 20k and contain certain regex pattern. All this without modifying the source code, of course.

Any recommendations?

Replies are listed 'Best First'.
Re: Choosing logging module Log::Log4perl with subcategories
by imp (Priest) on May 25, 2007 at 03:55 UTC
    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('parser.events.text'); my $logstart = Log::Log4perl->get_logger('parser.events.start'); my $logend = Log::Log4perl->get_logger('parser.events.end'); # 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 'parser.events'.

    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.
Re: Choosing logging module
by blazar (Canon) on May 25, 2007 at 10:50 UTC

    I don't know if this may help you, but Log::Log4perl itself is based on Log::Dispatch. Since your requirements are relatively simple, you may want to build your own logging system on the latter.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://617382]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-26 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found