From: Chris A. <ch...@nu...> - 2005-09-09 06:07:47
|
In an application where I have multiple children each needing to log to their own file, I've noticed using the get_logger() or init to changes the configuration for all the children and the parent as well. So effectively, when multiple processes use the same instance through get_logger() they all just combine in the same file. I didn't see anything in the documentation regarding how to handle this, so I was hoping someone may be able to shed some light on what I need to do to segregate these logging rules. |
From: Mike S. <m...@pe...> - 2005-09-09 07:52:45
|
On Fri, 9 Sep 2005, Chris Anderson wrote: > In an application where I have multiple children each needing to log to > their own file, I've noticed using the get_logger() or init to changes > the configuration for all the children and the parent as well. So > effectively, when multiple processes use the same instance through > get_logger() they all just combine in the same file. I didn't see > anything in the documentation regarding how to handle this, so I was > hoping someone may be able to shed some light on what I need to do to > segregate these logging rules. It depends on where you call Log::Log4perl->init(). If it's called in each child process, then the filename can be specified dynamically (using something like the pid): http://log4perl.sourceforge.net/d/Log/Log4perl/FAQ.html#02bbc If Log::Log4perl->init() gets called in the main process, messages need to be filtered dynamically, using a custom filter: http://log4perl.sourceforge.net/d/Log/Log4perl/FAQ.html#c7fa8 http://log4perl.sourceforge.net/d/Log/Log4perl/Filter.html Let me know if you need more detailed info. -- Mike Mike Schilli m...@pe... |
From: Chris A. <ch...@nu...> - 2005-09-09 17:30:18
|
Mike Schilli wrote: >On Fri, 9 Sep 2005, Chris Anderson wrote: > > > >>In an application where I have multiple children each needing to log to >>their own file, I've noticed using the get_logger() or init to changes >>the configuration for all the children and the parent as well. So >>effectively, when multiple processes use the same instance through >>get_logger() they all just combine in the same file. I didn't see >>anything in the documentation regarding how to handle this, so I was >>hoping someone may be able to shed some light on what I need to do to >>segregate these logging rules. >> >> > >It depends on where you call Log::Log4perl->init(). > >If it's called in each child process, then the filename can be specified >dynamically (using something like the pid): > > http://log4perl.sourceforge.net/d/Log/Log4perl/FAQ.html#02bbc > >If Log::Log4perl->init() gets called in the main process, messages >need to be filtered dynamically, using a custom filter: > > The main process initializes Log4perl, then each child has its own log file to use. > http://log4perl.sourceforge.net/d/Log/Log4perl/FAQ.html#c7fa8 > http://log4perl.sourceforge.net/d/Log/Log4perl/Filter.html > >Let me know if you need more detailed info. > > It looks rather simple to filter based on a level, however with each child dynamically needing a different filename while main initializes I'm not clear on how the filter would need to be setup. >-- Mike > >Mike Schilli >m...@pe... > > |
From: Mike S. <m...@pe...> - 2005-09-10 21:18:23
|
On Fri, 9 Sep 2005, Chris Anderson wrote: > It looks rather simple to filter based on a level, however with each > child dynamically needing a different filename while main initializes > I'm not clear on how the filter would need to be setup. Alternatively, you could have the child change its appender file: use Log::Log4perl qw(:easy); my $conf = q( log4perl.category = DEBUG, Logfile log4perl.appender.Logfile = Log::Log4perl::Appender::File log4perl.appender.Logfile.filename = test.log log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m %n ); Log::Log4perl->init(\$conf); for(1..3) { my $pid = fork(); die "Can't fork" unless defined $pid; if($pid == 0) { # Child, switch appender file to test.log.PID my $app = Log::Log4perl->appender_by_name("Logfile"); my $old = $app->filename(); $app->file_switch($app->filename() . ".$$"); DEBUG "Child $$"; sleep (3); exit; } } print "parent\n"; wait() for (1..5); print "Done.\n"; -- Mike Mike Schilli m...@pe... |
From: Mike S. <m...@pe...> - 2005-09-11 00:21:31
|
On Sat, 10 Sep 2005, Mike Schilli wrote: > Alternatively, you could have the child change its appender file: Or, you could even write your own file appender. This way, things like init_and_watch() will still work: ###################### # FileTrunkappender.pm ###################### package FileTrunkAppender; use base "Log::Log4perl::Appender::File"; sub new { my($class, @options) = @_; my $self = { name => "unknown name", umask => undef, autoflush => 1, mode => "append", @options, }; die "Mandatory parameter 'filename_trunk' missing" unless exists $self->{filename_trunk}; bless $self, $class; } sub log { my($self, @args) = @_; if(! exists $self->{filename} or ! -f $self->{filename}) { $self->{filename} = $self->{filename_trunk} . ".$$"; $self->file_open(); } $self->SUPER::log(@args); } 1; ###################### # test.pl ###################### use Log::Log4perl qw(:easy); my $conf = q( log4perl.category = DEBUG, Logfile log4perl.appender.Logfile = FileTrunkAppender log4perl.appender.Logfile.filename_trunk = test.log log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m %n ); Log::Log4perl->init(\$conf); for(1..3) { my $pid = fork(); die "Can't fork" unless defined $pid; if($pid == 0) { DEBUG "Child $$"; sleep (3); exit; } } print "parent\n"; wait() for (1..5); print "Done.\n"; -- Mike Mike Schilli m...@pe... |
From: Chris A. <ch...@nu...> - 2005-09-11 05:20:08
|
Mike Schilli wrote: >On Fri, 9 Sep 2005, Chris Anderson wrote: > > > >>It looks rather simple to filter based on a level, however with each >>child dynamically needing a different filename while main initializes >>I'm not clear on how the filter would need to be setup. >> >> > >Alternatively, you could have the child change its appender file: > > use Log::Log4perl qw(:easy); > > my $conf = q( > log4perl.category = DEBUG, Logfile > log4perl.appender.Logfile = Log::Log4perl::Appender::File > log4perl.appender.Logfile.filename = test.log > log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout > log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m %n > ); > > Log::Log4perl->init(\$conf); > > for(1..3) { > my $pid = fork(); > > die "Can't fork" unless defined $pid; > > if($pid == 0) { > # Child, switch appender file to test.log.PID > my $app = Log::Log4perl->appender_by_name("Logfile"); > my $old = $app->filename(); > $app->file_switch($app->filename() . ".$$"); > > DEBUG "Child $$"; > sleep (3); > exit; > } > } > > print "parent\n"; > wait() for (1..5); > print "Done.\n"; > > > ah, that clears things up very well. Thank you. >-- Mike > >Mike Schilli >m...@pe... > > |