release 0.005001 (FINALLY!)
[p5sagit/Log-Contextual.git] / lib / Log / Contextual.pm
index 36fe2ad..4df4f46 100644 (file)
@@ -3,353 +3,295 @@ package Log::Contextual;
 use strict;
 use warnings;
 
-our $VERSION = '0.00101';
+our $VERSION = '0.005001';
+$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
 
-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 = (
-   @dlog, @log,
-   qw( set_logger with_logger )
-);
-
-our %EXPORT_TAGS = (
-   dlog => \@dlog,
-   log  => \@log,
-   all  => [@dlog, @log],
-);
-
-sub import {
-   my $package = shift;
-   die 'Log::Contextual does not have a default import list'
-      unless @_;
-
-   for my $idx ( 0 .. $#_ ) {
-      if ( $_[$idx] eq '-logger' ) {
-         set_logger($_[$idx + 1]);
-         splice @_, $idx, 2;
-         last;
-      }
-   }
-   $package->export_to_level(1, $package, @_);
+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(qw(____ set_logger with_logger ));
+
+export_tag dlog => ('____');
+export_tag log  => ('____');
+import_arguments qw(logger package_logger default_logger);
+
+sub router {
+   our $Router_Instance ||= do {
+      require Log::Contextual::Router;
+      Log::Contextual::Router->new
+     }
 }
 
-our $Get_Logger;
+sub default_import {
+   my ($class) = shift;
 
-sub set_logger {
-   my $logger = $_[0];
-   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 } }
-   }
-   $Get_Logger = $logger;
+   die 'Log::Contextual does not have a default import list';
+
+   ()
 }
 
-sub with_logger {
-   my $logger = $_[0];
-   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 } }
+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 before_import {
+   my ($class, $importer, $spec) = @_;
+   my $router      = $class->router;
+   my $exports     = $spec->exports;
+   my %router_args = (
+      exporter  => $class,
+      target    => $importer,
+      arguments => $spec->argument_info
+   );
+
+   my @tags = $class->default_import($spec)
+     if $spec->config->{default};
+
+   for (@tags) {
+      die "only tags are supported for defaults at this time"
+        unless $_ =~ /^:(.*)$/;
+
+      $spec->config->{$1} = 1;
    }
-   local $Get_Logger = $logger;
-   $_[1]->();
-}
 
+   $router->before_import(%router_args);
 
+   if ($exports->{'&set_logger'}) {
+      die ref($router) . " does not support set_logger()"
+        unless $router->does('Log::Contextual::Role::Router::SetLogger');
 
-sub log_trace (&@) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   $log->trace($code->(@_))
-      if $log->is_trace;
-   @_
-}
+      $spec->add_export('&set_logger', sub { $router->set_logger(@_) })
+   }
 
-sub log_debug (&@) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   $log->debug($code->(@_))
-      if $log->is_debug;
-   @_
-}
+   if ($exports->{'&with_logger'}) {
+      die ref($router) . " does not support with_logger()"
+        unless $router->does('Log::Contextual::Role::Router::WithLogger');
 
-sub log_info (&@) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   $log->info($code->(@_))
-      if $log->is_info;
-   @_
-}
+      $spec->add_export('&with_logger', sub { $router->with_logger(@_) })
+   }
 
-sub log_warn (&@) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   $log->warn($code->(@_))
-      if $log->is_warn;
-   @_
+   my @levels = @{$class->arg_levels($spec->config->{levels})};
+   for my $level (@levels) {
+      if ($spec->config->{log}) {
+         $spec->add_export(
+            "&log_$level",
+            sub (&@) {
+               my ($code, @args) = @_;
+               $router->handle_log_request(
+                  exporter       => $class,
+                  caller_package => scalar(caller),
+                  caller_level   => 1,
+                  message_level  => $level,
+                  message_sub    => $code,
+                  message_args   => \@args,
+               );
+               return @args;
+            });
+         $spec->add_export(
+            "&logS_$level",
+            sub (&@) {
+               my ($code, @args) = @_;
+               $router->handle_log_request(
+                  exporter       => $class,
+                  caller_package => scalar(caller),
+                  caller_level   => 1,
+                  message_level  => $level,
+                  message_sub    => $code,
+                  message_args   => \@args,
+               );
+               return $args[0];
+            });
+      }
+      if ($spec->config->{dlog}) {
+         $spec->add_export(
+            "&Dlog_$level",
+            sub (&@) {
+               my ($code, @args) = @_;
+               my $wrapped = sub {
+                  local $_ = (@_ ? Data::Dumper::Concise::Dumper @_ : '()');
+                  &$code;
+               };
+               $router->handle_log_request(
+                  exporter       => $class,
+                  caller_package => scalar(caller),
+                  caller_level   => 1,
+                  message_level  => $level,
+                  message_sub    => $wrapped,
+                  message_args   => \@args,
+               );
+               return @args;
+            });
+         $spec->add_export(
+            "&DlogS_$level",
+            sub (&$) {
+               my ($code, $ref) = @_;
+               my $wrapped = sub {
+                  local $_ = Data::Dumper::Concise::Dumper($_[0]);
+                  &$code;
+               };
+               $router->handle_log_request(
+                  exporter       => $class,
+                  caller_package => scalar(caller),
+                  caller_level   => 1,
+                  message_level  => $level,
+                  message_sub    => $wrapped,
+                  message_args   => [$ref],
+               );
+               return $ref;
+            });
+      }
+   }
 }
 
-sub log_error (&@) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   $log->error($code->(@_))
-      if $log->is_error;
-   @_
+sub after_import {
+   my ($class, $importer, $spec) = @_;
+   my %router_args = (
+      exporter  => $class,
+      target    => $importer,
+      arguments => $spec->argument_info
+   );
+   $class->router->after_import(%router_args);
 }
 
-sub log_fatal (&@) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   $log->fatal($code->(@_))
-      if $log->is_fatal;
-   @_
-}
+1;
 
+__END__
 
-sub logS_trace (&$) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   my $value = shift;
-   $log->trace($code->($value))
-      if $log->is_trace;
-   $value
-}
+=head1 NAME
 
-sub logS_debug (&$) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   my $value = shift;
-   $log->debug($code->($value))
-      if $log->is_debug;
-   $value
-}
+Log::Contextual - Simple logging interface with a contextual log
 
-sub logS_info (&$) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   my $value = shift;
-   $log->info($code->($value))
-      if $log->is_info;
-   $value
-}
+=head1 SYNOPSIS
 
-sub logS_warn (&$) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   my $value = shift;
-   $log->warn($code->($value))
-      if $log->is_warn;
-   $value
-}
+ use Log::Contextual qw( :log :dlog set_logger with_logger );
+ use Log::Contextual::SimpleLogger;
+ use Log::Log4perl ':easy';
+ Log::Log4perl->easy_init($DEBUG);
 
-sub logS_error (&$) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   my $value = shift;
-   $log->error($code->($value))
-      if $log->is_error;
-   $value
-}
+ my $logger  = Log::Log4perl->get_logger;
 
-sub logS_fatal (&$) {
-   my $log  = $Get_Logger->();
-   my $code = shift;
-   my $value = shift;
-   $log->fatal($code->($value))
-      if $log->is_fatal;
-   $value
-}
+ set_logger $logger;
 
+ log_debug { 'program started' };
 
+ sub foo {
 
-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
-}
+   my $minilogger = Log::Contextual::SimpleLogger->new({
+     levels => [qw( trace debug )]
+   });
 
-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
-}
+   my @args = @_;
 
-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
-}
+   with_logger $minilogger => sub {
+     log_trace { 'foo entered' };
+     my ($foo, $bar) = Dlog_trace { "params for foo: $_" } @args;
+     # ...
+     log_trace { 'foo left' };
+   };
+ }
 
-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
-}
+ foo();
 
-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
-}
+Beginning with version 1.008 L<Log::Dispatchouli> also works out of the box
+with C<Log::Contextual>:
 
-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
-}
+ use Log::Contextual qw( :log :dlog set_logger );
+ use Log::Dispatchouli;
+ my $ld = Log::Dispatchouli->new({
+    ident     => 'slrtbrfst',
+    to_stderr => 1,
+    debug     => 1,
+ });
 
+ set_logger $ld;
 
+ log_debug { 'program started' };
 
-sub DlogS_trace (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_trace {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
+=head1 DESCRIPTION
 
-sub DlogS_debug (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_debug {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
+Major benefits:
 
-sub DlogS_info (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_info {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
+=over 2
 
-sub DlogS_warn (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_warn {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
+=item * Efficient
 
-sub DlogS_error (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_error {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
+The logging functions take blocks, so if a log level is disabled, the
+block will not run:
 
-sub DlogS_fatal (&$) {
-  my $code = $_[0];
-  my $value = $_[1];
-  logS_fatal {
-     do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
-  } $value
-}
+ # the following won't run if debug is off
+ log_debug { "the new count in the database is " . $rs->count };
 
-1;
+Similarly, the C<D> prefixed methods only C<Dumper> the input if the level is
+enabled.
 
-__END__
+=item * Handy
 
-=head1 NAME
+The logging functions return their arguments, so you can stick them in
+the middle of expressions:
 
-Log::Contextual - Simple logging interface with a contextual log
+ for (log_debug { "downloading:\n" . join qq(\n), @_ } @urls) { ... }
 
-=head1 SYNOPSIS
+=item * Generic
 
- use Log::Contextual qw( :log :dlog set_logger with_logger );
- use Log::Contextual::SimpleLogger;
- use Log::Log4perl ':easy';
- Log::Log4perl->easy_init($DEBUG);
+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
 
- my $logger  = Log::Log4perl->get_logger;
+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
 
- set_logger $logger;
+=item * Scalable
 
- log_debug { 'program started' };
+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.
 
- sub foo {
-   with_logger(Log::Contextual::SimpleLogger->new({
-       levels => [qw( trace debug )]
-     }) => sub {
-     log_trace { 'foo entered' };
-     my ($foo, $bar) = Dlog_trace { "params for foo: $_" } @_;
-     # ...
-     log_trace { 'foo left' };
-   });
- }
+=back
 
- foo();
+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.
 
-=head1 DESCRIPTION
+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.
 
-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, take a look at L<Log::Dispatchouli>.
+=head1 IMPORT OPTIONS
 
-=head1 OPTIONS
+See L</SETTING DEFAULT IMPORT OPTIONS> for information on setting these project
+wide.
+
+=head2 -logger
 
 When you import this module you may use C<-logger> as a shortcut for
 L<set_logger>, for example:
@@ -365,17 +307,101 @@ case you might try something like the following:
  BEGIN { $var_log = VarLogger->new }
  use Log::Contextual qw( :dlog ), -logger => $var_log;
 
-=head1 A WORK IN PROGRESS
+=head2 -levels
 
-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:
+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;
+ use Log::Contextual
+   -levels => [qw( debug info notice warning error critical alert emergency )];
 
-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.
+=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 B<default> logger for the current package.
+
+Basically it sets the logger to be used if C<set_logger> is never called; so
+
+ package My::Package;
+ use Log::Contextual::SimpleLogger;
+ use Log::Contextual qw( :log ),
+   -default_logger => Log::Contextual::WarnLogger->new({
+      env_prefix => 'MY_PACKAGE'
+   });
+
+=head1 SETTING DEFAULT IMPORT OPTIONS
+
+Eventually you will get tired of writing the following in every single one of
+your packages:
+
+ use Log::Log4perl;
+ use Log::Log4perl ':easy';
+ BEGIN { Log::Log4perl->easy_init($DEBUG) }
+
+ use Log::Contextual -logger => Log::Log4perl->get_logger;
+
+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)] }
+ sub default_import { ':log' }
+
+ # 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)]>;
+
+Additionally, the C<default_import> method is what happens if a user tries to
+use your subclass with no arguments.  The default just dies, but if you'd like
+to change the default to import a tag merely return the tags you'd like to
+import.  So the following will all work:
+
+ sub default_import { ':log' }
+
+ sub default_import { ':dlog' }
+
+ sub default_import { qw(:dlog :log ) }
 
 =head1 FUNCTIONS
 
@@ -384,11 +410,13 @@ 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
-C<CodeRef> for you.
+C<CodeRef> for you.  C<set_logger> is really meant only to be called from a
+top-level script.  To avoid foot-shooting the function will warn if you call it
+more than once.
 
 =head2 with_logger
 
@@ -401,7 +429,7 @@ C<CodeRef> for you.
     }
  };
 
-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
@@ -413,7 +441,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 (&@) {
@@ -434,29 +462,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
-
- log_trace { 'entered method foo with args ' join q{,}, @args };
+Which functions are exported depends on what was passed to L</-levels>.  The
+default (no C<-levels> option passed) would export:
 
-=head3 log_debug
+=over 2
 
- log_debug { 'entered method foo' };
+=item log_trace
 
-=head3 log_info
+=item log_debug
 
- log_info { 'started process foo' };
+=item log_info
 
-=head3 log_warn
+=item log_warn
 
- log_warn { 'possible misconfiguration at line 10' };
+=item log_error
 
-=head3 log_error
+=item log_fatal
 
- log_error { 'non-numeric user input!' };
-
-=head3 log_fatal
-
- log_fatal { '1 is never equal to 0!' };
+=back
 
 =head2 logS_$level
 
@@ -495,29 +518,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
-
- Dlog_debug { "random data structure: $_" } { foo => $bar };
+Which functions are exported depends on what was passed to L</-levels>.  The
+default (no C<-levels> option passed) would export:
 
-=head3 Dlog_info
+=over 2
 
- return Dlog_info { "html from method returned: $_" } "<html>...</html>";
+=item Dlog_trace
 
-=head3 Dlog_warn
+=item Dlog_debug
 
- Dlog_warn { "probably invalid value: $_" } $foo;
+=item Dlog_info
 
-=head3 Dlog_error
+=item Dlog_warn
 
- Dlog_error { "non-numeric user input! ($_)" } $port;
+=item Dlog_error
 
-=head3 Dlog_fatal
+=item Dlog_fatal
 
- Dlog_fatal { '1 is never equal to 0!' } 'ZOMG ZOMG' if 1 == 0;
+=back
 
 =head2 DlogS_$level
 
@@ -532,6 +550,69 @@ 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.
+
+Here is a basic example of a logger that exploits C<caller> to reproduce the
+output of C<warn> with a logger:
+
+ my @caller_info;
+ my $var_log = Log::Contextual::SimpleLogger->new({
+    levels  => [qw(trace debug info warn error fatal)],
+    coderef => sub { chomp($_[0]); warn "$_[0] at $caller_info[1] line $caller_info[2].\n" }
+ });
+ my $warn_faker = sub {
+    my ($package, $args) = @_;
+    @caller_info = caller($args->{caller_level});
+    $var_log
+ };
+ set_logger($warn_faker);
+ log_debug { 'test' };
+
+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>.
+
+Note that it increases the caller level as it dispatches based on where
+the caller of the log function, not the log function itself.
+
+ 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} + 1);
+          $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
@@ -556,17 +637,34 @@ The first six merely need to return true if that level is enabled.  The latter
 six take the results of whatever the user returned from their coderef and log
 them.  For a basic example see L<Log::Contextual::SimpleLogger>.
 
+=head1 LOG ROUTING
+
+In between the loggers and the log functions is a log router that is responsible for
+finding a logger to handle the log event and passing the log information to the
+logger. This relationship is described in the documentation for C<Log::Contextual::Role::Router>.
+
+C<Log::Contextual> and packages that extend it will by default share a router singleton that
+implements the with_logger() and set_logger() functions and also respects the -logger,
+-package_logger, and -default_logger import options with their associated default value
+functions. The router singleton is available as the return value of the router() function. Users
+of Log::Contextual may overload router() to return instances of custom log routers that
+could for example work with loggers that use a different interface.
+
 =head1 AUTHOR
 
 frew - Arthur Axel "fREW" Schmidt <frioux@gmail.com>
 
+=head1 CONTRIBUTORS
+
+triddle - Tyler Riddle <t.riddle@shadowcat.co.uk>
+
 =head1 DESIGNER
 
 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