Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: log4perl help

by Anonymous Monk
on Mar 03, 2021 at 14:05 UTC ( [id://11129079]=note: print w/replies, xml ) Need Help??


in reply to Re^2: log4perl help
in thread log4perl help

Use the init method, example in doc

Replies are listed 'Best First'.
Re^4: log4perl help
by drohr (Acolyte) on Mar 03, 2021 at 16:23 UTC

    I appreciate the help and I know about the documentation. I have been reading all that I can and trying to implement what is being discussed. Clearly, I am not understanding something and I would like some specific code direction.

    I took the advice and tried to use the init() method. Here is my attempt:

    use Log::Log4perl; sub init() { my %log_conf = ( "log4perl.logger.STDOUT" => "DEBUG, STDOUT", "log4perl.logger.FILE" => "DEBUG, FILE", "log4perl.appender.STDOUT" => "Log::Log4perl::Appender: +:Screen", "log4perl.appender.STDOUT.stderr" => "0", "log4perl.appender.STDOUT.layout" => "Log::Log4perl::Layout::P +atternLayout", "log4perl.appender.STDOUT.layout.ConversionPattern" => "%d %p> + %F{1}:%L %M - %m%n", "log4perl.appender.FILE" => "Log::Log4perl::Appender: +:File", "log4perl.appender.FILE.filename" => "test.log", "log4perl.appender.FILE.mode" => "append", "log4perl.appender.FILE.layout" => "Log::Log4perl::Layout::P +atternLayout", "log4perl.appender.FILE.layout.ConversionPattern" => "%d %p> % +F{1}:%L %M - %m%n" ); Log::Log4perl->init(\%log_conf); } sub getLogger { my ($category) = @_; my $logger = Log::Log4perl->get_logger($category); return $logger; } sub modify_logger { my ($level) = @_; Log::Log4perl->appender_by_name('FILE')->{min_level} = $level; Log::Log4perl->appender_by_name('FILE')->{filename} = "file2.log"; ${Log::Log4perl::Logger::APPENDER_BY_NAME{'FILE'}}->{min_level} = +$level; ${Log::Log4perl::Logger::APPENDER_BY_NAME{'FILE'}}->{filename} = " +test2.log"; my $logger_appender = ${Log::Log4perl::Logger::APPENDER_BY_NAME{'F +ILE'}}; my $appenders = Log::Log4perl->appenders(); my $appender = Log::Log4perl->appender_by_name('FILE'); print; } init(); modify_logger('TRACE'); my $stdout_logger = getLogger(STDOUT); $stdout_logger->trace("testing stdout logger"); my $file_logger = getLogger(FILE); $file_logger->trace("testing file logger"); print

    I can query and modify the appenders in the modify_logger() function (I can see the changes in my debugger), but the logging behavior does not change. How can I change the values AND the behavior?

    I have been banging my head against this problem and I could really use some specific help.

    Thank you

      While I am unfamiliar with Log4perl, I can guess that there is probably some action you must take to cause the output file to be reopened before a change to the output filename will be effective.

      Is there some kind of "rotate logs" operation in Log4perl?

        After alot of searching, I found this:

        http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl/Appender/File.html

        So, for the next person who runs into this issue. This is how you change the file in an appender at run time:

        Log::Log4perl->appender_by_name('FILE')->file_switch('test2.log');

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129079]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found