Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

log4perl question

by chimni (Pilgrim)
on Sep 17, 2003 at 05:26 UTC ( [id://292058]=perlquestion: print w/replies, xml ) Need Help??

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


hi monks,
i am trying to set up a logging implementation which logs WARN messages to one log and INFO messages to another log whose filename should be dynamically generated from the month which is ongoing
here is my attempt
log4perl.rootLogger=INFO,LOGFILE log4perl.appender.LOGFILE=Log::Log4perl::Appender::File log4perl.appender.LOGFILE.filename=/var/log/myerrs.log log4perl.appender.LOGFILE.mode=append log4perl.appender.LOGFILE.layout=PatternLayout log4perl.appender.LOGFILE.layout.ConversionPattern=%d [%l] %p %c - %m% +n log4perl.Logger.newpingdata=INFO,LOG log4perl.appender.LOG=Log::Log4perl::Appender::File log4perl.appender.LOG.filename=/var/log/errs.log log4perl.appender.LOG.mode=append log4perl.appender.LOG.layout=PatternLayout log4perl.appender.LOG.layout.ConversionPattern=%d [%l] %p %c - %m%n # tried this above to dynamicaaly generate filename log4perl.appender.LOG.filename = sub { my @curr=`date`;my +($day,$month,$date,$time,$std,$year) = split (/\s+/,$curr[0]);chomp $ +month; return "/var/log/myerrs.$month.log"} I use this for logging my $log -> Log::Log4perl->get_logger(); $log->warn("hi"); my $log -> Log::Log4perl->get_logger("newpingdata"); $log->info("down"); but both hi and down get logged in the same file (/var/log/myerrs.log) +????

any suggestions.
regards
chimni

Replies are listed 'Best First'.
Re: log4perl question
by leriksen (Curate) on Sep 17, 2003 at 07:23 UTC
    I'm not sure one can put perl code in the conf file - dont forget that Log::Log4perl can also parse xml and java - but if it doesn't perhaps your best bet is to dynamically generate the conf file itself - put a line like
    log4perl.appender.LOG.filename=MARKER
    and have your script do a
    s/MARKER/gen_log_name()/e;
    or such like - sorry for the brevity - she who must be obeyed just summoned me home.
Re: log4perl question
by andreychek (Parson) on Sep 17, 2003 at 14:05 UTC

    Howdy,

    At a glance, the code in your conf file looks good. I think you're being bitten by another issue -- basically, whenever Log4perl sends a message off to your newpingdata Logger, it additionally will give that message to every other logger in the heirarchy, including your rootLogger.

    The exact problem and solution were discussed in-depth, including examples, in this node on Log4perl. To summarize, you can tell a logger not to send messages up the heirarchy using additivity. You can also tell a given appender not to accept log messages below a certain log level by using threshold. And of course, there are filters and other goodies, but I don't think you need to go that far for your current problem.

    So, check out the above node, and also look up those terms in Log4perl's documentation. Good luck!

    -Eric

    --
    Lucy: "What happens if you practice the piano for 20 years and then end up not being rich and famous?"
    Schroeder: "The joy is in the playing."
      And that is why recently I started to not even have a root logger - to avoid the double logging that you mention.
      I did this as an experiment with some code I am writing now - and it seems to work fabulously well
      # set your logger here # Note - no root logger - this is not always what you want # but at this early stage of developement, is good enough #log4perl.logger=INFO, A1 log4perl.logger.WSR_Generator._create_wsr_document=INFO, A1 #log4perl.logger.WSR_Utils.form_order=DEBUG, A1 #log4perl.logger.WSR_New_Worker_Return.create_wsr_records=DEBUG, A1 #configure SCREEN logger log4perl.appender.A1=Log::Dispatch::Screen log4perl.appender.A1.min_level=debug log4perl.appender.A1.stderr=1 log4perl.appender.A1.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.A1.layout.ConversionPattern=%d %-5.5p - %M - %m%n

      Thank you andrey and lerik
      i have it up and running no root looger suits me for the time being.
      You were right it was a "inheritance" problem
      thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-18 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found