Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Should there be this much overhead? When I see all this repeated code, I don't like it. This adds 4 lines to each program (not a big deal) but it also adds 4 lines to each .t script (?!?!) and 2 lines to module...surely it shouldn't require this much repeated code?
Log::Log4perl can be used in different ways, just pick the one that suits you:

For simple scripts, people like using :easy mode. This approach is also discussed in this FAQ and uses

  • a simplified init method without a configuration file and
  • logging functions instead of objects/method calls
Here's an example:
use Log::Log4perl qw(:easy); Log::Log4perl->easy_init( { level => $DEBUG, file => ">>test.log" } ); DEBUG("Debug this!");
Calling DEBUG() instead of obtaining a logger reference via get_logger($category) and then calling $logger->debug() is what Log4perl calls using stealth loggers. Stealth loggers are also recommended to be used in modules, just say
package MyPackage; use Log::Log4perl qw(:easy); sub method { DEBUG "Doing something!"; }
and you've got full-fledged logging in place. Stealth loggers use Log4perl categories equal to the current package. Performance stays the same.

The main program is responsible for calling init() or easy_init(). If it doesn't, Log::Log4perl will keep sleeping.

As for spreading like Kudzu through your application, that's a common issue with logging systems. Log::Log4perl reduces the exposure by not requiring to pass a handler around, but provides a singleton mechanism.

However, ideally, logging would be completely orthogonal to your application logic. You may have heard of aspect oriented programming (AOP), that's how it's done. Log4perl might provide a way to do that in the future.


In reply to Re: Log::Log4Perl best practices by saintmike
in thread Log::Log4Perl best practices by water

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2019-10-23 04:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?