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

Hi there,

I have a question about log4perl and message buffering

Currently i have implemented a Moose based Distribution, which will be used by my perl-script. My question now is how can i collect all error/warning/info messages throughout all used modules and send this as a mail-digest at the end of the perl-script in ONE Message? Is there a clever way to do this - because i think this must be a standard problem; but i have not found some solution in the web ....?

The point is: i do NOT want sending the mail out right now, when the error/warning/info happens. I just want to collect it for sending the mail at the end.

Do i really have to write a own Appender for this?

Thanks

  • Comment on log4perl collecting (buffering) messages

Replies are listed 'Best First'.
Re: log4perl collecting (buffering) messages
by Neighbour (Friar) on Mar 13, 2013 at 12:39 UTC
    Or you could just use a standard appender and log to file. Then setup a scheduled job that mails the file at specific times.

      first of all - thanks for your reply ...

      but i thought, there is a more elegant solution included in log4perl .... hmmm ... it seams that i have to write a own appender for collecting and buffering the messages till i send it at the end of the script.

      the point is that i do NOT want to write to disk and do NOT pass a message-string through all used modules (concatenating all messages).

Re: log4perl collecting (buffering) messages
by McA (Priest) on Mar 13, 2013 at 17:08 UTC

    Hi,

    have a look at Log::Log4perl::Appender::String. I used it once for a similar reason.

    But afterwards I don't think that the resulting code was nice. I think this is the wrong solution/architecture.

    Best regards
    McA

      Hi McA

      ... it seams that this is what i searched for ;-) - Thanks! And i share your opinion about the architecture!

      But if i use this Appender and fetch a appropriate logger of it ... how can i make my Moose Objects aware of this logger?

      e.g. if i use the ROOT-logger i can do this with the Role Concept of Moose like this:

      package My::Class; use Moose; with 'MooseX::Log::Log4perl'; ... $self->log->info("CREATED Object: " . $self->name);

      But if i create my OWN-logger in my perl-script like this:

      # my perl-script using My::Class my $fileLogger = Log::Log4perl->get_logger('file');

      i would have to pass the reference $fileLogger through ALL my Modules - have i? - or is there a better solution to make my Modules aware of $fileLogger??? (similar to the solution with ROOT-Logger)

        Hi,

        this is now more a MooseX::Log::Log4perl related question. When you look at the perldoc of that package IMHO anything is said. You have to initialize the Log::Log4perl singleton before logging the first time. Example is given in the perldoc.

        Init like

        my $log4perl_conf = q( log4perl.logger=DEBUG, STRINGCOLLECTOR log4perl.appender.STRINGCOLLECTOR = Log::Log4perl::Appender::String log4perl.appender.STRINGCOLLECTOR.layout = Log::Log4perl::Layout::Patt +ernLayout log4perl.appender.STRINGCOLLECTOR.layout.ConversionPattern = %p:%d{ISO +8601}:myscript: %m%n ); Log::Log4perl->init( \$log4perl_conf );
        before first usage.

        UPDATE: As the doc says: You can get the logger instance with $self->logger. So later you should get the collected output with

        my $appender = $self->logger->appender_by_name('STRINGCOLLECTOR'); my $string = ''; if($appender) { $string = $appender->string(); }

        McA

Re: log4perl collecting (buffering) messages (Log::Log4perl::FAQ)
by Anonymous Monk on Mar 14, 2013 at 00:34 UTC

      hi

      thanks for your reply, but i want to determine exactly when to send the mail. I know that your solution is buffering the logs before sending. Your can disable buffering with:

      log4perl.appender.Mailer.buffered = 0

      in your log4perl.conf

      but this is not what i´m searching for ... nevertheless THANKS