Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Under mod_perl when working with external modules and most templating/frameworks I have found code similar to this helpful:
# OO method for error to STDERR uisng 'warn' # which under mod_perl/Apache is your # default error_log sub error_to_log { my ($self,$error,$override) = @_; if ($self->_debug() || $override) { warn "$error\n"; } }
So if your _debug method is toggled on this comment would get logged:
$object->error_to_log("I made it this far, I can't believe it");

If you don't "advertise"* the _debug method or you are doing a minor debug you could simply call it with:
$object->error_to_log("Date: $date Variable: $var",1);
The '1' simply forces the error message to be written.

There are many more robust solutions available, but I have found this method to be very useful and allows me control over the error logging process. Since it is abstracted(?) enough I can make changes to the logging method without recoding other sections of the application.

So you don't want OO?
# $debug is a global variable # you will need to assign sub error_to_log { my ($error,$override) = @_; if ($debug || $override) { warn "$error\n"; } }

Then you call it like any other sub:
&error_to_log("Error message");

* In Perl it is not as common to make a method completely private, as it may be in languages like Java. In most cases they are still acessible if you know they exist, that is why I prefer the "advertise" verbiage. Ways to make a method private can be found in other nodes, but I didn't bother to look them up because this node is about logging not private vs. public methods.

In reply to Re: Defensive Programming and Audit Trails by trs80
in thread Defensive Programming and Audit Trails by FoxtrotUniform

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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: (7)
    As of 2014-10-24 09:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (131 votes), past polls