Permalink
Browse files

Added test case for get_logger() with a ref() on the actual object in…

…stead

of on a static category. Updated docs.
  • Loading branch information...
1 parent b7d58e1 commit 1ceda57198f2e24760ec49c42be3e3a1027bf281 @mschilli committed Aug 21, 2011
Showing with 74 additions and 8 deletions.
  1. +2 −0 Changes
  2. +28 −8 lib/Log/Log4perl.pm
  3. +44 −0 t/064RealClass.t
View
@@ -4,6 +4,8 @@
1.34 (not yet released)
* (ms) InternalDebug now replaces all instances of INTERNAL_DEBUG,
not just the first one.
+ * (ms) Added test case for get_logger() with a ref() on the actual
+ object instead of on a static category. Updated docs.
1.33 (2011/05/31)
* (ms) [RT 67132] Applied patch by Darin McBride to allow for
View
@@ -1490,14 +1490,34 @@ to obtain a logger of the category matching the
I<actual> class of the object, like in
# ... in Bar::new() ...
- my $logger = Log::Log4perl::get_logger($class);
-
-This way, you'll make sure the logger logs appropriately,
-no matter if the method is inherited or called directly.
-C<new()> always gets the
-real class name as an argument and all other methods can determine it
-via C<ref($self)>), so it shouldn't be a problem to get the right class
-every time.
+ my $logger = Log::Log4perl::get_logger( $class );
+
+In a method other than the constructor, the class name of the actual
+object can be obtained by calling C<ref()> on the object reference, so
+
+ package BaseClass;
+ use Log::Log4perl qw( get_logger );
+
+ sub new {
+ bless {}, shift;
+ }
+
+ sub method {
+ my( $self ) = @_;
+
+ get_logger( ref $self )->debug( "message" );
+ }
+
+ package SubClass;
+ our @ISA = qw(BaseClass);
+
+is the recommended pattern to make sure that
+
+ my $sub = SubClass->new();
+ $sub->meth();
+
+starts logging if the C<"SubClass"> category
+(and not the C<"BaseClass"> category has logging enabled at the DEBUG level.
=head2 Initialize once and only once
View
@@ -0,0 +1,44 @@
+# get_logger($self) in the base class returns a logger for the subclass
+# category
+
+use strict;
+use Test::More;
+use Log::Log4perl::Appender::TestBuffer;
+
+plan tests => 1;
+
+package AppBaseClass;
+use Log::Log4perl qw(get_logger :easy);
+sub meth {
+ my( $self ) = @_;
+ get_logger( ref $self )->warn("in base class");
+}
+
+package AppSubClass;
+our @ISA = qw(AppBaseClass);
+use Log::Log4perl qw(get_logger :easy);
+sub new {
+ bless {}, shift;
+}
+
+package main;
+
+use Log::Log4perl qw(get_logger :easy);
+
+# $Log::Log4perl::CHATTY_DESTROY_METHODS = 1;
+
+my $conf = q(
+log4perl.category.AppSubClass = WARN, LogBuffer
+log4perl.appender.LogBuffer = Log::Log4perl::Appender::TestBuffer
+log4perl.appender.LogBuffer.layout = Log::Log4perl::Layout::PatternLayout
+log4perl.appender.LogBuffer.layout.ConversionPattern = %m%n
+);
+
+Log::Log4perl::init(\$conf);
+
+my $buffer = Log::Log4perl::Appender::TestBuffer->by_name("LogBuffer");
+
+my $sub = AppSubClass->new();
+$sub->meth();
+
+is $buffer->buffer(), "in base class\n", "subclass logger in base class";

0 comments on commit 1ceda57

Please sign in to comment.