From: Tyler Riddle Date: Fri, 9 Nov 2012 19:41:04 +0000 (-0800) Subject: Log::Any can inject messages into log router and acts as full class citizen in loggin... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0cdad12d5774beb63cb9d428e2aab53bd85ae44a;p=scpubgit%2FObject-Remote.git Log::Any can inject messages into log router and acts as full class citizen in logging framework including transparent forwarding across interpreters --- diff --git a/lib/Object/Remote/Logging.pm b/lib/Object/Remote/Logging.pm index 7e030ff..49d43f5 100644 --- a/lib/Object/Remote/Logging.pm +++ b/lib/Object/Remote/Logging.pm @@ -69,9 +69,9 @@ sub before_import { caller_level => 1, level => $level, }, $code) }; - warn "could not deliver log event during Flog_$level: $@" if defined $@; + warn "could not deliver log event during Flog_$level: $@" if $@; eval { carp $code->() }; - warn "could not emit warning during Flog_$level: $@" if defined $@; + warn "could not emit warning during Flog_$level: $@" if $@; exit($exit_value); }); } @@ -84,7 +84,13 @@ sub init_logging { my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT}; my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS}; my %controller_should_log; - + + eval { + require Log::Any::Adapter; + require Object::Remote::Logging::LogAnyInjector; + Log::Any::Adapter->set('+Object::Remote::Logging::LogAnyInjector'); + }; + return unless defined $level; $format = "[%l %r] %s" unless defined $format; $selections = __PACKAGE__ unless defined $selections; diff --git a/lib/Object/Remote/Logging/LogAnyInjector.pm b/lib/Object/Remote/Logging/LogAnyInjector.pm new file mode 100644 index 0000000..a8cf177 --- /dev/null +++ b/lib/Object/Remote/Logging/LogAnyInjector.pm @@ -0,0 +1,51 @@ +package Object::Remote::Logging::LogAnyInjector; + +use Moo; +use Object::Remote::Logging qw( router ); +use Carp qw(croak); + +BEGIN { + our %LEVEL_NAME_MAP = ( + #key is Log::Any log level name or alias and value is Object::Remote::Logging + #log level name + trace => 'trace', debug => 'debug', info => 'info', notice => 'verbose', + warning => 'warn', error => 'error', fatal => 'fatal', + critical => 'error', alert => 'error', 'emergency' => 'error', + inform => 'info', warn => 'warn', err => 'error', crit => 'error', + ); +} + +sub AUTOLOAD { + my ($self, @content) = @_; + (my $log_level) = (our $AUTOLOAD =~ /([^:]+)$/); + my $generator; + my $log_contextual_level; + our %LEVEL_NAME_MAP; + + #just a proof of concept - support for the is_ methods can + #be done but requires more modifications to the router + return 1 if $log_level =~ m/^is_/; + #skip DESTROY and friends + return if $log_level =~ m/^[A-Z]+$/; + + if ($log_level =~ s/f$//) { + my $format = shift(@content); + $generator = sub { sprintf($format, @content) }; + } else { + $generator = sub { @content }; + } + + $log_contextual_level = $LEVEL_NAME_MAP{$log_level}; + croak "invalid log level name: $log_level" unless defined $log_contextual_level; + + router->handle_log_request({ + controller => 'Log::Any', + package => scalar(caller), + caller_level => 1, + level => $log_contextual_level, + }, $generator); + + return; +} + +1;