Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: log4Perl dynamic filename

by dd-b (Monk)
on May 16, 2013 at 22:47 UTC ( [id://1033924] : note . print w/replies, xml ) Need Help??

in reply to log4Perl dynamic filename

I do it like this:

use Log::Log4perl; my $logfile = 'clean.log'; unlink $logfile; # ignore errors do { # Isolate # Log configuration. # If you go for interpolation, escape the {} in the pattern or use # different delimiters or something. my $lconf = q{ log4perl.logger = TRACE, ap1 log4perl.appender.ap1 = Log::Log4perl::Appender::File log4perl.appender.ap1.filename = } . $logfile . q' log4perl.appender.ap1.layout = Log::Log4perl::Layout::PatternLayou +t log4perl.appender.ap1.layout.ConversionPattern = %d{yyyyMMdd HH:mm +:ss.SSSS} %5p %F{1} %L: %m %n log4perl.appender.ap2 = Log::Log4perl::Appender::Screen log4perl.appender.ap2.layout = Log::Log4perl::Layout::PatternL +ayout log4perl.appender.ap2.layout.ConversionPattern = %d{yyyyMMdd H +H:mm:ss.SSSS} %5p %F{1} %L: %m %n '; # unclear, but this MIGHT mean something. Log::Log4perl::init ( \$lconf ) or die "Log init failed"; }; # Isolate my $logger = Log::Log4perl->get_logger('Test');
That is, by using substitution within the quoted string config.

You could also do it using function-call config, and possibly by doing a string config and then overriding that one thing with a function call, I believe, but I haven't done that, so I won't give you fake example code.

Replies are listed 'Best First'.
Re^2: log4Perl dynamic filename
by Anonymous Monk on May 17, 2013 at 13:58 UTC
    Hello fritz1968, I personally prefer to use config files for configuration purposes, so I have a file pllog.conf:
    log4perl.rootLogger = TRACE, FileApp, XMLAppender, ScreenApp log4perl.appender.FileApp = Log::Log4perl::Appender::File log4perl.appender.FileApp.filename = sub { my $Me=$0; $Me =~ s/^.*\\// +; $Me =~ s/\..*//; return "$Me.log" } log4perl.appender.FileApp.layout = PatternLayout log4perl.appender.FileApp.layout.ConversionPattern = %d{ISO8601} (%- +13F: %04L) %m%n
    And somewhere in perl code:
    use Log::Log4perl qw(get_logger); Log::Log4perl->init("pllog.conf"); my $logger = get_logger("My_Tools.$Me");
    Used in this way, you get a logfile with the same name as your perl file but with .log instead of .pl. I only showed the file appender path, as you requsted. You can change this approach easily to fit your needs. And you can change the whole log4perl thing without changing your code.