I have a question about forwarding STDERR & STDOUT to a log4perl object on a per-job basis (inside a while loop). Here is some background:

I have a daemon program (uses Proc::Daemon) that grabs "jobs" to process from a database. Once it has the "job" it uses the info from the database to shell out and "process" the job. The processing is basically calling a bunch of external programs in sequence and making sure the output of said programs looks sane. There's probably 8-10 steps in a "job" that are all handled by different program.

When each new job is started, a new Log::Log4perl "category" is created and this category is logged to that job's "run path" (the directory in which the commands are run). I can then call:
$log->('some global log message'); $job_log->('some log message specific to this job');
This allows me to log messages specific to a job to one log file and all other messages to a global log file. This is all working fine.

Although we hope that there will never be errors with the various external programs, we are more realistic and we know that we will at some point have to debug some errors. So I would like to forward the STDERR and STDOUT from these external programs to my $job_log log4perl object.

When Proc::Daemon runs, it somehow takes over STDOUT and STDERR so I don't see the messages. I'm not sure this matters.

The question is, how do I make it so messages written to STDOUT and STDERR are forwarded (and timestamped) by Log::Log4perl?

I have seen this FAQ answer:

Some module prints messages to STDERR. How can I funnel them to Log::Log4perl?

Which is somewhat related but doesn't really answer the question. They are using a DEBUG function call as opposed to an object $job_log->debug() call. I'm not sure this distinction matters. Any help is greatly appreciated.

In reply to Daemon, log4perl & stderr/stdout by saberworks

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.