To a system of about a hundred perl scripts (that get executed by thousands of people a day, across the company), I'd like to add some basic logging (well, more like monitoring). I'd like to track things like the name of the script, who ran it, its execution time, and the nature of any unhandled exceptions that it threw, every time a script runs.

Now, I already have a repository for the data, and a way to get it there (we have a departmental tool with an XML interface for receiving the data; it also has a web UI, for reporting against collected data). I also have the collection details worked out, in the form of a script template into which I could wrap the contents of each script. It's the obvious approach, and looks like this:

#!/usr/bin/perl -w use strict; use Monitor; eval { Monitor::register($0, @ARGV); ### EXISTING CODE GOES HERE }; if ($@) { my $exc = $@; Monitor::exception($exc); die $exc }

Inside the Monitor module, a BEGIN block would track the start time, and an END block would track the elapsed time and handle the task of posting all of the data to the monitor application. Pretty straightforward stuff. What I don't like, though, is that this template code has to go into every source file... This amount of copy/paste just seems wrong (and lacking in elegance).

A colleague suggested that we should shoot for a way to get it down to a single-line addition to every file: the inclusion of a use Monitor line. He reasoned that we should be able to come up with a way to trap unhandled exceptions, without actually putting the eval everywhere. Basically, the Monitor module would do something to set things up, and then reach back into main:: (e.g.), from its END block, to figure out whether any unhandled exceptions occurred.

Alas, despite playing with this for a quite some time, we couldn't get it worked out. For one thing, $@ only applies after an eval. For another, there doesn't seem to be a foolproof way to even get to $@, given that other modules could have END blocks that fire first, each of which could corrupt it.

So, I come to the monks for guidance...


In reply to logging, to include unhandled exceptions by klassa

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.