a bit of doc cleanup
[p5sagit/Log-Contextual.git] / lib / Log / Contextual.pm
index b33097d..8133226 100644 (file)
@@ -1,10 +1,10 @@
 package Log::Contextual;
 
+# ABSTRACT: Simple logging interface with a contextual log
+
 use strict;
 use warnings;
 
-our $VERSION = '0.004202';
-
 my @levels = qw(debug trace warn info error fatal);
 
 use Exporter::Declare;
@@ -12,6 +12,36 @@ use Exporter::Declare::Export::Generator;
 use Data::Dumper::Concise;
 use Scalar::Util 'blessed';
 
+use B qw(svref_2object);
+
+sub stash_name {
+   my ($coderef) = @_;
+   ref $coderef or return;
+   my $cv = B::svref_2object($coderef);
+   $cv->isa('B::CV') or return;
+   # bail out if GV is undefined
+   $cv->GV->isa('B::SPECIAL') and return;
+
+   return $cv->GV->STASH->NAME;
+}
+
+my @dlog = ((map "Dlog_$_", @levels), (map "DlogS_$_", @levels));
+
+my @log = ((map "log_$_", @levels), (map "logS_$_", @levels));
+
+sub _maybe_export {
+   my ($spec, $target, $name, $new_code) = @_;
+
+   if (my $code = $target->can($name)) {
+
+      # this will warn
+      $spec->add_export("&$name", $new_code)
+        unless (stash_name($code) eq __PACKAGE__);
+   } else {
+      $spec->add_export("&$name", $new_code)
+   }
+}
+
 eval {
    require Log::Log4perl;
    die if $Log::Log4perl::VERSION < 1.29;
@@ -22,7 +52,7 @@ eval {
 # export anything but the levels selected
 sub ____ { }
 
-exports(qw(____ set_logger with_logger ));
+exports('____', @dlog, @log, qw( set_logger with_logger ));
 
 export_tag dlog => ('____');
 export_tag log  => ('____');
@@ -35,6 +65,14 @@ sub router {
      }
 }
 
+sub default_import {
+   my ($class) = shift;
+
+   die 'Log::Contextual does not have a default import list';
+
+   ()
+}
+
 sub arg_logger         { $_[1] }
 sub arg_levels         { $_[1] || [qw(debug trace warn info error fatal)] }
 sub arg_package_logger { $_[1] }
@@ -50,30 +88,43 @@ sub before_import {
       arguments => $spec->argument_info
    );
 
-   die 'Log::Contextual does not have a default import list'
+   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;
+   }
+
    $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');
 
-      $spec->add_export('&set_logger', sub { $router->set_logger(@_) })
+      _maybe_export($spec, $importer, 'set_logger',
+         sub { $router->set_logger(@_) },
+      );
    }
 
    if ($exports->{'&with_logger'}) {
       die ref($router) . " does not support with_logger()"
         unless $router->does('Log::Contextual::Role::Router::WithLogger');
 
-      $spec->add_export('&with_logger', sub { $router->with_logger(@_) })
+      _maybe_export($spec, $importer, 'with_logger',
+         sub { $router->with_logger(@_) },
+      );
    }
 
    my @levels = @{$class->arg_levels($spec->config->{levels})};
    for my $level (@levels) {
-      if ($spec->config->{log}) {
-         $spec->add_export(
-            "&log_$level",
+      if ($spec->config->{log} || $exports->{"&log_$level"}) {
+         _maybe_export(
+            $spec,
+            $importer,
+            "log_$level",
             sub (&@) {
                my ($code, @args) = @_;
                $router->handle_log_request(
@@ -85,9 +136,14 @@ sub before_import {
                   message_args   => \@args,
                );
                return @args;
-            });
-         $spec->add_export(
-            "&logS_$level",
+            },
+         );
+      }
+      if ($spec->config->{log} || $exports->{"&logS_$level"}) {
+         _maybe_export(
+            $spec,
+            $importer,
+            "logS_$level",
             sub (&@) {
                my ($code, @args) = @_;
                $router->handle_log_request(
@@ -99,11 +155,14 @@ sub before_import {
                   message_args   => \@args,
                );
                return $args[0];
-            });
+            },
+         );
       }
-      if ($spec->config->{dlog}) {
-         $spec->add_export(
-            "&Dlog_$level",
+      if ($spec->config->{dlog} || $exports->{"&Dlog_$level"}) {
+         _maybe_export(
+            $spec,
+            $importer,
+            "Dlog_$level",
             sub (&@) {
                my ($code, @args) = @_;
                my $wrapped = sub {
@@ -119,9 +178,14 @@ sub before_import {
                   message_args   => \@args,
                );
                return @args;
-            });
-         $spec->add_export(
-            "&DlogS_$level",
+            },
+         );
+      }
+      if ($spec->config->{dlog} || $exports->{"&DlogS_$level"}) {
+         _maybe_export(
+            $spec,
+            $importer,
+            "DlogS_$level",
             sub (&$) {
                my ($code, $ref) = @_;
                my $wrapped = sub {
@@ -152,14 +216,20 @@ sub after_import {
    $class->router->after_import(%router_args);
 }
 
+for (qw(set with)) {
+   no strict 'refs';
+   my $sub = "${_}_logger";
+   *{"Log::Contextual::$sub"} = sub {
+      die "$sub is no longer a direct sub in Log::Contextual.  "
+        . 'Note that this feature was never tested nor documented.  '
+        . "Please fix your code to import $sub instead of trying to use it directly"
+     }
+}
+
 1;
 
 __END__
 
-=head1 NAME
-
-Log::Contextual - Simple logging interface with a contextual log
-
 =head1 SYNOPSIS
 
  use Log::Contextual qw( :log :dlog set_logger with_logger );
@@ -239,11 +309,11 @@ C<Log::Contextual> you will be able to swap underlying loggers later.
 
 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
+need to later.
 
 =item * Scalable
 
-If you just want to add logging to your extremely basic application, start with
+If you just want to add logging to your 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.
 
@@ -254,7 +324,7 @@ 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
+but in general you should use a real logger instead.  For something
 more serious but not overly complicated, try L<Log::Dispatchouli> (see
 L</SYNOPSIS> for example.)
 
@@ -278,7 +348,7 @@ wide.
 =head2 -logger
 
 When you import this module you may use C<-logger> as a shortcut for
-L<set_logger>, for example:
+L</set_logger>, for example:
 
  use Log::Contextual::SimpleLogger;
  use Log::Contextual qw( :dlog ),
@@ -305,10 +375,10 @@ supporting those levels is as easy as doing
 =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.
+except C<-package_logger> sets the logger for the current package.
 
 Unlike L</-default_logger>, C<-package_logger> cannot be overridden with
-L</set_logger>.
+L</set_logger> or L</with_logger>.
 
  package My::Package;
  use Log::Contextual::SimpleLogger;
@@ -323,7 +393,7 @@ 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.
+except C<-default_logger> sets the B<default> logger for the current package.
 
 Basically it sets the logger to be used if C<set_logger> is never called; so
 
@@ -357,6 +427,7 @@ own C<Log::Contextual> subclass as follows:
 
  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] }
@@ -366,7 +437,7 @@ own C<Log::Contextual> subclass as follows:
 
 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,
+either throw them away, honor them, die on usage, etc.  To be clear,
 if you define your subclass, and someone uses it as follows:
 
  use MyApp::Log::Contextual -default_logger => $foo,
@@ -375,6 +446,20 @@ if you define your subclass, and someone uses it as follows:
 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 ) }
+
+See L<Log::Contextual::Easy::Default> for an example of a subclass of
+C<Log::Contextual> that makes use of default import options.
+
 =head1 FUNCTIONS
 
 =head2 set_logger
@@ -526,7 +611,7 @@ slurping up (and also setting C<wantarray>) all the C<@args>
 
 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
+The logger coderef is passed the package of the caller, and 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.
 
@@ -622,27 +707,17 @@ functions. The router singleton is available as the return value of the router()
 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
 
+=encoding utf8
+
 triddle - Tyler Riddle <t.riddle@shadowcat.co.uk>
 
+voj - Jakob Voß <voss@gbv.de>
+
 =head1 DESIGNER
 
 mst - Matt S. Trout <mst@shadowcat.co.uk>
 
-=head1 COPYRIGHT
-
-Copyright (c) 2012 the Log::Contextual L</AUTHOR> and L</DESIGNER> as listed
-above.
-
-=head1 LICENSE
-
-This library is free software and may be distributed under the same terms as
-Perl 5 itself.
-
 =cut