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

Log::Log4perl Module

by nvivek (Vicar)
on Feb 12, 2011 at 05:36 UTC ( #887729=perlquestion: print w/replies, xml ) Need Help??
nvivek has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
I am using Log::Log4perl for logging the message in the module. In my code, when problem occurs, I am printing the message because of this, it prints the same log message again and again. I want to print the message one time with count. Is there any way to achieve this in Log::Log4perl? I want to suppress the duplicate messages.

Replies are listed 'Best First'.
Re: Log::Log4perl Module
by broomduster (Priest) on Feb 12, 2011 at 15:03 UTC
      A side effect of this buffering approach is that the timestamp will be incorrect for most messages, especially when there is a long delay between messages. Adding time-based signals could reduce the problem, but not mitigate it completely.
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
        Agreed. The OP is apparently trying to emulate behavior of syslog, where some(?) implementations do this by default. But there are plenty of rants about why this is not necessarily a good idea.

      Thanks a lot. I done appropriate checking in the log function to compare the messages excluding timestamp.

Re: Log::Log4perl Module
by Khen1950fx (Canon) on Feb 12, 2011 at 06:26 UTC
    Here's the FAQ about the problem. It'll show you how to set the additivity flag to 0.

      I think you have mis-understood nvivek's question. The FAQ entry you linked to covers the situation where due to the way log4perl is configured, and how multiple loggers interact with each other a single call to $log->warn('some message'); results in duplicate copies of a single message appearing.

      I think nvivek has a different situation. He has written some code like this:

      sub connect_to_service { my $service_url = shift; my $logger = Log::Log4perl->get_logger(); my $max_attempts = 10; ATTEMPT: while( $max_attempts-- ) { my $connection = connect($service_url, {timeout=>30}); if( defined $connection ) { $logger->info("Connected OK to $service_url"); return $connection; } else { $logger->error("Error connecting to $service_url, will ret +ry"); } } # If this line is reached then all 10 connection attempts failed $logger->logdie("Could not connect to $service_url"); }

      nvivek wants to see in his logs something like this:

      ERROR: Error connecting to soap//service, will retry [last message repeated 10 times] FATAL: Could not connect to soap//service

      To do this, you would need to run a log::log4perl appender that retains state, and compares the current message with the previous one, and if they are similar enough would not log the message, but instead would increment an internal count. When it sees a different message it would then emit the last message repeated $n times message. (The appender could not simply compare for identical messages, because most layouts pretend log messages with date-stamps or the like.)

      Unfortunately I don't know of an appender that fits this requirement, and I don't want to post the result of a google search without taking the time to understand it and make sure that it matches what the supplicant asked for.

        Thanks a lot chrestomanci. You understood my requirement correctly. I am also searching it. If you find the result, post the answer.
        Thanks & Regards,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://887729]
Approved by broomduster
Front-paged by chrestomanci
[stevieb]: perldigious You should start by writing some unit tests. That'll ensure current functionality doesn't break with changes.
[choroba]: unit tests++
[choroba]: The only problem you can't solve by adding more tests is the problem of having too many tests.
[stevieb]: choroba Been there, done that :) Better too many than not enough though!
[stevieb]: I very often write a base set of unit tests before writing any code. It helps dictate the API, and ensures my earliest of ideas stay on track.

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

    Results (335 votes). Check out past polls.