From: Jens B. <log...@je...> - 2009-04-09 21:29:31
|
Hi, I am new to this list and hope this is the right place for my questions. I am using Log4perl in a server project with parent and child processes. And it's great, of course ;-) Due to an initialization of log4perl that has multiple steps I recently switched from text based initialization to API calls. Three problems occured: 1) The sub Log::Log4perl::Appender::Synchronized->post_init() is not called when the appender is created via API. Is this correct? My way to handle this was to write an own class derived from Synchronized an copy the code from post_init() to new(). 2) How to correctly configure a composite appender via API? Concrete: How to create an appender which is not connected to any logger and register it in a composite appender? (It seems that there is no alternative to the code used in Log::Log4perl::Config->create_appender_instance(), line 402) Following code works, but results in double log entries in the file: my $fileApp = Log::Log4perl::Appender->new( 'Log::Log4perl::Appender::File', name => 'MyFileApp', filename => '/tmp/mylog', mode => 'append', syswrite => 1, ); $fileApp->layout( Log::Log4perl::Layout::PatternLayout::Multiline->new('%d{yyyy-MM-dd HH:mm:ss} %p [%c] #%P> %m%n') ); $this->logger->get_root_logger()->add_appender($fileApp); my $syncApp = Log::Log4perl::Appender->new( 'Log::Log4perl::Appender::Synchronized', name => 'MySyncApp', appender => 'MyFileApp', key => 'nem', ); $this->logger->get_root_logger()->add_appender($syncApp); 3) When initialising Log::Log4perl::Appender::Synchronized before forking child processes, eventually all existing processes try to remove the semaphore. I could only solve this with a derived class that stores the process id of the parent and only allowed the parent to remove the semaphore. I'm struggling with these problems for 4 days now and would appreciate any answer! But for the time being: I wish you a very Happy Easter! Regards, Jens Berthold |
From: Mike S. <m...@pe...> - 2009-04-13 05:18:04
|
On Thu, 9 Apr 2009, Jens Berthold wrote: > 1) The sub Log::Log4perl::Appender::Synchronized->post_init() is not > called when the appender is created via API. Is this correct? My way > to handle this was to write an own class derived from Synchronized an > copy the code from post_init() to new(). Hi Jens, as you've noticed, the internal function create_appender_instance() performs some magic that makes composite appenders work, but this method isn't suited in its current form to be used from the outside. I'll dig into the internals to find a way to fix this. As it is now, you'd have to do something like use Log::Log4perl qw(get_logger :levels); my $fileApp = Log::Log4perl::Appender->new( 'Log::Log4perl::Appender::File', name => 'MyFileApp', filename => 'mylog', mode => 'append', ); $fileApp->layout( Log::Log4perl::Layout::PatternLayout::Multiline->new( '%d{yyyy-MM-dd HH:mm:ss} %p [%c] #%P> %m%n') ); $Log::Log4perl::Logger::APPENDER_BY_NAME{"MyFileApp"} = $fileApp; my $syncApp = Log::Log4perl::Appender->new( 'Log::Log4perl::Appender::Synchronized', name => 'MySyncApp', appender => 'MyFileApp', key => 'nem', ); $syncApp->post_init(); $syncApp->composite(1); get_logger("")->add_appender($syncApp); get_logger("")->level($DEBUG); get_logger("wonk")->debug("waah!"); which is most certainly terrible and, as mentioned above, needs to be fixed in one of the upcoming Log4perl releases. One more thing: Is there a reason why you're using 'syswrite' and the Synchronized appender in combination? By using 'syswrite', you won't need the Synchronized appender at all, because the OS will make sure that messages will be written out entirely without overlap. > 3) When initialising Log::Log4perl::Appender::Synchronized before > forking child processes, eventually all existing processes try to remove > the semaphore. > I could only solve this with a derived class that stores the process id > of the parent and only allowed the parent to remove the semaphore. Hmm, this sounds like a bug, but I'm not sure what your 'existing' processes are doing in this case -- can you provide some test code? -- Mike Mike Schilli m...@pe... |
From: Mike S. <m...@pe...> - 2009-04-20 07:01:48
|
On Sun, 12 Apr 2009, Mike Schilli wrote: > which is most certainly terrible and, as mentioned above, needs to be > fixed in one of the upcoming Log4perl releases. Okay, I've cleaned it up a bit and added some documentation on how to initialize composite appenders with the API: http://github.com/mschilli/log4perl/commit/da3f88ed6e556418685b367674384e4076c9d261 -- Mike Mike Schilli m...@pe... |
From: Jens B. <log...@je...> - 2009-04-21 19:39:10
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> <title></title> </head> <body bgcolor="#ffffff" text="#000000"> Mike Schilli wrote:<br> <br> >> I added a new test script for this behaviour and a possible patch to<br> >> Synchronized (Log4perl version 1.21).<br> ><br> >Looks good, I've applied it to the git repo (we're in the process of<br> >migrating to github).<br> <br> Great! Altough my changes were tiny, it still feels good to collaborate in such a useful project like log4perl.<br> <br> > Okay, I've cleaned it up a bit and added some documentation on how to<br> > initialize composite appenders with the API:<br> ><br> > <a class="moz-txt-link-freetext" href="http://github.com/mschilli/log4perl/commit/da3f88ed6e556418685b367674384e4076c9d261">http://github.com/mschilli/log4perl/commit/da3f88ed6e556418685b367674384e4076c9d261</a><br> <br> Thank you, this will simplify my code. Now it's a clean thing to do the API initialization.<br> Thank you for the documentation, too.<br> <br> Best wishes aus der alten Heimat ;-)<br> Jens<br> <br> </body> </html> |