I am using log4perl in a system which does the following:-
Iterates over a list of jobs it gets from a database Creates a CommandHandler which uses Log4Perl CommandHandler calls Config which uses Log4Perl Calls CommandIterator which uses Log4Perl
I am defining a Log4Perl logger each time I change jobs in perl code.
sub Log4PerlInit{ # Set up log4perl =head2 NAME Used to set up the Log4Perl logger Sets it up for the calling class - but also ensures that this class lo +gs to it =cut my ($self, @args) = @_; my ($logfile, $logdir); # Just use the jobref id which will be a unique sequence to log in +to $logdir=$self->{LOG_DIR}.$args[0].$self->{U}.$self->LogTime; unless ( -e $logdir && -d $logdir){ mkpath($logdir) or throw TNBCriticalException("Cannot make direc +tory $logdir"); } $logfile=$logdir.$self->{DLM}."job".$args[0].".log"; $self->{CurLogFile} = $logfile; # Jump off and define a logger for this class $self->{_logfile} = $self->SelfLog("DEBUG"); # Define a category logger for the calling class my $log = Log::Log4perl->get_logger($args[1]); # Define a layout my $layout = Log::Log4perl::Layout::PatternLayout->new("[%d][%p] % +M %m%n"); # Define a file appender my $file_appender = Log::Log4perl::Appender->new( "Log::Log4perl::Appender::File", name => "filelog", filename => "$logfile"); # Define a stdout appender my $stdout_appender = Log::Log4perl::Appender->new( "Log::Log4perl::Appender::Screen", name => "screenlog", stderr => 0); $stdout_appender->layout($layout); $file_appender->layout($layout); $log->add_appender($stdout_appender); $log->add_appender($file_appender); $log->level($args[2]); return $logdir,$log; } sub SelfLog{ # Set up log4perl for this class =head2 NAME Used to set up the Log4Perl logger =cut my ($self, @args) = @_; # Jump off and define a logger for this class # Define a category logger for the calling class my $log = Log::Log4perl->get_logger("SDU::Config"); # Define a layout my $layout = Log::Log4perl::Layout::PatternLayout->new("[%d][%p] % +M %m%n"); # Define a file appender my $file_appender = Log::Log4perl::Appender->new( "Log::Log4perl::Appender::File", name => "filelog", filename => $self->{CurLogFile}); # Define a stdout appender my $stdout_appender = Log::Log4perl::Appender->new( "Log::Log4perl::Appender::Screen", name => "screenlog", stderr => 0); # Have both appenders use the same layout (could be different) $stdout_appender->layout($layout); $file_appender->layout($layout); $log->add_appender($stdout_appender); $log->add_appender($file_appender); $log->level($INFO); return $log; }
The logger resets the filename so I should get a new log for each job. The logger is then saved as part of the object

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

Even though all my objects exist in CommandHandler and that is undefed at the end of the cycle. The Log4Perl loggers continue to point to the original logfile.

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

Edit by tye, title, preserve formatting


In reply to Log4Perl not changing to new log by set_uk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.