Here's an example of how to do this. Log:
ispatch is really a great
way to handle process messages...
But, wait, there's more! If you use the Log:
ispatch module in the
next 30 minutes, you will get callback capability at NO ADDITIONAL
COST! That's right - a billion dollar value is yours FREE!
Are you tired of having to fuss with linefeeds on your output messages?
Define a callback! Do you want to timestamp each line that you write
to your logfile (but not bother to show the timestamp on STDOUT
messages? Define a callback! For example:
my $add_lf = sub { my %p = @_; "$p{'message'}\n"};
my $add_timestamp = sub { my %p = @_;
sprintf "%s - %s", scalar(localtime),
$p{'message'}; };
OK, now you've defined two callbacks (one to add linefeeds, one to add
timestamps). Assuming that you want to add linefeeds to every single
thing that you output, you would define your logger to always use the
$add_lf callback:
$log = Log:
ispatch->new ( callbacks => $add_lf );
But you only want to call the timestamp-er on the 'file' method, so you
would define that method like this:
$log ->add( Log:
ispatch::File ->new(
name => 'file',
min_level => 'info',
filename => '/tmp/whatever.log',
callbacks => $add_timestamp,
mode => 'append' )
);
Now you can log even multiple lines, such as this:
$log->debug('line 1', 'line 2', 'line 3');
And it will automatically add linefeeds to each line, and it will
timestamp each line (but only in the file, not on the terminal).
Call CPAN now - webservers are standing by to take your order!