Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Catching perl warnings and errors

by hotshot (Prior)
on May 26, 2003 at 07:55 UTC ( [id://260792]=perlquestion: print w/replies, xml ) Need Help??

hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellows !

How can I catch Perl's errors and warning (e.g.: "Use of uninitialized value in ....", etc.), so they won't be thrown to the console? I want to send them to syslog (Linux).

Thanks

Hotshot

Replies are listed 'Best First'.
Re: Catching perl warnings and errors
by CukiMnstr (Deacon) on May 26, 2003 at 08:03 UTC
    Errors and warnings (in UNIX, I don't know how this works in Windows) are printed to STDERR (standard error). STDERR is attached to the console by default, so if you want to print errors/warnings somewhere else, you just have to redirect STDERR (filehandle 2):
    $ perl -Mstrict -e '$test = "foo"'
    will print
    Global symbol "$test" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.
    to the console, but
    $ perl -Mstrict -e '$test = "foo"' 2> error.log
    will print the error to file ./error.log. Check your shell's manpage for details on redirecting STDERR.

    hope this helps,

    Update: as for using syslog to log the errors, I don't know if you can do this, since as far as I can remember, you use a specific system call to do that: syslog(3).

    Update #2: a quick question in #debian @ freenode.net and I have something else: check the logger command, which is a "shell command interface to the syslog(3) system log module" (from the manpage).

      I use perl module Sys::Syslog for sending any messages to syslog, it work great. I only want to catch perl errors (from within my scripts not by redirecting it from the command line)and send them to syslog the same way.

      Hotshot
        oh. I understood something else from your first post...

        There are many ways to catch exceptions. You could use eval(), but using eval() will not work for warnings from the compiler, from the POD:

        Beware that using "eval" neither silences perl from printing warnings +to STDERR, nor does it stuff the text of warning messages into $@.
        Or you can use Error as nite_man suggests, but I just made a couple of tests and read the docs and compiler warnings don't seem to be handled by it.

        hope this helps,

Re: Catching perl warnings and errors
by nite_man (Deacon) on May 26, 2003 at 09:33 UTC
    In my mind, development some function for catching error and warnings is better way to have full control at your application. I mean to use try and catch. Unfortunatelly, Perl doesn't consist those function but you can implement them yourself or use existing modules from CPAN, for example Error:
    use Error qw(:try); try { some code; code that might thrown an exception; more code; return; } catch Error with { my $ex = shift; # Get hold of the exception object handle the exception; } finally { cleanup code; }; # <-- Remember the semicolon
    Look at Object Oriented Exception Handling in Perl for more clear understanding of how to catch exception and error in Perl.

    Updated:
    For insert message into Apache log you can use module Apache::Log or Sys::Syslog, Net::Syslog for console applications.

          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: Catching perl warnings and errors
by adrianh (Chancellor) on May 26, 2003 at 17:06 UTC

    Take a look at the __WARN__ and __DIE__ signal handlers in perlvar for one way to catch die & warn messages.

    However, be careful of the die handler since it can cause problems in shared environments like mod_perl. If used incorrectly it can also foul up people using die for exception handling. Read the docs in perlvar and on die carefully.

    It may be simpler to override the CORE::GLOBAL::die() routine and substitute your own that does the appropriate logging calls.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found