pass more information to logger
[p5sagit/Log-Contextual.git] / lib / Log / Contextual.pm
index f4c81a6..495781e 100644 (file)
@@ -3,63 +3,97 @@ package Log::Contextual;
 use strict;
 use warnings;
 
-our $VERSION = '0.00201';
+our $VERSION = '0.004100';
 
-require Exporter;
+my @levels = qw(debug trace warn info error fatal);
+
+use Exporter::Declare;
+use Exporter::Declare::Export::Generator;
 use Data::Dumper::Concise;
 use Scalar::Util 'blessed';
 
-BEGIN { our @ISA = qw(Exporter) }
-
-my @dlog = (qw(
-   Dlog_debug DlogS_debug
-   Dlog_trace DlogS_trace
-   Dlog_warn DlogS_warn
-   Dlog_info DlogS_info
-   Dlog_error DlogS_error
-   Dlog_fatal DlogS_fatal
- ));
-
-my @log = (qw(
-   log_debug logS_debug
-   log_trace logS_trace
-   log_warn logS_warn
-   log_info logS_info
-   log_error logS_error
-   log_fatal logS_fatal
- ));
-
-our @EXPORT_OK = (
+my @dlog = ((map "Dlog_$_", @levels), (map "DlogS_$_", @levels));
+
+my @log = ((map "log_$_", @levels), (map "logS_$_", @levels));
+
+eval {
+   require Log::Log4perl;
+   die if $Log::Log4perl::VERSION < 1.29;
+   Log::Log4perl->wrapper_register(__PACKAGE__)
+};
+
+# ____ is because tags must have at least one export and we don't want to
+# export anything but the levels selected
+sub ____ {}
+
+exports ('____',
    @dlog, @log,
    qw( set_logger with_logger )
 );
 
-our %EXPORT_TAGS = (
-   dlog => \@dlog,
-   log  => \@log,
-   all  => [@dlog, @log],
-);
+export_tag dlog => ('____');
+export_tag log  => ('____');
+import_arguments qw(logger package_logger default_logger);
+
+sub before_import {
+   my ($class, $importer, $spec) = @_;
 
-sub import {
-   my $package = shift;
    die 'Log::Contextual does not have a default import list'
-      unless @_;
-
-   for my $idx ( 0 .. $#_ ) {
-      my $val = $_[$idx];
-      if ( defined $val && $val eq '-logger' ) {
-         set_logger($_[$idx + 1]);
-         splice @_, $idx, 2;
-      } elsif ( defined $val && $val eq '-default_logger' ) {
-         _set_default_logger_for(scalar caller, $_[$idx + 1]);
-         splice @_, $idx, 2;
+      if $spec->config->{default};
+
+   my @levels = @{$class->arg_levels($spec->config->{levels})};
+   for my $level (@levels) {
+      if ($spec->config->{log}) {
+         $spec->add_export("&log_$level", sub (&@) {
+            _do_log( $level => _get_logger( caller ), shift @_, @_)
+         });
+         $spec->add_export("&logS_$level", sub (&@) {
+            _do_logS( $level => _get_logger( caller ), $_[0], $_[1])
+         });
+      }
+      if ($spec->config->{dlog}) {
+         $spec->add_export("&Dlog_$level", sub (&@) {
+           my ($code, @args) = @_;
+           return _do_log( $level => _get_logger( caller ), sub {
+              local $_ = (@args?Data::Dumper::Concise::Dumper @args:'()');
+              $code->(@_)
+           }, @args );
+         });
+         $spec->add_export("&DlogS_$level", sub (&$) {
+           my ($code, $ref) = @_;
+           _do_logS( $level => _get_logger( caller ), sub {
+              local $_ = Data::Dumper::Concise::Dumper $ref;
+              $code->($ref)
+           }, $ref )
+         });
       }
    }
-   $package->export_to_level(1, $package, @_);
+}
+
+sub arg_logger { $_[1] }
+sub arg_levels { $_[1] || [qw(debug trace warn info error fatal)] }
+sub arg_package_logger { $_[1] }
+sub arg_default_logger { $_[1] }
+
+sub after_import {
+   my ($class, $importer, $specs) = @_;
+
+   if (my $l = $class->arg_logger($specs->config->{logger})) {
+      set_logger($l)
+   }
+
+   if (my $l = $class->arg_package_logger($specs->config->{package_logger})) {
+      _set_package_logger_for($importer, $l)
+   }
+
+   if (my $l = $class->arg_default_logger($specs->config->{default_logger})) {
+      _set_default_logger_for($importer, $l)
+   }
 }
 
 our $Get_Logger;
 our %Default_Logger;
+our %Package_Logger;
 
 sub _set_default_logger_for {
    my $logger = $_[1];
@@ -71,13 +105,24 @@ sub _set_default_logger_for {
    $Default_Logger{$_[0]} = $logger
 }
 
+sub _set_package_logger_for {
+   my $logger = $_[1];
+   if(ref $logger ne 'CODE') {
+      die 'logger was not a CodeRef or a logger object.  Please try again.'
+         unless blessed($logger);
+      $logger = do { my $l = $logger; sub { $l } }
+   }
+   $Package_Logger{$_[0]} = $logger
+}
+
 sub _get_logger($) {
    my $package = shift;
    (
+      $Package_Logger{$package} ||
       $Get_Logger ||
       $Default_Logger{$package} ||
       die q( no logger set!  you can't try to log something without a logger! )
-   )->($package);
+   )->($package, { caller_level => 3 });
 }
 
 sub set_logger {
@@ -104,247 +149,28 @@ sub with_logger {
    $_[1]->();
 }
 
+sub _do_log {
+   my $level  = shift;
+   my $logger = shift;
+   my $code   = shift;
+   my @values = @_;
 
-
-sub log_trace (&@) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->trace($code->(@_))
-      if $log->is_trace;
-   @_
+   $logger->$level($code->(@_))
+      if $logger->${\"is_$level"};
+   @values
 }
 
-sub log_debug (&@) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->debug($code->(@_))
-      if $log->is_debug;
-   @_
-}
-
-sub log_info (&@) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->info($code->(@_))
-      if $log->is_info;
-   @_
-}
-
-sub log_warn (&@) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->warn($code->(@_))
-      if $log->is_warn;
-   @_
-}
-
-sub log_error (&@) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->error($code->(@_))
-      if $log->is_error;
-   @_
-}
-
-sub log_fatal (&@) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->fatal($code->(@_))
-      if $log->is_fatal;
-   @_
-}
+sub _do_logS {
+   my $level  = shift;
+   my $logger = shift;
+   my $code   = shift;
+   my $value  = shift;
 
-
-sub logS_trace (&$) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   my $value = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->trace($code->($value))
-      if $log->is_trace;
-   $value
-}
-
-sub logS_debug (&$) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   my $value = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->debug($code->($value))
-      if $log->is_debug;
+   $logger->$level($code->($value))
+      if $logger->${\"is_$level"};
    $value
 }
 
-sub logS_info (&$) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   my $value = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->info($code->($value))
-      if $log->is_info;
-   $value
-}
-
-sub logS_warn (&$) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   my $value = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->warn($code->($value))
-      if $log->is_warn;
-   $value
-}
-
-sub logS_error (&$) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   my $value = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->error($code->($value))
-      if $log->is_error;
-   $value
-}
-
-sub logS_fatal (&$) {
-   my $log  = _get_logger( caller );
-   my $code = shift;
-   my $value = shift;
-   local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1;
-   $log->fatal($code->($value))
-      if $log->is_fatal;
-   $value
-}
-
-
-
-sub Dlog_trace (&@) {
-  my $code = shift;
-  my @values = @_;
-  return log_trace {
-     if (@values) {
-        do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
-     } else {
-        do { local $_ = '()'; $code->() };
-     }
-  } @values
-}
-
-sub Dlog_debug (&@) {
-  my $code = shift;
-  my @values = @_;
-  log_debug {
-     if (@values) {
-        do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
-     } else {
-        do { local $_ = '()'; $code->() };
-     }
-  } @values
-}
-
-sub Dlog_info (&@) {
-  my $code = shift;
-  my @values = @_;
-  log_info {
-     if (@values) {
-        do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
-     } else {
-        do { local $_ = '()'; $code->() };
-     }
-  } @values
-}
-
-sub Dlog_warn (&@) {
-  my $code = shift;
-  my @values = @_;
-  log_warn {
-     if (@values) {
-        do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
-     } else {
-        do { local $_ = '()'; $code->() };
-     }
-  } @values
-}
-
-sub Dlog_error (&@) {
-  my $code = shift;
-  my @values = @_;
-  log_error {
-     if (@values) {
-        do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
-     } else {
-        do { local $_ = '()'; $code->() };
-     }
-  } @values
-}
-
-sub Dlog_fatal (&@) {
-  my $code = shift;
-  my @values = @_;
-  log_fatal {
-     if (@values) {
-        do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
-     } else {
-        do { local $_ = '()'; $code->() };
-     }
-  } @values
-}
-
-
-
-sub DlogS_trace (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_trace {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
-
-sub DlogS_debug (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_debug {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
-
-sub DlogS_info (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_info {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
-
-sub DlogS_warn (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_warn {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
-
-sub DlogS_error (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_error {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
-
-sub DlogS_fatal (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_fatal {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
-
 1;
 
 __END__
@@ -360,7 +186,6 @@ Log::Contextual - Simple logging interface with a contextual log
  use Log::Log4perl ':easy';
  Log::Log4perl->easy_init($DEBUG);
 
-
  my $logger  = Log::Log4perl->get_logger;
 
  set_logger $logger;
@@ -368,14 +193,17 @@ Log::Contextual - Simple logging interface with a contextual log
  log_debug { 'program started' };
 
  sub foo {
-   with_logger(Log::Contextual::SimpleLogger->new({
-       levels => [qw( trace debug )]
-     }) => sub {
+
+   my $minilogger = Log::Contextual::SimpleLogger->new({
+     levels => [qw( trace debug )]
+   });
+
+   with_logger $minilogger => sub {
      log_trace { 'foo entered' };
      my ($foo, $bar) = Dlog_trace { "params for foo: $_" } @_;
      # ...
      log_trace { 'foo left' };
-   });
+   };
  }
 
  foo();
@@ -397,12 +225,72 @@ with C<Log::Contextual>:
 
 =head1 DESCRIPTION
 
-This module is a simple interface to extensible logging.  It is bundled with a
-really basic logger, L<Log::Contextual::SimpleLogger>, but in general you
-should use a real logger instead of that.  For something more serious but not
-overly complicated, try L<Log::Dispatchouli> (see L</SYNOPSIS> for example.)
+Major benefits:
+
+=over 2
+
+=item * Efficient
+
+The logging functions take blocks, so if a log level is disabled, the
+block will not run:
+
+ # the following won't run if debug is off
+ log_debug { "the new count in the database is " . $rs->count };
+
+Similarly, the C<D> prefixed methods only C<Dumper> the input if the level is
+enabled.
+
+=item * Handy
+
+The logging functions return their arguments, so you can stick them in
+the middle of expressions:
+
+ for (log_debug { "downloading:\n" . join qq(\n), @_ } @urls) { ... }
+
+=item * Generic
+
+C<Log::Contextual> is an interface for all major loggers.  If you log through
+C<Log::Contextual> you will be able to swap underlying loggers later.
+
+=item * Powerful
+
+C<Log::Contextual> chooses which logger to use based on L<< user defined C<CodeRef>s|/LOGGER CODEREF>>.
+Normally you don't need to know this, but you can take advantage of it when you
+need to later
+
+=item * Scalable
 
-=head1 OPTIONS
+If you just want to add logging to your extremely basic application, start with
+L<Log::Contextual::SimpleLogger> and then as your needs grow you can switch to
+L<Log::Dispatchouli> or L<Log::Dispatch> or L<Log::Log4perl> or whatever else.
+
+=back
+
+This module is a simple interface to extensible logging.  It exists to
+abstract your logging interface so that logging is as painless as possible,
+while still allowing you to switch from one logger to another.
+
+It is bundled with a really basic logger, L<Log::Contextual::SimpleLogger>,
+but in general you should use a real logger instead of that.  For something
+more serious but not overly complicated, try L<Log::Dispatchouli> (see
+L</SYNOPSIS> for example.)
+
+=head1 A WORK IN PROGRESS
+
+This module is certainly not complete, but we will not break the interface
+lightly, so I would say it's safe to use in production code.  The main result
+from that at this point is that doing:
+
+ use Log::Contextual;
+
+will die as we do not yet know what the defaults should be.  If it turns out
+that nearly everyone uses the C<:log> tag and C<:dlog> is really rare, we'll
+probably make C<:log> the default.  But only time and usage will tell.
+
+=head1 IMPORT OPTIONS
+
+See L</SETTING DEFAULT IMPORT OPTIONS> for information on setting these project
+wide.
 
 =head2 -logger
 
@@ -420,10 +308,39 @@ case you might try something like the following:
  BEGIN { $var_log = VarLogger->new }
  use Log::Contextual qw( :dlog ), -logger => $var_log;
 
+=head2 -levels
+
+The C<-levels> import option allows you to define exactly which levels your
+logger supports.  So the default,
+C<< [qw(debug trace warn info error fatal)] >>, works great for
+L<Log::Log4perl>, but it doesn't support the levels for L<Log::Dispatch>.  But
+supporting those levels is as easy as doing
+
+ use Log::Contextual
+   -levels => [qw( debug info notice warning error critical alert emergency )];
+
+=head2 -package_logger
+
+The C<-package_logger> import option is similar to the C<-logger> import option
+except C<-package_logger> sets the the logger for the current package.
+
+Unlike L</-default_logger>, C<-package_logger> cannot be overridden with
+L</set_logger>.
+
+ package My::Package;
+ use Log::Contextual::SimpleLogger;
+ use Log::Contextual qw( :log ),
+   -package_logger => Log::Contextual::WarnLogger->new({
+      env_prefix => 'MY_PACKAGE'
+   });
+
+If you are interested in using this package for a module you are putting on
+CPAN we recommend L<Log::Contextual::WarnLogger> for your package logger.
+
 =head2 -default_logger
 
 The C<-default_logger> import option is similar to the C<-logger> import option
-except C<-default_logger> sets the the default logger for the current package.
+except C<-default_logger> sets the the B<default> logger for the current package.
 
 Basically it sets the logger to be used if C<set_logger> is never called; so
 
@@ -434,20 +351,46 @@ Basically it sets the logger to be used if C<set_logger> is never called; so
       env_prefix => 'MY_PACKAGE'
    });
 
-If you are interested in using this package for a module you are putting on
-CPAN we recommend L<Log::Contextual::WarnLogger> for your default logger.
+=head1 SETTING DEFAULT IMPORT OPTIONS
 
-=head1 A WORK IN PROGRESS
+Eventually you will get tired of writing the following in every single one of
+your packages:
 
-This module is certainly not complete, but we will not break the interface
-lightly, so I would say it's safe to use in production code.  The main result
-from that at this point is that doing:
+ use Log::Log4perl;
+ use Log::Log4perl ':easy';
+ BEGIN { Log::Log4perl->easy_init($DEBUG) }
 
- use Log::Contextual;
+ use Log::Contextual -logger => Log::Log4perl->get_logger;
 
-will die as we do not yet know what the defaults should be.  If it turns out
-that nearly everyone uses the C<:log> tag and C<:dlog> is really rare, we'll
-probably make C<:log> the default.  But only time and usage will tell.
+You can set any of the import options for your whole project if you define your
+own C<Log::Contextual> subclass as follows:
+
+ package MyApp::Log::Contextual;
+
+ use base 'Log::Contextual';
+
+ use Log::Log4perl ':easy';
+ Log::Log4perl->easy_init($DEBUG)
+
+ sub arg_default_logger { $_[1] || Log::Log4perl->get_logger }
+ sub arg_levels { [qw(debug trace warn info error fatal custom_level)] }
+
+ # or maybe instead of default_logger
+ sub arg_package_logger { $_[1] }
+
+ # and almost definitely not this, which is only here for completeness
+ sub arg_logger { $_[1] }
+
+Note the C<< $_[1] || >> in C<arg_default_logger>.  All of these methods are
+passed the values passed in from the arguments to the subclass, so you can
+either throw them away, honor them, die on usage, or whatever.  To be clear,
+if you define your subclass, and someone uses it as follows:
+
+ use MyApp::Log::Contextual -default_logger => $foo,
+                            -levels => [qw(bar baz biff)];
+
+Your C<arg_default_logger> method will get C<$foo> and your C<arg_levels>
+will get C<[qw(bar baz biff)]>;
 
 =head1 FUNCTIONS
 
@@ -456,7 +399,7 @@ probably make C<:log> the default.  But only time and usage will tell.
  my $logger = WarnLogger->new;
  set_logger $logger;
 
-Arguments: C<Ref|CodeRef $returning_logger>
+Arguments: L</LOGGER CODEREF>
 
 C<set_logger> will just set the current logger to whatever you pass it.  It
 expects a C<CodeRef>, but if you pass it something else it will wrap it in a
@@ -475,7 +418,7 @@ more than once.
     }
  };
 
-Arguments: C<Ref|CodeRef $returning_logger, CodeRef $to_execute>
+Arguments: L</LOGGER CODEREF>, C<CodeRef $to_execute>
 
 C<with_logger> sets the logger for the scope of the C<CodeRef> C<$to_execute>.
 As with L</set_logger>, C<with_logger> will wrap C<$returning_logger> with a
@@ -487,7 +430,7 @@ Import Tag: C<:log>
 
 Arguments: C<CodeRef $returning_message, @args>
 
-All of the following six functions work the same except that a different method
+C<log_$level> functions all work the same except that a different method
 is called on the underlying C<$logger> object.  The basic pattern is:
 
  sub log_$level (&@) {
@@ -508,29 +451,24 @@ ways, but often it's convenient just for partial inspection of passthrough data
 If you want complete inspection of passthrough data, take a look at the
 L</Dlog_$level> functions.
 
-=head3 log_trace
+Which functions are exported depends on what was passed to L</-levels>.  The
+default (no C<-levels> option passed) would export:
 
- log_trace { 'entered method foo with args ' join q{,}, @args };
+=over 2
 
-=head3 log_debug
+=item log_trace
 
- log_debug { 'entered method foo' };
+=item log_debug
 
-=head3 log_info
+=item log_info
 
- log_info { 'started process foo' };
+=item log_warn
 
-=head3 log_warn
+=item log_error
 
- log_warn { 'possible misconfiguration at line 10' };
+=item log_fatal
 
-=head3 log_error
-
- log_error { 'non-numeric user input!' };
-
-=head3 log_fatal
-
- log_fatal { '1 is never equal to 0!' };
+=back
 
 =head2 logS_$level
 
@@ -569,29 +507,24 @@ and the output might look something like:
  "fRUE"
  "fiSMBoC"
 
-=head3 Dlog_trace
-
- my ($foo, $bar) = Dlog_trace { "entered method foo with args: $_" } @_;
-
-=head3 Dlog_debug
+Which functions are exported depends on what was passed to L</-levels>.  The
+default (no C<-levels> option passed) would export:
 
- Dlog_debug { "random data structure: $_" } { foo => $bar };
+=over 2
 
-=head3 Dlog_info
+=item Dlog_trace
 
- return Dlog_info { "html from method returned: $_" } "<html>...</html>";
+=item Dlog_debug
 
-=head3 Dlog_warn
+=item Dlog_info
 
- Dlog_warn { "probably invalid value: $_" } $foo;
+=item Dlog_warn
 
-=head3 Dlog_error
+=item Dlog_error
 
- Dlog_error { "non-numeric user input! ($_)" } $port;
+=item Dlog_fatal
 
-=head3 Dlog_fatal
-
- Dlog_fatal { '1 is never equal to 0!' } 'ZOMG ZOMG' if 1 == 0;
+=back
 
 =head2 DlogS_$level
 
@@ -606,6 +539,50 @@ slurping up (and also setting C<wantarray>) all the C<@args>
  my $pals_rs = DlogS_debug { "pals resultset: $_" }
    $schema->resultset('Pals')->search({ perlers => 1 });
 
+=head1 LOGGER CODEREF
+
+Anywhere a logger object can be passed, a coderef is accepted.  This is so
+that the user can use different logger objects based on runtime information.
+The logger coderef is passed the package of the caller the caller level the
+coderef needs to use if it wants more caller information.  The latter is in
+a hashref to allow for more options in the future.
+
+The following is an example that uses the information passed to the logger
+coderef.  It sets the global logger to C<$l3>, the logger for the C<A1>
+package to C<$l1>, except the C<lol> method in C<A1> which uses the C<$l2>
+logger and lastly the logger for the C<A2> package to C<$l2>.
+
+ my $complex_dispatcher = do {
+
+    my $l1 = ...;
+    my $l2 = ...;
+    my $l3 = ...;
+
+    my %registry = (
+       -logger => $l3,
+       A1 => {
+          -logger => $l1,
+          lol     => $l2,
+       },
+       A2 => { -logger => $l2 },
+    );
+
+    sub {
+       my ( $package, $info ) = @_;
+
+       my $logger = $registry{'-logger'};
+       if (my $r = $registry{$package}) {
+          $logger = $r->{'-logger'} if $r->{'-logger'};
+          my (undef, undef, undef, $sub) = caller($info->{caller_level});
+          $sub =~ s/^\Q$package\E:://g;
+          $logger = $r->{$sub} if $r->{$sub};
+       }
+       return $logger;
+    }
+ };
+
+ set_logger $complex_dispatcher;
+
 =head1 LOGGER INTERFACE
 
 Because this module is ultimately pretty looking glue (glittery?) with the
@@ -640,7 +617,7 @@ mst - Matt S. Trout <mst@shadowcat.co.uk>
 
 =head1 COPYRIGHT
 
-Copyright (c) 2010 the Log::Contextual L</AUTHOR> and L</DESIGNER> as listed
+Copyright (c) 2012 the Log::Contextual L</AUTHOR> and L</DESIGNER> as listed
 above.
 
 =head1 LICENSE