Re: Can Perl redirect STDOUT to file AND to command window?
- From: usenet@xxxxxxxxxxxxxxx
- Date: 29 Nov 2005 14:31:14 -0800
usenet@xxxxxxxxxxxxxxx wrote:
> Or, if you want to get REALLY cool, use Log::Dispatch and attach
> handlers for file and screen...
Here's an example of how to do this. Log::Dispatch is really a great
way to handle process messages (and you can attach other methods if you
wish - it can send you e-mail, or log to XML files or to databases)...
#!/usr/bin/perl
use strict; use warnings;
use Log::Dispatch::Screen;
use Log::Dispatch::File;
my $log = Log::Dispatch -> new; #This is the logger
#Logging method for terminal output. Fires at 'debug' or higher
$log ->add( Log::Dispatch::Screen ->new(
name => 'screen',
min_level => 'debug',
stderr => 0, )
);
#Logging method for file output. Fires at 'info' or higher
$log ->add( Log::Dispatch::File ->new(
name => 'file',
min_level => 'info',
filename => '/tmp/whatever.log',
mode => 'append' )
);
### Now the logging methods are set up. We can invoke the logger
### by passing it a message at a particular urgency level, namely:
### debug info notice warning error critical alert emergency
### The logger will fire ANY and ALL methods which are defined as
### equal to or less than the urgency level we stipulate.
$log -> debug("This message goes to the screen only\n");
$log -> info("This message goes to BOTH screen and file.\n");
$log -> log_to(name => 'file',
level => 'info',
message => "Force message to go to file ONLY\n");
__END__
# Never use 'print()' again!
.
- Follow-Ups:
- References:
- Prev by Date: Re: Making sure a string is valid money number
- 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):