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

In order to avoid the tiresome:
#!/usr/bin/env perl use strict; use warnings; my $debug = 1; # ... tralalalalala warn $val if $debug ; # this we want to end!
I would like to know what module you like to use when tracing/debugging CGI scripts and/or command-line programs. Perhaps one where you can set and control debug levels via environmental variables or configuration files.... as I write this Log::Log4perl magically sprung into my consciousness. If I don't hear anything supportive of something greater, then that shall be my choice. Be well all, - pawn.

Replies are listed 'Best First'.
Re: What module do you like for debugging CGI scripts
by jettero (Monsignor) on Jan 27, 2007 at 12:28 UTC
    I usually use CGI::Carp qw(fatalsToBrowser) to trap errors so I can see them. I use sudo tail -f /var/log/http-errors.log to watch for my warnings. If there's something better, I'd like to know about it also.

    -Paul

      I took a look at CGI::Carp and it does not support the ability to specify the level of log msg that you want output. I also don't see a way to switch off logging altogether when in a production environment.

        You don't want to turn off the logging altogether in a production environment, merely not to send the output to the browser. CGI::Carp will, by default, send the output to stderr, hence to the server log, which you do want in a production environment.

        The following snippet should help:

        BEGIN { my $carp_arg; if ($ENV{ENV} =~ /DEV/) { # or some other test for a non producti +on env $carp_arg = 'fatalsToBrowser'; } use CGI::Carp $carp_arg; }

        --

        Oh Lord, won’t you burn me a Knoppix CD ?
        My friends all rate Windows, I must disagree.
        Your powers of persuasion will set them all free,
        So oh Lord, won’t you burn me a Knoppix CD ?
        (Missquoting Janis Joplin)

        Surely there are no bugs in the code you're running on your production environment ;)

        Also, if you have  if $DEBUG attached to the end of all your calls to warn then they'll just disappear when you set $DEBUG=undef

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
        Just don't load the module in production, though you may want to adjust your web server (apache) to show better pages on 500 code errors.

        just another cpan module author
Re: What module do you like for debugging CGI scripts
by merlyn (Sage) on Jan 27, 2007 at 13:46 UTC
Re: What module do you like for debugging CGI scripts - Log::Dispatch
by imp (Priest) on Jan 27, 2007 at 15:28 UTC
    My preferred approach is to only log fairly important events by default, but to record the entire debug trail in a database when an error occurs. In production environments this approach avoids the usual problem of having either too much log output (filling the log file and wasting disk IO), or too little (unable to diagnose errors). It is also helpful to log the contents of %ENV and sometimes a dump of the current CGI params, but be careful not to record credit card numbers in a log file.

    The best module for this that I have found is Log::Dispatch with Log::Dispatch::Buffer, but I'm sure you could do something similar with Log::Log4perl. You should always log things of notice level or higher to syslog in my opinion.

    I designed this sytem for something that used CGI::Application and added a teardown task that handled the check for highest severity.

    If your reason for asking for a logging system recommendation is that you are currently trying to track down a bug, then I very strongly recommend you break some of your supporting code into supporting modules and write a reasonable test suite for it. Once you start writing tests you'll never go back :)

Re: What module do you like for debugging CGI scripts
by SFLEX (Chaplain) on Jan 27, 2007 at 13:28 UTC
    I have found this to be a realy good error log for Perl.
    use strict; use warnings; # Log Perl Warnings and Errors - Standard # may have to make the blank file "error.log". use CGI::Carp qw(carpout); open(LOG, '>>', './error.log') or die "Unable to append to at $!\n"; carpout(*LOG);

    This should log all warnings errors.
    Works the best when use strict and use warnings are used.
Re: What module do you like for debugging CGI scripts
by talexb (Chancellor) on Jan 28, 2007 at 01:01 UTC

    Someone's already mentioned Log::Log4perl, but let me just add another vote. I use it in Production code, and if Something Goes Bad, instead of hacking code on a Production server (never a good idea), I can just turn up the debug level from WARN to DEBUG and watch a few web requests go by in the log file.

    There are also lots of options .. for example, instead of sending messages to a log file, they can be E-Mailed, sent out a serial port for external logging, etc., etc.

    Highly recommended.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: What module do you like for debugging CGI scripts
by logie17 (Friar) on Jan 27, 2007 at 18:47 UTC
    One thing that I have found extremely useful is the use of Caller in conjuction with an error log. It's great for trying to generate a stack trace and trying to debug any code which is very large.

    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
Re: What module do you like for debugging CGI scripts
by jimX11 (Friar) on Jan 28, 2007 at 02:52 UTC
Re: What module do you like for debugging CGI scripts
by EvanK (Chaplain) on Jan 28, 2007 at 08:26 UTC
    personally, I use a combination of CGI::Carp and Data::Dumper. its quite handy to not have to tail the (usually) giant apache error logs every time i test a new script. as with any method that I prefer, your mileage may vary :)

    __________
    The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
    - Terry Pratchett

Re: What module do you like for debugging CGI scripts
by hesco (Deacon) on Jan 28, 2007 at 08:31 UTC
    I used Log::Log4perl on a project and really liked it, but lost half a day trying to make it work once we moved the application into production. We never got it working on the production server. But then I learned how to bury my logger calls in comments which would be ressurrected on a machine which has Log4perl working, but will be treated as comments on machines where it does not work. And I had to write a conditional around the code which defined the logger files, choosing a log in my home directory if my user is running tests from the command line, but an apache writable log when the script runs from a browser call. I'd like to learn more about Log::Dispatch, though. Thanks for the heads up.

    -- Hugh

    if( $lal && $lol ) { $life++; }
Re: What module do you like for debugging CGI scripts
by uwevoelker (Pilgrim) on Jan 28, 2007 at 12:43 UTC
    I use Devel::ptkdb (Tk debugger). It works for CGI, command line and even Catalyst applications. In case of Catalyst you have to load some modules by hand (e. g. the controller) with
    perl -d:ptkdb -MMyApp::Controller::Root script/myapp_server_debug.pl