From: Arthur Axel 'fREW' Schmidt Date: Sat, 3 Nov 2012 20:29:27 +0000 (-0500) Subject: make arg_router a method so that it can always be overridden X-Git-Tag: v0.005000_01~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4d6051649139b17b441f11c6ff126c9017b24349;p=p5sagit%2FLog-Contextual.git make arg_router a method so that it can always be overridden --- diff --git a/lib/Log/Contextual.pm b/lib/Log/Contextual.pm index e0c59d5..248a731 100644 --- a/lib/Log/Contextual.pm +++ b/lib/Log/Contextual.pm @@ -50,12 +50,31 @@ sub arg_default_logger { $_[1] } sub before_import { my ($class, $importer, $spec) = @_; my $router = $class->arg_router; + my $exports = $spec->exports; die 'Log::Contextual does not have a default import list' if $spec->config->{default}; $router->before_import(@_); + $spec->add_export('&set_logger', sub { + my $router = $class->arg_router; + + die ref($router) . " does not support set_logger()" + unless $router->does('Log::Contextual::Role::Router::SetLogger'); + + return $router->set_logger(@_); + }) if $exports->{'&set_logger'}; + + $spec->add_export('&with_logger', sub { + my $router = $class->arg_router; + + die ref($router) . " does not support with_logger()" + unless $router->does('Log::Contextual::Role::Router::WithLogger'); + + return $router->with_logger(@_); + }) if $exports->{'&with_logger'}; + my @levels = @{$class->arg_levels($spec->config->{levels})}; for my $level (@levels) { if ($spec->config->{log}) { @@ -109,25 +128,7 @@ sub before_import { } } -sub after_import { return arg_router()->after_import(@_) } - -sub set_logger { - my $router = arg_router(); - - die ref($router) . " does not support set_logger()" - unless $router->does('Log::Contextual::Role::Router::SetLogger'); - - return $router->set_logger(@_); -} - -sub with_logger { - my $router = arg_router(); - - die ref($router) . " does not support with_logger()" - unless $router->does('Log::Contextual::Role::Router::WithLogger'); - - return $router->with_logger(@_); -} +sub after_import { $_[0]->arg_router->after_import(@_) } 1; diff --git a/t/lib/BaseLogger.pm b/t/lib/BaseLogger.pm index 4d0b1bc..614985f 100644 --- a/t/lib/BaseLogger.pm +++ b/t/lib/BaseLogger.pm @@ -7,6 +7,13 @@ my $logger = DumbLogger2->new; sub arg_levels { $_[1] || [qw(lol wut zomg)] } sub arg_logger { $_[1] || $logger } +sub arg_router { + our $Router_Instance ||= do { + require Log::Contextual::Router; + Log::Contextual::Router->new + } +} + package DumbLogger2;