log4perl and Proc::Daemon
Matt Lawrence
matt.lawrence at virgin.net
Tue Jul 8 11:45:09 BST 2008
If memory serves, Proc::Daemon closes *all* handles when daemonising.
The easiest approach is to construct the logger object after
daemonising, and just use warn and die for reporting problems that occur
before then.
Also, why are you calling Proc::Daemon::Fork? Init() forks a child and
exits the parent, so you don't need to explicitly fork unless you've got
something you want the parent to do, which it appears from the comment
you don't.
Matt
Chisel Wright wrote:
> Sorry for taking the list off-topic, but I'm getting myself tied up in
> knots trying to resolve this issue.
>
> The key ingredients in this mix are Proc::Daemon, Log::Log4perl, and
> some modules that log via:
>
> package MyApp::Logger;
> # usual stuff ...
>
> # initialise the logger
> Log::Log4perl->init_once( $log4perl_conf_file );
>
> sub app_logger :Export() {
> my $category = shift;
> $category = defined $category ? $category : 'MyApp';
> return get_logger($category);
> }
>
> I've reduced my daemonizing script to:
>
> use Proc::Daemon;
> use Proc::PID::File;
> use MyApp::Logfile qw(app_logger);
>
> if (my $pid = Proc::Daemon::Fork) { # parent
> # nothing here
> }
> else { # child
> app_logger->debug('pre-daemon message');
>
> # daemonize
> Proc::Daemon::Init;
>
> # re-open stderr and stdout
> open(*STDOUT, '>>', '/tmp/test.log');
> open(*STDERR, '>&STDOUT');
>
> # test to make sure we see stuff there
> warn "warn()\n";
> print "print()\n";
>
> app_logger->debug('post-daemon message');
>
> # child loop
> while (1) {
> app_logger->debug('child tick');
> sleep(60);
> }
> }
>
> In the logs I'm seeing:
>
> warn()
> print()
> Cannot write to '/var/data/xtracker/logs/xtracker.log': Bad file
> descriptor at /usr/local/share/perl/5.8.8/Log/Dispatch/File.pm line 134.
>
> Do I need to re-init the Log::Log4perl somehow? I'm assuming (yeah, I
> know) that the FH get closed in a similar manner to stdout/stderr.
> If I do, any ideas what impact (if any) this will have on an active
> apache+mod_perl process that using the same logger?
>
> Chiz
>
More information about the london.pm
mailing list