Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Log4perl modifying filename

by rootcho (Pilgrim)
on Jan 25, 2011 at 20:38 UTC ( #884216=perlquestion: print w/replies, xml ) Need Help??
rootcho has asked for the wisdom of the Perl Monks concerning the following question:

hi guys
Do you know a way to modify attributes of appenders, layouts etc.. on the fly
without putting perl code inside the config i.e. something like this
Log::Log4perl->init(\$conf); my $appenders = $log->appenders; $appenders->{blah}->filename('/path/to/new_file.log');
the only examples of dynamic values I see are using perl code inside the configuration which call-backs some code inside your app.
I don't want to create the appenders on the fly, I want to modify already configured appender in the config


Replies are listed 'Best First'.
Re: Log4perl modifying filename
by chrestomanci (Priest) on Jan 25, 2011 at 22:03 UTC

    What do you have against putting perl code in your config?

    You know that you can keep your config in a string, modify it in the usual way, and then pass that string into Log::Log4perl::init. Something like this (from one of my scripts)

    my $log4perlConf = q( log4perl.appender.scriptName.filename=__PLACEHOLDER__ log4perl.appender.scriptName.layout=Log::Log4perl::Layout::Pattern +Layout log4perl.logger = INFO, scriptName ); $log4perlConf =~ s/__PLACEHOLDER__/$logFileName/; Log::Log4perl::init(\$log4perlConf);

    I don't think this approach will let you change the output file on the fly, though depending on what you are trying to do, you could also look into: Log::Dispatch::FileRotate which rotates the log file daily, weekly or whatever you like.

Re: Log4perl modifying filename
by Khen1950fx (Canon) on Jan 26, 2011 at 01:27 UTC
    I experimented with the file_switch method and tried this:
    #!/usr/bin/perl use strict; use warnings; use Log::Log4perl::Appender::File; my $app = Log::Log4perl::Appender::File->new( filename => 'new_file.log', mode => 'append', autoflush => 1, syswrite => 1, binmode => ":utf8", ); my $file = $app; for $file ('some_other') { while ($app->file_switch("$file.log")) { require "$"; } print "$file.log\n"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://884216]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2017-07-20 19:28 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (313 votes). Check out past polls.