BeneSphinx has asked for the wisdom of the Perl Monks concerning the following question:

I am revamping the logging for a program I maintain and trying to do it the "right way." Wanted to get your thoughts as I'm trying to avoid butchering this, as I have a unique architecture... I want to write INFO+ to Screen... simple enough. However, as for files, I want to switch between files depending on program flow. The program is basically a wrapper that runs other jobs, like:
Launch program Run Job 1 Run Job 2 Run Job 3 ... Exit program
The wrapper takes care of things at a "high level" and the jobs do the "low level" dirty work. So my idea was to write wrapper logging to a "main.log" and then each job gets its own log, like "job1.log", "job2.log", etc. The jobs are run in a "for" loop, by the way. But both main.log and jobX.log will have the same range of severity levels, from DEBUG to ERROR, so I can't use those to distinguish. I have two ideas:
  1. Use two different loggers, one with the mainLogger category and one with the jobLogger category. Both would have the Screen appender but they'd have different File appenders. Use one logger or the other as needed. Downside is maintaining separate loggers plus now categories != packages

    OR

  2. Use a single logger, but swap the file appenders when I get to different parts of the code, like:
    [set appenders: Screen, Main] ... for my $job (@jobs){ [set appenders: Screen, (current job)] } [set appenders: Screen, Main]
    Downside is that I don't think appenders were meant to be dynamically swapped in this way, at least not frequently.
Wondering which of these (if either) seems best to you, according to best practices... thank you greatly!

Replies are listed 'Best First'.
Re: log4perl usage question
by zwon (Abbot) on Mar 31, 2012 at 06:58 UTC

    I don't see how the 1 can work. Unless you have predefined set of jobs, you have to create new appender for every job anyway. If you can be satisfied with just two appenders -- one for the main process and one for jobs, you can set NDC before starting the job and add filters to appenders that will filter messages based on NDC, in this case you don't have to change categories.

    I don't see anything principally wrong about 2, though it depends on the number of jobs and your exact intentions.

Re: log4perl usage question
by wwe (Friar) on Mar 31, 2012 at 09:47 UTC
    First of all if your "jobs" are different programs you start from the wrapper I see any chance to have them use same logger instance. If "jobs" are part of the program why don't let the write into the same file with their own log category? And I think you have to create one appender per log file you expect and then you can filter messages based on the category (wich comes from the job)