Perl Logging Practices

Mike Fragassi

What's on CPAN

CPAN - Domain-specific Modules


...but not the focus of this talk.

CPAN - Major Frameworks


Overview

Components of Logging Systems

Message Levels

The importance of a message.

Message Levels - Flexible

Message Levels - Granularity

System Level

Log Targets

Can be:

Also called "Output objects", "Appenders", "Sinks"

Log Dispatchers

Log Categories

TIMTOWTDI



Some of the differences you'll see in CPAN modules include:

Levels: Labels vs. Integers

Integers are confusing

    If the system is at logging level 4,
    and a specific log message is marked at level 3,
    should it be logged or not?

Which is it?

Rollercoaster or Limbo?
 

Arguments vs. Methods

Level Granularity

Matter of taste

Objects?

What to do

DIY Logging

2 Tricks to Know

  1. Using select() to redirect print() to logs:

    select(ERROR_LOG); 
    print "oops";

    print() now goes to ERROR_LOG automatically

%SIG

  1. Using signal handlers to redirect warn/die/carp to logs:

    $SIG{__WARN__} = sub { print ERROR_LOG shift ; };
    warn "oops";
    warn() now goes to ERROR_LOG automatically
    (see perldoc perlvar for %SIG)

Safe::Logs

Tie::LogFile

Log::LogLite


  
  my $log = new Log::LogLite("file.log", 3);

  $log->write("your error here", 1);

Log::StdLog

Happy June 6th, 2006!
  use Log::StdLog { level => 'warn', 
                    file => "error.log" };
 
  print {*STDLOG} error => "your message here\n";
  # that is the same as:
  print STDLOG ('error', "your message here\n");   

Others

Lots more, many with minimal features or poor documentation

Fancier Loggers

Fancier Loggers

Most Complete & Mature

Log::Dispatch

Log::Dispatch Example

  my $dispatcher = Log::Dispatch->new;

  
  $dispatcher->add( 
      Log::Dispatch::File->new(name => 'file1',
                               filename => 'error.log'
                               min_level => 'debug' ) );
  
  $dispatcher->log( level => 'info',
                    message => 'your error here');

Log::Log4perl

Log::Log4perl Features

Log::Log4perl Bubbling

Log::Log4perl Quirks

Log::Channel

Unless I'm missing something, just use Log::Dispatch

Recommendations

Mange tak!