From: Kevin G. <ke...@go...> - 2002-11-07 16:58:53
|
Excellent solution. I had started one yesterday myself but yours is way better. But check out the attached test. Without the syslog appender it works fine, but with the syslog appender it gives this error message for the ->fatal() level (the other levels work fine). syslog: invalid level/facility: EMERG at /usr/local/lib/perl5/site_perl/5.6.1/Log/Dispatch/Syslog.pm line 78 Is this a known problem with Log::Dispatch::Syslog, or what? Mike Schilli wrote: > Hi there, > > here's a patch to review for the problem of Log4perl passing along messages to the Log::Dispatch > appenders with priority DEBUG only. > > What I did was define a mapping between Log4perl priorities/levels and Log::Dispatch's, put it into > a hash (%L4P_TO_LD) in Log::Log4perl::Levels and extend the level-generating function with one > additional parameter, which is the equivalent Log::Dispatch priority of the Log4perl priority to be > defined. This way, all Log::Dispatch appenders are initialized with min_level=debug (no change), but > the loggers will figure out at runtime (hash-lookup) what Log::Dispatch priority corresponds to the > current message's level and pass that on the Log::Dispatch logger when it comes down to actually > logging the message. > I've defined a test case also, but it doesn't show up yet, because I haven't checked in the file > yet. It's getting kind of late, so let me submit this to the list before I check it in, just to make > sure I haven't overlooked anything stupid :) > > -- Mike > -------------------------------------------------------- > Mike Schilli log...@pe... http://perlmeister.com > -------------------------------------------------------- > > > ------------------------------------------------------------------------ > > ? t/030LDLevel.t > Index: lib/Log/Log4perl/Appender.pm > =================================================================== > RCS file: /cvsroot/log4perl/Log-Log4perl/lib/Log/Log4perl/Appender.pm,v > retrieving revision 1.14 > diff -a -u -r1.14 Appender.pm > --- lib/Log/Log4perl/Appender.pm 23 Oct 2002 20:19:45 -0000 1.14 > +++ lib/Log/Log4perl/Appender.pm 7 Nov 2002 09:51:36 -0000 > @@ -60,8 +60,10 @@ > } > > my $appender = $appenderclass->new( > - # Set min_level to default, *we* are controlling this now > - min_level => 'debug', > + # Set min_level to the lowest setting. *we* are > + # controlling this now, the appender should just > + # log it with no questions asked. > + min_level => 'debug', > # Set 'name' and other parameters > map { $_ => $params{$_} } keys %params, > ); > Index: lib/Log/Log4perl/Level.pm > =================================================================== > RCS file: /cvsroot/log4perl/Log-Log4perl/lib/Log/Log4perl/Level.pm,v > retrieving revision 1.10 > diff -a -u -r1.10 Level.pm > --- lib/Log/Log4perl/Level.pm 18 Sep 2002 04:33:28 -0000 1.10 > +++ lib/Log/Log4perl/Level.pm 7 Nov 2002 09:51:37 -0000 > @@ -24,24 +24,44 @@ > our %PRIORITY = (); # unless (%PRIORITY); > our %LEVELS = () unless (%LEVELS); > our %SYSLOG = () unless (%SYSLOG); > +our %L4P_TO_LD = () unless (%L4P_TO_LD); > > sub add_priority { > - my ($prio, $intval, $syslog) = @_; > + my ($prio, $intval, $syslog, $log_dispatch_level) = @_; > $prio = uc($prio); # just in case; > > - $PRIORITY{$prio} = $intval; > - $LEVELS{$intval} = $prio; > - $SYSLOG{$prio} = $syslog if defined($syslog); > + $PRIORITY{$prio} = $intval; > + $LEVELS{$intval} = $prio; > + > + # Set up the mapping between Log4perl integer levels and > + # Log::Dispatch levels > + # Note: Log::Dispatch uses the following levels: > + # 0 debug > + # 1 info > + # 2 notice > + # 3 warning > + # 4 error > + # 5 critical > + # 6 alert > + # 7 emergency > + > + # The equivalent Log::Dispatch level is optional, set it to > + # the highest value (7=emerg) if it's not provided. > + $log_dispatch_level = 7 unless defined $log_dispatch_level; > + > + $L4P_TO_LD{$prio} = $log_dispatch_level; > + > + $SYSLOG{$prio} = $syslog if defined($syslog); > } > > # create the basic priorities > -add_priority("OFF", OFF_INT, -1); > -add_priority("FATAL", FATAL_INT, 0); > -add_priority("ERROR", ERROR_INT, 3); > -add_priority("WARN", WARN_INT, 4); > -add_priority("INFO", INFO_INT, 6); > -add_priority("DEBUG", DEBUG_INT, 7); > -add_priority("ALL", ALL_INT, 7); > +add_priority("OFF", OFF_INT, -1, 7); > +add_priority("FATAL", FATAL_INT, 0, 7); > +add_priority("ERROR", ERROR_INT, 3, 4); > +add_priority("WARN", WARN_INT, 4, 3); > +add_priority("INFO", INFO_INT, 6, 1); > +add_priority("DEBUG", DEBUG_INT, 7, 0); > +add_priority("ALL", ALL_INT, 7, 0); > > # we often sort numerically, so a helper func for readability > sub numerically {$a <=> $b} > Index: lib/Log/Log4perl/Logger.pm > =================================================================== > RCS file: /cvsroot/log4perl/Log-Log4perl/lib/Log/Log4perl/Logger.pm,v > retrieving revision 1.36 > diff -a -u -r1.36 Logger.pm > --- lib/Log/Log4perl/Logger.pm 15 Oct 2002 23:17:48 -0000 1.36 > +++ lib/Log/Log4perl/Logger.pm 7 Nov 2002 09:51:39 -0000 > @@ -205,7 +205,7 @@ > \$appender->log( > #these get passed through to Log::Dispatch > { name => \$appender_name, > - level => 0, > + level => \$Log::Log4perl::Level::L4P_TO_LD{\$level}, > message => \$message, > }, > #these we need > Index: lib/Log/Log4perl/TestBuffer.pm > =================================================================== > RCS file: /cvsroot/log4perl/Log-Log4perl/lib/Log/Log4perl/TestBuffer.pm,v > retrieving revision 1.5 > diff -a -u -r1.5 TestBuffer.pm > --- lib/Log/Log4perl/TestBuffer.pm 23 Sep 2002 16:45:43 -0000 1.5 > +++ lib/Log/Log4perl/TestBuffer.pm 7 Nov 2002 09:51:39 -0000 > @@ -10,7 +10,8 @@ > use base qw( Log::Dispatch::Output ); > use fields qw( stderr ); > > -our %POPULATION = (); > +our %POPULATION = (); > +our $LOG_PRIORITY = 0; > > ################################################## > sub new { > @@ -36,6 +37,7 @@ > my $self = shift; > my %params = @_; > > + $self->{buffer} .= "[$params{level}]: " if $LOG_PRIORITY; > $self->{buffer} .= $params{message}; > } > -- Happy Trails . . . Kevin M. Goess (and Anne and Frank) 904 Carmel Ave. Albany, CA 94706 (510) 525-5217 |