Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: log4Perl dynamic filename

by periapt (Hermit)
on May 20, 2013 at 12:24 UTC ( #1034326=note: print w/replies, xml ) Need Help??

in reply to log4Perl dynamic filename

For many projects, I like to define a simple functional logging configuration.
sub InitLogger { my $logfile = shift; my $logconf = q| log4perl.logger.[package_name] = INFO, Log +File log4perl.appender.LogFile = Log::Log4perl::Appende +r::File log4perl.appender.LogFile.filename = LOGFILE log4perl.appender.LOG.mode = append log4perl.appender.LogFile.layout = PatternLa +yout log4perl.appender.LogFile.layout.ConversionPattern = %d %5p> % +m%n |; $logconf =~ s/LOGFILE/$logfile/; Log::Log4perl->init( \$logconf ); return Log::Log4perl::get_logger("[package_name"); } # end InitLogger();
I set the log4perl.appender.LogFile.filename option to an arbitrary value 'LOGNAME' so that I can replace it in the string later. Not sure why but I've found this to be more stable than using variable substitution inside the config string itself.

If my projects grows to encompass several program files with almost identical log specs (except maybe for filename), I can pull the function into a library and share

use strict; use warnings; use diagnostics;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1034326]
[Corion]: marto: How's things? I hope the kids are fine and you too!
[Corion]: Oh yay. I wonder why a very simple change in a program doesn't elicit a diff, and now I see that my diff program seems to have a bug ;)
[1nickt]: marto thanks for asking, so far so good. A pretty modern stack and decent procedures, although rather too much home-built stuff (e.g. a logging role that should tries to duplicate Log::Any).
[Corion]: No. It's just that I'm comparing the same output file twice, instead of comparing the output files of the two runs %-)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2017-12-11 11:39 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (289 votes). Check out past polls.