|
From: Mike S. <msc...@ao...> - 2003-10-30 04:40:15
|
Simon Taylor wrote on 10/29/2003, 2:48 PM:
> The logger resets the filename so I should get a new log for each job.
I'm assuming by "job" you mean a call within the same (Unix) process --
correct?
> The logger is then saved as part of the object
Actually, loggers should *never* be saved in objects or elsewhere. You
can always obtain them cheaply by calling get_logger("category"). This
way, you're safe to change the configuration on the fly without ending
up with stale loggers.
> Log4PerlInit - is called from Config.
> SelfLog is used when i want to get the Config module to log to the
> same file as the module that called Log4PerlInit
Not quite sure why you have two different functions -- there's a lot of
code duplication between the two. Hopefully, you'll like the approach
outlined further down.
> Whats the best way to undef all the loggers at each job change. Or put
> another way when running a long job how do I safely get log4perl to
> switch to a new log appender and stop using the old one
I won't suggest to call Log4perl's init() on every job, although that
would probably work for your scenario.
Unfortunately, our current file appender doesn't have a "switch_file"
method. So, for now, you could create a new file appender class, inherit
from L4p::Appender::File and define a switch_file method, blatently
making use of L4p::Appender::File's private data:
##################################################
package ReopeningFileAppender;
##################################################
use warnings;
use strict;
use base qw(Log::Log4perl::Appender::File);
##################################################
sub switch_file {
##################################################
my($self, $newname) = @_;
undef $self->{fh};
$self->{filename} = $newname;
my $arrows = ">";
if($self->{mode} eq "append") {
$arrows = ">>";
}
my $fh = do { local *FH; *FH; };
open $fh, "$arrows$self->{filename}" or
die "Can't open $self->{filename} ($@)";
$self->{fh} = $fh;
}
1;
__END__
If you put that code into ReopeningFileAppender.pm and define a
configuration file l4p.conf like
# l4p.conf
log4perl.logger = DEBUG, APP1
log4perl.appender.APP1=ReopeningFileAppender
log4perl.appender.APP1.layout = Log::Log4perl::Layout::SimpleLayout
log4perl.appender.APP1.filename = test.log
then your code is free to do something like
Log::Log4perl->init("l4p.conf");
DEBUG("logged to test.log");
# Get appender and switch file on it
my $app = Log::Log4perl::appenders()->{APP1};
$app->switch_file("foobar.log");
DEBUG("logged to foobar.log");
which first logs to test.log, then obtains the appender and causes it to
flush its buffer to the old file and open a new one.
Again, not pretty, because it assumes L4p::Appender::File isn't changing
its private 'fh' handle, but for the next version, I might add a
switch_file() method to our file appender.
-
-- Mike
Mike Schilli
m...@pe...
|