From: Tyler Riddle Date: Wed, 7 Nov 2012 00:56:55 +0000 (-0800) Subject: export router function in ::Logging; add time of log event to metadata and render... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c3d5ef8a61017dc4bf70b7b3c0d5308c13c1787c;p=scpubgit%2FObject-Remote.git export router function in ::Logging; add time of log event to metadata and render time in logger output; put a space before lines that are part of a multi-line log message so that condition can be detected --- diff --git a/lib/Object/Remote/Connection.pm b/lib/Object/Remote/Connection.pm index 34c99e9..887f708 100644 --- a/lib/Object/Remote/Connection.pm +++ b/lib/Object/Remote/Connection.pm @@ -1,6 +1,6 @@ package Object::Remote::Connection; -use Object::Remote::Logging qw (:log :dlog get_router); +use Object::Remote::Logging qw (:log :dlog router); use Object::Remote::Future; use Object::Remote::Null; use Object::Remote::Handle; @@ -18,7 +18,7 @@ use JSON::PP qw(encode_json); use Moo; BEGIN { - get_router()->exclude_forwarding; + router()->exclude_forwarding; #this will reap child processes as soon #as they are done executing so the process diff --git a/lib/Object/Remote/Future.pm b/lib/Object/Remote/Future.pm index 570bbb5..4b4e072 100644 --- a/lib/Object/Remote/Future.pm +++ b/lib/Object/Remote/Future.pm @@ -4,9 +4,9 @@ use strict; use warnings; use base qw(Exporter); -use Object::Remote::Logging qw( :log get_router ); +use Object::Remote::Logging qw( :log router ); -BEGIN { get_router()->exclude_forwarding } +BEGIN { router()->exclude_forwarding } use CPS::Future; diff --git a/lib/Object/Remote/Handle.pm b/lib/Object/Remote/Handle.pm index 8e0a84f..0d5f271 100644 --- a/lib/Object/Remote/Handle.pm +++ b/lib/Object/Remote/Handle.pm @@ -2,7 +2,7 @@ package Object::Remote::Handle; use Object::Remote::Proxy; use Scalar::Util qw(weaken blessed); -use Object::Remote::Logging qw ( :log get_router ); +use Object::Remote::Logging qw ( :log router ); use Object::Remote::Future; #must find way to exclude certain log events #from being forwarded - log events generated in @@ -11,7 +11,7 @@ use Object::Remote::Future; use Module::Runtime qw(use_module); use Moo; -BEGIN { get_router()->exclude_forwarding } +BEGIN { router()->exclude_forwarding } has connection => ( is => 'ro', required => 1, diff --git a/lib/Object/Remote/Logging.pm b/lib/Object/Remote/Logging.pm index e7da88a..eb336a5 100644 --- a/lib/Object/Remote/Logging.pm +++ b/lib/Object/Remote/Logging.pm @@ -3,9 +3,12 @@ package Object::Remote::Logging; use Moo; use Scalar::Util qw(blessed); use Object::Remote::Logging::Logger; +use Exporter::Declare; extends 'Log::Contextual'; +exports(qw( router )); + sub router { our $Router_Instance ||= do { require Object::Remote::Logging::Router; diff --git a/lib/Object/Remote/Logging/Logger.pm b/lib/Object/Remote/Logging/Logger.pm index a073a47..e9dc278 100644 --- a/lib/Object/Remote/Logging/Logger.pm +++ b/lib/Object/Remote/Logging/Logger.pm @@ -51,7 +51,6 @@ sub _install_methods { sub _log { my ($self, $level, $content, $metadata_in) = @_; - #TODO this stinks but is backwards compatible with the original logger api my %metadata = %$metadata_in; my $rendered = $self->_render($level, \%metadata, @$content); $self->_output($rendered); @@ -59,17 +58,27 @@ sub _log { sub _render { my ($self, $level, $metadata, @content) = @_; - my $rendered = "[$level] "; my $remote_info = $metadata->{object_remote}; + my $when = $metadata->{timestamp}; + my $rendered; + if (defined($when)) { + $when = localtime($when); + } else { + $when = 'no time data'; + } + if ($remote_info) { - $rendered .= "[connection #$remote_info->{connection_id}] "; + $rendered .= "[$level connection #$remote_info->{connection_id}] [$when] "; } else { - $rendered .= "[local] "; + $rendered .= "[$level local] [$when] "; } + $rendered .= join('', @content); - $rendered .= "\n" unless substr($rendered, -1) eq "\n"; + chomp($rendered); + $rendered =~ s/\n/\n /g; + $rendered .= "\n"; return $rendered; } diff --git a/lib/Object/Remote/Logging/Router.pm b/lib/Object/Remote/Logging/Router.pm index de3b1c4..14b97ad 100644 --- a/lib/Object/Remote/Logging/Router.pm +++ b/lib/Object/Remote/Logging/Router.pm @@ -57,7 +57,7 @@ sub handle_log_request { #to caller level will be available in the future delete $metadata{caller_level}; $metadata{object_remote} = $self->_remote_metadata; - + $metadata{timestamp} = time; foreach my $logger ($self->_get_loggers(%metadata)) { $logger->$level([ $generator->(@args) ], \%metadata); } diff --git a/lib/Object/Remote/MiniLoop.pm b/lib/Object/Remote/MiniLoop.pm index 5a47f56..cf572d6 100644 --- a/lib/Object/Remote/MiniLoop.pm +++ b/lib/Object/Remote/MiniLoop.pm @@ -2,10 +2,10 @@ package Object::Remote::MiniLoop; use IO::Select; use Time::HiRes qw(time); -use Object::Remote::Logging qw( :log :dlog get_router ); +use Object::Remote::Logging qw( :log :dlog router ); use Moo; -BEGIN { get_router()->exclude_forwarding } +BEGIN { router()->exclude_forwarding } # this is ro because we only actually set it using local in sub run has is_running => (is => 'ro', clearer => 'stop'); diff --git a/lib/Object/Remote/ReadChannel.pm b/lib/Object/Remote/ReadChannel.pm index 456e5aa..ed29f58 100644 --- a/lib/Object/Remote/ReadChannel.pm +++ b/lib/Object/Remote/ReadChannel.pm @@ -2,10 +2,10 @@ package Object::Remote::ReadChannel; use CPS::Future; use Scalar::Util qw(weaken openhandle); -use Object::Remote::Logging qw(:log :dlog get_router ); +use Object::Remote::Logging qw(:log :dlog router ); use Moo; -BEGIN { get_router()->exclude_forwarding } +BEGIN { router()->exclude_forwarding } has fh => ( is => 'ro', required => 1, diff --git a/lib/Object/Remote/Role/Connector/PerlInterpreter.pm b/lib/Object/Remote/Role/Connector/PerlInterpreter.pm index 79f2660..32cb877 100644 --- a/lib/Object/Remote/Role/Connector/PerlInterpreter.pm +++ b/lib/Object/Remote/Role/Connector/PerlInterpreter.pm @@ -4,7 +4,7 @@ use IPC::Open2; use IPC::Open3; use IO::Handle; use Symbol; -use Object::Remote::Logging qw( :log :dlog get_router ); +use Object::Remote::Logging qw( :log :dlog router ); use Object::Remote::ModuleSender; use Object::Remote::Handle; use Object::Remote::Future; @@ -57,7 +57,7 @@ around connect => sub { my ($conn) = $f->get; $self->_setup_watchdog_reset($conn); my $sub = $conn->remote_sub('Object::Remote::Logging::init_logging_forwarding'); - $sub->('Object::Remote::Logging', router => get_router, connection_id => $conn->_id); + $sub->('Object::Remote::Logging', router => router(), connection_id => $conn->_id); Object::Remote::Handle->new( connection => $conn, class => 'Object::Remote::ModuleLoader', diff --git a/t/logsetup.pl b/t/logsetup.pl index e583663..c119940 100644 --- a/t/logsetup.pl +++ b/t/logsetup.pl @@ -10,10 +10,10 @@ sub _output { } package main; -use Object::Remote::Logging qw( get_router ); +use Object::Remote::Logging qw( router ); #make sure to enable execution of every logging code block #by setting the log level as high as it can go -get_router->connect(Object::Remote::Logging::TestOutput->new( +router->connect(Object::Remote::Logging::TestOutput->new( min_level => 'trace', max_level => 'error', level_names => Object::Remote::Logging->arg_levels(), ));