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.

In reply to Re: Choosing logging module Log::Log4perl with subcategories by imp
in thread Choosing logging module by dgaramond2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.