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

Hi Monks
I have a module that will create the text file passed by the name like in the code here, my question is how can I redirect stderr to my sub routine as well. I need to log any errors this way, any help?
&app_error("log.txt","stderr");

Replies are listed 'Best First'.
Re: Logging Errors
by leocharre (Priest) on Jan 06, 2006 at 18:08 UTC

    This is pretty weird. Why would you log errors this way?

    There is already a lot of very good work into logging things with CGI::Carp

    I don't know if you are master hacker and you've got some spooky stuff going on. But if you're not- and you don't- Then I would suggest to use CGI:Carp and stay clear of re-inventing the wheel.

    BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/var/log/yourlogfile") + or die("Unable to open yourlogfile: $!\n"); carpout(LOG);}

    Yes, I know at first hand it doesn't seem to replace anything. But, you could always record to a log, and run a script on cron that greps out sections to flat files- whatever.

    If you must though... CPAN has IO::Capture::Stderr - Capture all output sent to STDERR. No perl minion should feel safe even going to take a sh&t before I searching CPAN for the right toilet paper to wipe your a55. And it's free. Like the one you steel from work.

    Hope this helps.. ?