An excellent question. I like logging for different reasons, depending on whether I'm the developer or the user.

First wearing my User Hat, logging is a Good Thing because when the application goes wrong - and *all* software goes wrong at some point - it means that when I whine at the developer I can accompany my whine with information he will find useful for hunting down and fixing the bug.

And wearing my Developer Hat, given that my users are going to whine at me - and *all* users whine at some point - I want them to be able to whine intelligently. Not to mention that logging is very handy indeed for debugging the rubbish I write before I release it on an unsuspecting world.

Given those reasons *why* to use it, answering the *when* becomes easy - you should log *always*. Make your application log by default, allowing the user to turn logging off if they really care.

The question you don't ask is *what* to log. I like to log all significant events, with a timestamp, and with some indicator of how important the event is - is it an informational notice? a warning? a failure? debugging info? Release your app with debugging turned off, but all other log levels turned on, and as well as allowing the user to turn all logging on or off, it's nice to let them turn the log level up and down.

An example of a notice would be "constructor method called with parameters FOO BAR BAZ". A warning might be "constructor called with invalid value FOO for parameter BAR". A failure might be "failed to serialise object to disk". Debugging info would be hideously verbose, like for each subroutine, logging entry parameters and return values. Log files should be formatted both to be easily searchable (find all warnings in the Foo module) and easy to read. Whitespace and indentation is Good.


In reply to Re: When to use logging and why? by DrHyde
in thread When to use logging and why? by Anonymous Monk

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.