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

Hello All,

Greetings!!

Here is a piece of code, using which,I am trying to capture the Error code in a log file on the "die" event. I need to avoid printing the error message on the Console.I havent been successful so far.

Kindly Advice.

my $db = DBI->connect("dbi:Oracle:host=shost;sid=$sid;port=$port", $ui +d, $pwd) or {capture_error()}; #===================================================================== # simple debugging routine #===================================================================== sub debug { if ($debug_output) { # print STDERR @_; # Just return if it fails to open debug file open(OUT, ">>$debug_output") || return; print OUT @_, "\n"; close(OUT); } } #===================================================================== # capture error routine #===================================================================== sub capture_error { debug("Error while connecting to database!!!\nIncorrect Connection + information\n"); exit(0); }
Ananda

Replies are listed 'Best First'.
Re: Capturing Error message in a log file.
by graff (Chancellor) on Jan 03, 2003 at 04:41 UTC
    First, have you tried using the "%attrib" part of the DBI "connect()" call? It can be used to set parameters like "PrintError" and "RaiseError", which might help to control the reporting behavior in the way that you want.

    Second, if you have access to a bash or bourne shell, you could just redirect STDERR to a file at the command line, like this:

    your_script [options and args] 2> errlog
    (There is a Windows port of bash, if you happen to be on a Windows system.)

    Finally, if neither of the above points will work for you, try this at the beginning of your script:

    open( STDERR, ">>$debug_output" ) or die "Can't write errlog: $!\n";
    That way, you don't need any special functions or special DBI connect args -- everything that Perl would normally print to STDERR will be stored in the file whose name is provided in "$debug_output". (The original STDERR handle will be closed, so no error messages will go to the console.)
      Second, if you have access to a bash or bourne shell, you could just redirect STDERR to a file at the command line, like this:
      your_script options and args 2> errlog

      Windows 2000 command shell allows this type of redirection, too. Our build system at work used it until I rewrote the whole thing in perl.

      --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Capturing Error message in a log file.
by gjb (Vicar) on Jan 03, 2003 at 04:56 UTC

    You might want to use some "professional" logging facility such as Log::Log4Perl (or one of the numerous others on CPAN. If the script has to terminate on that particular error, just log it and exit.

    Hope this helps, -gjb-

Re: Capturing Error message in a log file.
by vek (Prior) on Jan 03, 2003 at 14:37 UTC
    I haven't been successful so far.

    If the $debug_output file is not getting written to, take a look at the error message if/when the open fails:
    open(OUT, ">>$debug_output") || warn "Could not append to $debug_outpu +t - $!\n";
    Check the value of $debug_output to make sure it contains a valid filename/path. Check permissions of the file etc...

    -- vek --
Re: Capturing Error message in a log file.
by jdporter (Paladin) on Jan 03, 2003 at 11:17 UTC
    Looks like the problem is getting $debug_output to contain the error message.

    Since it looks like you're passing the desired message string as an argument to debug, then the thing to do is set $debug_output from the argument. Like so:
    sub debug { my $debug_output = shift; if ($debug_output) {
    Are you using strict? If not, then you probably should, because it would have helped you locate the source of your problem.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.