Brothers,

I've searched long and read much on daemons and logging, including Proc::Daemon and Log::Log4perl on using Log::Log4perl and Proc::Daemon. Although that example suggests initializing Log4perl after calling Init() works, I haven't been able to get that to work, and I need to do logging both before and after going into the background.

Basically, I need to start up a daemon process, have it process configuration and command line arguments and log to a file via log4perl. If anything fails during initialization, it will exit with a non-zero code, letting the caller know that the startup didn't succeed. After initialization, the process will daemonize and continue logging, hopefully to the same log file.

The problem I have is that I need to process config and log before I daemonize, or I can't return a meaningful exit status to the caller. I need to continue to log after I deamonize, and would like to log to the same FileRotate appender/file.

I've tried quite a few combinations, and cannot get log4perl to initialize at all after Proc::Daemon::Init, regardless of whether I have attempted to initialize a log before the Init() call.

If I initialize before the Init() call, I get all the config logging I would expect, but then nothing (naturally, since Init() closed all the FDs). I have tried reinitializing after Init, but as I stated above above, no matter what I do, I can't get any logging after Init().

There must be a proven recipe out there for exactly this situation:

  1. start the process
  2. process args and config w/ logging
  3. exit with non-zero status on error
  4. go into background
  5. continue logging

I've looked at App::Daemon, which looks promising. But the description of how log4perl is initialize and handled is vague. What I don't understand is how to get access to the logger initialized by App::Daemon, and if the logger is available both before and after the call to daemonize().

Please, bless me Brothers, with the wisdom you hold within you.


In reply to Daemon w/ logging before and after background by batkins61

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.