Util::Logger - write messages to screen or file
use Util::Logger;
# simple example using default options
my $log = Util::Logger->new();
$log->print('INFO',
'You will see this as well as WARN, ERROR and FATAL messages');
# letīs play with the log levels...
my $log_less = Util::Logger->new({ loglevel => 'WARN' });
$log_less->print('INFO', 'This message wonīt be logged.');
$log_less->print('WARN', 'This message will be logged.');
$log_less->print('ERROR', 'This message will be logged.');
# write log file and disable line wrapping that is mainly for screen display
my $log_to_file = Util::Logger->new({ loglevel => 'ERROR',
file => '/tmp/stuff.log',
wrap => 0 });
This class allows you to print messages to the screen or into a file. With each
call to the print method you will (besides the actual message) pass a
category name. The categories have a certain order of severity and by adjusting
the log level you can easily control the verbosity of your program without
touching your code.
The log level categories in descending severity:
FATAL ERROR WARN INFO MORE DEBUG
All methods of this class (except of the constructor new) are meant to be
called on an instance only (see SYNOPSIS). Do NOT call them as class methods.
Perl gives you the freedom to do so, but donīt blame me for the result; you have
been warned.
$instance-{ERR}>.
'INFO'.
Only messages of this category or a more severe one will be printed.
undef.
If the given filename exists and is writable, messages will be appended.
Otherwise a new file is created at the given location. If that fails, messages
are printed to the screen.
80.
Defines the number of columns that can be printed into a line before a newline
character is inserted; this is useful when writing to the screen.
If set to 0 then lines wonīt be wrapped at all, which is recommended for logging
to a file.
1.
Toggles printing of the category name in front of the actual message.
0.
Toggles printing of a timestamp in front of the actual message, which is
recommended for logging to a file.
' | '.
Sets the string that is used to seperate a log entyīs components (timestamp,
category, message text) from each other.
message if its category is high enough for the current loglevel.
If not, the message will be discarded.
If the given category is not valid, a warning will be issued and the current
loglevel will be used instead.
Newline characters in message are not allowed and will be replaced by tabs.
Returns undef if an error occured, true otherwise.
- use existing namespace, e.g. Log::Dual Log::Levels
- add convenience methods like info($text) as shortcut for print('INFO', $text)
- add setLevel() for readjustment of logging level
- allow logging to screen _and_ file simultaneously
- dto. and allow different settings, loglevels etc.
Joachim Jautz
http://www.jay-jay.net/contact.html
Copyright (c) 2004 Joachim Jautz. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License, distributed with Perl.