From: Robert J. <yad...@sn...> - 2007-03-13 19:58:46
|
I'm trying to use the "trigger" function of Buffer, but I can't seem to get it to work. I just took the examples from the perldoc and stuck them together: use Log::Log4perl qw(:easy); my $conf = qq( log4perl.category = DEBUG, Buffer # Regular Screen Appender log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.stdout = 1 log4perl.appender.Screen.layout = PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %d %p %c %m %n # Buffering appender, using the appender above as outlet log4perl.appender.Buffer = Log::Log4perl::Appender::Buffer log4perl.appender.Buffer.appender = Screen log4perl.appender.Buffer.trigger = sub { \ my($self, $params) = @_; \ return $params->{log4p_level} >= \ $Log::Log4perl::Level::ERROR; } ); Log::Log4perl->init(\$conf); DEBUG("This message gets buffered."); INFO("This message gets buffered also."); # Time passes. Nothing happens. But then ... print "It's GO time!!!\n"; ERROR("This message triggers a buffer flush."); But when I run the above I get: Can't evaluate 'sub {' (Missing right curly or square bracket at (eval 9) line 2, at end of line syntax error at (eval 9) line 2, at EOF ) at /opt/ActivePerl-5.8/lib/site_perl/5.8.7/Log/Log4perl/Config.pm line 741. Even a simple "return 1" doesn't work, i.e.: log4perl.appender.Buffer.trigger = sub {return 1;} Am I missing something simple? (again? :) ) -- Rob |
From: Kevin M. G. <cp...@go...> - 2007-03-13 21:01:59
|
I thought this would be a quick, easy answer, but there's actually a couple things going on: 1) since you're using qq() to enclose the string containing your config, all the $'s and \'s are being interpolated by perl when the script loads. Use q() instead--it's like single quotes around a string, so there is no interpolation. 2) It looks like the docs for trigger are just a little bit wrong. To get the desired behavior, you currently have to wrap your sub in another sub, like this: log4perl.appender.Buffer.trigger = sub { sub{return 1} } I'll propose a fix to Mike in a separate note to the list. 3) The comparison inside the trigger also won't work the way the docs are written. You'll need something like this, which is inherently inefficient and uses some internal functions, but it's a quick answer: log4perl.appender.Buffer.trigger = sub { \ my($self, $params) = @_; \ return \ Log::Log4perl::Level::to_priority($params->{log4p_level}) \ >= \ Log::Log4perl::Level::to_priority('ERROR') } Robert Jacobson wrote: > I'm trying to use the "trigger" function of Buffer, but I can't seem > to get it to work. I just took the examples from the perldoc and > stuck them together: > > use Log::Log4perl qw(:easy); > > my $conf = qq( > log4perl.category = DEBUG, Buffer > > # Regular Screen Appender > log4perl.appender.Screen = Log::Log4perl::Appender::Screen > log4perl.appender.Screen.stdout = 1 > log4perl.appender.Screen.layout = PatternLayout > log4perl.appender.Screen.layout.ConversionPattern = %d %p %c %m %n > > # Buffering appender, using the appender above as outlet > log4perl.appender.Buffer = > Log::Log4perl::Appender::Buffer > log4perl.appender.Buffer.appender = Screen > log4perl.appender.Buffer.trigger = sub { \ > my($self, $params) = @_; \ > return $params->{log4p_level} >= \ > $Log::Log4perl::Level::ERROR; } > ); > > Log::Log4perl->init(\$conf); > > DEBUG("This message gets buffered."); > INFO("This message gets buffered also."); > > # Time passes. Nothing happens. But then ... > > print "It's GO time!!!\n"; > > ERROR("This message triggers a buffer flush."); > > > But when I run the above I get: > > Can't evaluate 'sub {' (Missing right curly or square bracket at > (eval 9) line 2, at end of line > syntax error at (eval 9) line 2, at EOF > ) at /opt/ActivePerl-5.8/lib/site_perl/5.8.7/Log/Log4perl/Config.pm line 741. > > > Even a simple "return 1" doesn't work, i.e.: > log4perl.appender.Buffer.trigger = sub {return 1;} > > Am I missing something simple? (again? :) ) > > > -- > Rob > > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share your > opinions on IT & business topics through brief surveys-and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > log4perl-devel mailing list > log...@li... > https://lists.sourceforge.net/lists/listinfo/log4perl-devel |
From: Robert J. <yad...@sn...> - 2007-03-14 12:49:41
|
At 04:01 PM 3/13/2007, Kevin M. Goess wrote: >I thought this would be a quick, easy answer, but there's actually a >couple things going on: [snip] Thanks for the explanation! At least now I have a workaround :) > 1) since you're using qq() to enclose the string containing your >config, [snip] It's in the docs that way. I guess the doc also needs to be updated. BTW, should the Buffer appender flush its buffer on DESTROY? It seems like when the script ends any messages still in the buffer are lost. I suppose this is the case for *any* trigger sub that doesn't always return true? -- Rob |