Log - provide an easy way to do multiple level, object oriented logging.
This module provides a simple way to embed log statemets into an application that incorparate several distinct levels of logging, so that debugging can easily be accomplished by simply making a configuration change.
A quick example:
#!/usr/bin/perl
use Log;
my $log = new Log (
file => 'some/file.log',
level => 'note', ## Will log this level and "above"
console => '0', ## Print all log messages to STDERR if true.
)
##Log messages:
$log->debug "some debug message"; ## Not printed.
$log->error "that's really bad!"; ## Printed
This module provides a (hopefully) complete way to handle logging in a moderately complex application. It is designed to have several levels of logging so that log statements can be sprikled throughout any application, or possibly across several modules. A desired level of logging can then be set so that logging can be increased or decreased without having to modify the code.
my $log = new Log( @parameters );
Valid parameters:
file => filename The file used for logging. If it doesn't exist, it will be created.
level => string The level at wich we are to begin logging. Often used as a command line parameter
levels => qw/list/ An ordered list of values to use in conjuction with the 'level' parameter.
console => [1/0] Log to STDOUT if true.
buffer_size => int How many log entries to keep in memory. Useful to introspect on what has already happened.
delimiters => [1/0] Use '### BEGIN LOG ###' messages if true.
isLogged('level')Expects a scalar containing a valid logging level. Returns 1 if that level of logging is ``on''.
setReport('level')Expects a scalar contatining a valid logging level. Sets the current level to the new one and returns the old level.
getBuffer()If called in a list context, it will return a list with the entire contents of the buffer and clear the buffer.
If called in a scalar context, it will return a scalar containing the oldest entry in the buffer.
Be sure that this object is created with a large enough buffer size if you intend to use this feature, since the size of the buffer is checked with each write to the log. If the buffer would be too big, the oldest entry is removed to make room for the new one.
error('message')
warning('message')
note('message')
debug('message')
trace('message')
dump('message')Call one of the above (or your own list defined in the 'levels' parameter) to write a message with the appropriate severity to the defined logging facility.
This is really a call to AUTOLOAD and checks to see what it should do based upon it's name. (If you don't know about this feature of perl, don't worry about it. Or, read the camel book.)
Returns the message that was logged for easy stringing.
First, look at the example above.
But my favorite way to use this is to initalize the important parameters from the command line.
#!/bin/perl
use Getopt::Std;
use Log;
getopts('cl:'); ## -c is boolean -l requires a level
my $log = new Log(
level => $opt_l,
console => $opt_c,
file => 'some_file.log',
);
$log->note( "Starting my program!" );
open FILE, data.file or die $log->error( "Couldn't open file: $!" );
$log->debug( "About to do some stuff");
## Do stuff;
$log->info( "Did some stuff" );
Tom Whipple <mail@tomwhipple.com>