Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

My proudest moment

by pileofrogs (Priest)
on Oct 29, 2007 at 16:02 UTC ( [id://647874]=perlmeditation: print w/replies, xml ) Need Help??

Hark ye, monks, particuarly ye sysadmin type monks.

I recently had a perl epiphany that I wish to shout from the mountain tops. It is simple and it is powerful. If you are a sysadmin, as I am, this is a wonderful thing.

$SIG{__DIE__} = sub { syslog("err", shift) };

This puts the "die" message into syslog. No more do you have to pepper your script with syslog calls next to each die, only to have your script actually die for some reason you either forgot to syslog or didn't manually die from.

Rejoice!

--Pileofrogs

Update:

Yes, use openlog. I just left it out to keep my example super short. Besides, the point is using $SIG{__DIE__} to call your logging routines, whatever they are.

Replies are listed 'Best First'.
Re: My proudest moment
by ikegami (Patriarch) on Oct 29, 2007 at 16:24 UTC

    That would syslog caught exceptions. See $^S.

    if (!eval { ...; 1 }) { ... }

    That wouldn't catch compile errors (although that's easily fixed using BEGIN).

    use strict; $SIG{__DIE__} = sub { syslog("err", shift) }; for my $counter (1..10) { print("$conter\n"); }
Re: My proudest moment
by grinder (Bishop) on Oct 29, 2007 at 17:20 UTC

    Second rule of Sys::Syslog: do NOT use syslog before calling openlog.

    (Damn. Can't find Sébastien's notes from YAPC::Europe... link, anyone?)

    • another intruder with the mooring in the heart of the Perl

      Well, "do NOT use syslog before calling openlog" is quite different from "You should use openlog() before calling syslog()", at least to me :-)

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: My proudest moment (...::Syslog::Carp)
by tye (Sage) on Oct 29, 2007 at 19:45 UTC

    Given some of the replies, it sounds like this would be worth putting into a module: Foo::Syslog::Carp where "Foo" is replaced by the proper top-level module category, probably one of qw( Logger Net Sys Unix Log ), based on what searching for syslog finds.

    - tye        

Re: My proudest moment
by tfrayner (Curate) on Oct 30, 2007 at 18:08 UTC
    I have for a couple of years now enhanced my local reputation for omniscience by using a $SIG{__DIE__} handler that sends me an email every time some part of my code dies (using MIME::Lite). I'd definitely recommend the approach for long-running, unsupervised, but production-critical daemon-style applications.

    Tim

Re: My proudest moment
by Dominus (Parson) on Nov 05, 2007 at 18:40 UTC
    Something similar I often do in CGI programs and other utilities that are not attached to the terminal:

    BEGIN { open STDERR, ">", "/tmp/$0-$$.log"; }
    and then all die and warn messages go to the log file in /tmp.

    To get this to work with Sys::Syslog you would need a little more machinery:

    BEGIN { sub SLH::PRINT { syslog("err", $_[1]) } sub SLH::TIEHANDLE { return bless ["dummy object"] => "SLH" } tie *STDERR => "SLH"; }
    And then everything written to STDERR gets passed along to syslog.

    Disclaimer 1: I didn't actually try this.

    Disclaimer 2: I never, ever use software written by Eric Allman.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://647874]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found