Re: Can Perl redirect STDOUT to file AND to command window?
- From: usenet@xxxxxxxxxxxxxxx
- Date: 29 Nov 2005 14:43:25 -0800
use...@xxxxxxxxxxxxxxx wrote:
> Here's an example of how to do this. Log::Dispatch is really a great
> way to handle process messages...
But, wait, there's more! If you use the Log::Dispatch 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::Dispatch->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::Dispatch::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!
.
- References:
- Prev by Date: Re: cASE sWITCHING?
- Next by Date: Re: string and hash [quite long]
- Previous by thread: Re: Can Perl redirect STDOUT to file AND to command window?
- Next by thread: Re: Can Perl redirect STDOUT to file AND to command window?
- Index(es):