in reply to Re: Rambling about debuggers
in thread Rambling about debuggers

Mine tend to end up looking like this:

print STDERR "Debug message\n" if $debug > 0;

Where the number may change, giving me several levels of debug statements, and $debug gets set by a command-line accumulating switch. I also tend to leave these in production code: Removing a debug statement is a change in code, and any change in code makes it possible to add a bug, after all.

Replies are listed 'Best First'.
Re^3: Rambling about debuggers
by chrestomanci (Priest) on Jul 27, 2011 at 09:48 UTC

    You can achieve a similar result, but with more flexibility by using Log::Log4perl. In most of my scripts I usually write something like:

    use Log::Log4perl qw(:easy); use Log::Log4perl::Level; Log::Log4perl->easy_init( {'level'=>$DEBUG, 'layout'=>'%m%n'} ); sub doSomething { my $logger = Log::Log4perl->get_logger(); $logger->debug('some message'); }

    As written above, all the logging messages will go to your console, which is the right thing for small scripts that you are actively debugging. Later on you can reduce the log level to $WARN, so that normal debug statements are suppressed, but warnings and errors still appear.

    If your script evolves into something larger or mission critical, then you can take advantage of the expressive grammar available in Log4perl configuration files to have different log levels in different functions, to send logs to files, databases pipes etc, to send you emails on fatal errors, and to change the log level used by a running script, so if it miss behaves in production you can investigate without downtime.