From: Matthew K. <mk...@ne...> - 2003-07-03 13:54:32
|
I want to be able to access the details of a given appender attached to = a logger. I can get the names of the appenders via = $logger->appender_names, but I would like to get access to the actual = appender details. Specifically I'm trying to get the filename which is = defined in a configuration file for a specific logger. I've been able = to hack it by adding the following line to the add_appender method in = Logger.pm $self->{appenders} =3D \%APPENDER_BY_NAME ; and then accessing it from the program for the logger Logfile by=20 $logger->{appenders}{Logfile}{appender}{filename} ; Is there a way of doing this that doesn't involve hacking the code ? If = not, would you consider adding this to the log4perl code ? |
From: Mike S. <log...@pe...> - 2003-07-03 18:50:19
|
On Thu, 3 Jul 2003, Matthew Keene wrote: > I want to be able to access the details of a given appender attached > to a logger. I can get the names of the appenders via > $logger->appender_names, but I would like to get access to the actual > appender details. Specifically I'm trying to get the filename which is > defined in a configuration file for a specific logger. I've been able > to hack it by adding the following line to the add_appender method in > Logger.pm > > $self->{appenders} = \%APPENDER_BY_NAME ; > > and then accessing it from the program for the logger Logfile by > > $logger->{appenders}{Logfile}{appender}{filename} ; > > Is there a way of doing this that doesn't involve hacking the code ? > If not, would you consider adding this to the log4perl code ? There's currently no 'official' way to do that, but Perl certainly lets you take a peek without hacking Log::Log4perl: %Log::Log4perl::Logger::APPENDER_BY_NAME is available. For better encapsulation, we should probably provide an appenders() method returning a ref to the hash, as in the patch below. Thoughts? -- Mike Mike Schilli log...@pe... http://perlmeister.com http://log4perl.sourceforge.net Index: Logger.pm =================================================================== RCS file: /cvsroot/log4perl/Log-Log4perl/lib/Log/Log4perl/Logger.pm,v retrieving revision 1.49 diff -a -u -r1.49 Logger.pm --- Logger.pm 10 Jun 2003 07:02:41 -0000 1.49 +++ Logger.pm 3 Jul 2003 18:49:22 -0000 @@ -456,6 +456,12 @@ } ################################################## +sub appenders { +################################################## + return \%APPENDER_BY_NAME; +} + +################################################## sub has_appenders { ################################################## my($self) = @_; |
From: Matthew K. <mk...@ne...> - 2003-07-03 22:09:09
|
>There's currently no 'official' way to do that, but Perl certainly >lets you take a peek without hacking Log::Log4perl: > %Log::Log4perl::Logger::APPENDER_BY_NAME You know, I must have had about six tries at doing that and couldn't get the package qualification right (although it seems pretty obvious now). Thanks very much. > For better encapsulation, we should probably provide > an appenders() method returning a ref to the hash, as in the patch below. > Thoughts? Definitely agree, in fact in one of the hacks that I did I created a get_appender_by_name method sub get_appender_by_name { my ($self, $appender_name) = @_ ; return $APPENDER_BY_NAME{$appender_name} ; } I don't know whether it's better from an OO interface standpoint to have access to the internal hash, or to be able to return individual entries (probably doesn't really matter, I'm certainly no OO bigot). Either way, I think it's a good (and easy) modification to have, especially where, as I said, the actual configuration happens outside your program. > > -- Mike > > Mike Schilli > log...@pe... > http://perlmeister.com > http://log4perl.sourceforge.net > > Index: Logger.pm > =================================================================== > RCS file: /cvsroot/log4perl/Log-Log4perl/lib/Log/Log4perl/Logger.pm,v > retrieving revision 1.49 > diff -a -u -r1.49 Logger.pm > --- Logger.pm 10 Jun 2003 07:02:41 -0000 1.49 > +++ Logger.pm 3 Jul 2003 18:49:22 -0000 > @@ -456,6 +456,12 @@ > } > > ################################################## > +sub appenders { > +################################################## > + return \%APPENDER_BY_NAME; > +} > + > +################################################## > sub has_appenders { > ################################################## > my($self) = @_; > |
From: Mike S. <log...@pe...> - 2003-07-07 00:20:47
|
On Fri, 4 Jul 2003, Matthew Keene wrote: > Definitely agree, in fact in one of the hacks that I did I created a > get_appender_by_name method > > sub get_appender_by_name { > my ($self, $appender_name) = @_ ; > return $APPENDER_BY_NAME{$appender_name} ; > } > > I don't know whether it's better from an OO interface standpoint to have > access to the internal hash, or to be able to return individual entries Good point. If we wanted to have it squeaky clean, however, there'd be a lot more to do (e.g. accessors for all fields in all Appender objects), and it's probably not worth it right now, so I kept the method returning a hashref which maps appender names to appender objects. I did make a minor modification to my previous post, though: I thought that it might be cleaner to put the method into Log::Log4perl, since the appenders returned are system- and not logger related. Here's the patch (to be part of 0.36): Index: Log4perl.pm =================================================================== RCS file: /cvsroot/log4perl/Log-Log4perl/lib/Log/Log4perl.pm,v retrieving revision 1.134 retrieving revision 1.136 diff -a -u -r1.134 -r1.136 --- Log4perl.pm 25 Jun 2003 23:42:52 -0000 1.134 +++ Log4perl.pm 7 Jul 2003 00:09:02 -0000 1.136 @@ -13,7 +13,7 @@ use constant DEBUG => 1; -our $VERSION = '0.35'; +our $VERSION = '0.36'; # set this to '1' if you're using a wrapper # around Log::Log4perl @@ -280,6 +280,12 @@ return Log::Log4perl::Logger->get_logger(@args); } +################################################## +sub appenders { # Get all defined appenders hashref +################################################## + return \%Log::Log4perl::Logger::APPENDER_BY_NAME; +} + 1; __END__ @@ -1856,6 +1862,23 @@ of C<$Log::Log4perl::caller_depth> (defaults to 0) by one for every wrapper that's in between your application and C<Log::Log4perl>, then C<Log::Log4perl> will compensate for the difference. + +=head1 Access to Internals + +The following methods are only of use if you want to peek/poke in +the internals of Log::Log4perl. Be careful not to disrupt its +inner workings. + +=over 4 + +=item C<< Log::Log4perl->appenders() >> + +To find out which appenders are currently defined (not only +for a particular logger, but overall), a C<appenders()> +method is available to return a reference to a hash mapping appender +names to their Log::Log4perl::Appender object references. + +=back =head1 EXAMPLE I've also added a test case in t/002 and a comment in Changes. -- Mike Mike Schilli log...@pe... http://perlmeister.com http://log4perl.sourceforge.net |