Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: using log4perl

by andreychek (Parson)
on Sep 04, 2003 at 17:44 UTC ( [id://288941]=note: print w/replies, xml ) Need Help??


in reply to using log4perl

Howdy,

Log::Log4perl is an excellent tool to use, and you seem to be on the right track. Log4perl allows you to use a heirarchy, similar to classes in OO programming. Your first line in your config file, with the word "rootLogger" in it, is at the top of the heirarchy. What Log4perl does, is any time a child logger logs a message, no matter what log level it was at when it was logged, that message is automatically given to all the parent loggers to be logged. The second line in your config file is setting up, a child logger.. sort of.

So that's exactly your problem -- messages are being logged at the INFO level, Log4perl says "Oh goody, this logger has parents!", and Log4perl gives the messages to the parents to log, as well as the child. Alas, it then gets logged in both places, which isn't what you want in this case.

I might recommend setting up different categories. What's going on becomes a little more obvious, and the actual problem is a bit easier to see. To do that, we'll get rid of the first two lines of your config file:

log4perl.rootLogger=FATAL, A1, Mailer
log4perl.logger=INFO, log

And we'll put this in it's place:

log4perl.logger.SomeCategory=FATAL, A1, Mailer
log4perl.logger.AnotherCategory=INFO, log

Of course, instead of "SomeCategory" and "AnotherCategory", you should use names which would be useful to you :-)

What we've is seperate your two loggers into seperate categories. They are no longer related, and we won't have a problem with duplicate messages. But your Perl code will have to change a bit to make use of these new categories. It's simply, and would just look like this:

my $log = Log::Log4perl->get_logger("SomeCategory); # -- or -- my $log = Log::Log4perl->get_logger("AnotherCategory); # Logging code looks the same, but no longer gets logged # to both places $log->info("connecting to database");

There are times when you don't want to use unrelated categories. Categories are quite useful, and often you do want to set up a heirarchy of some sort. To prevent duplicate logs in that case, you can do any of the following:

# Prevent the message from bubbling up to it's parents # by setting the logger's additivity to 0 in the config # file logger.additivity.SomeCategory = 0 # Tell a given appender not to accept messages # unless they were logged at a particular level logger.appender.log.theshold = FATAL

If you haven't already, I highly recommend reading the Log4perl tutorial on Perl.com. It goes over a few of the Log4perl traps, and offers some excellent examples of usage.

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."

Replies are listed 'Best First'.
Re: Re: using log4perl
by tbone (Beadle) on Sep 04, 2003 at 18:08 UTC
    Thanks Eric for the response. I found another way of handling this situation that works well if the script isnt in any sort of clean heirarchy. It uses the Log::Log4perl::filter. Here is the code:
    log4perl.logger=INFO, FatalFile, FatalMailer, InfoLog log4perl.filter.MatchFatal = Log::Log4perl::Filter::LevelMatch log4perl.filter.MatchFatal.LevelToMatch = FATAL log4perl.filter.MatchFatal.AcceptOnMatch = true log4perl.filter.MatchInfo = Log::Log4perl::Filter::LevelMatch log4perl.filter.MatchInfo.LevelToMatch = INFO log4perl.filter.MatchInfo.AcceptOnMatch = true log4perl.appender.FatalFile=Log::Dispatch::File log4perl.appender.FatalFile.filename=test.log log4perl.appender.FatalFile.mode=append log4perl.appender.FatalFile.layout=PatternLayout log4perl.appender.FatalFile.layout.ConversionPattern=%d %p> %F{1}:%L % +M - %m%n log4perl.appender.FatalFile.Filter = MatchFatal log4perl.appender.FatalMailer = Log::Dispatch::Email::MailSend +mail log4perl.appender.FatalMailer.to = joeshmoe log4perl.appender.FatalMailer.from = joeshmoe log4perl.appender.FatalMailer.subject = Something's broken! log4perl.appender.FatalMailer.layout = SimpleLayout log4perl.appender.FatalMailer.Filter = MatchFatal log4perl.appender.InfoLog=Log::Dispatch::File log4perl.appender.InfoLog.filename=mailer.log log4perl.appender.InfoLog.mode=append log4perl.appender.InfoLog.layout=PatternLayout log4perl.appender.InfoLog.layout.ConversionPattern=%d %p> %m%n log4perl.appender.InfoLog.Filter = MatchInfo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-03-28 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found