in reply to log4perl collecting (buffering) messages

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

Replies are listed 'Best First'.
Re^2: log4perl collecting (buffering) messages
by coke4all (Novice) on Mar 14, 2013 at 10:11 UTC

    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

        THANKS!!!!!!! you SAID IT ALL. I thinks next time i´ve to read the documentation for the SUPER-Class too ... puhhh ;-)

        In addition i´ve found this:

        "There's no need to pass around logger references between your system's functions. This effectively avoids cluttering up your beautifully crafted functions/methods with parameters unrelated to your implementation. get_logger() can be called by every function/method directly with little overhead in order to obtain a logger. get_logger makes sure that no new object is created unnecessarily. In most cases, it will just cheaply return a reference to an already existing object (singleton mechanism)."

        from: http://www.perl.com/pub/2002/09/11/log4perl.html