From: Tyler Riddle Date: Tue, 8 Jan 2013 23:27:07 +0000 (-0800) Subject: update logging system to match log-contextual router changes X-Git-Tag: v0.003001_01~21 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a663aea54e2f4167d60dad19083cc7a078cfd04b;p=scpubgit%2FObject-Remote.git update logging system to match log-contextual router changes --- diff --git a/lib/Object/Remote/Logging/Logger.pm b/lib/Object/Remote/Logging/Logger.pm index 78f3890..9f79d7b 100644 --- a/lib/Object/Remote/Logging/Logger.pm +++ b/lib/Object/Remote/Logging/Logger.pm @@ -92,7 +92,7 @@ sub _create_format_lookup { t => $self->_render_time($metadata->{timestamp}), r => $self->_render_remote($metadata->{object_remote}), s => $self->_render_log(@$content), l => $level, - c => $metadata->{controller}, p => $metadata->{package}, m => $method, + c => $metadata->{exporter}, p => $metadata->{caller_package}, m => $method, f => $metadata->{filename}, i => $metadata->{line}, h => $metadata->{hostname}, P => $metadata->{pid}, }; diff --git a/lib/Object/Remote/Logging/Router.pm b/lib/Object/Remote/Logging/Router.pm index 84a562f..46b6da7 100644 --- a/lib/Object/Remote/Logging/Router.pm +++ b/lib/Object/Remote/Logging/Router.pm @@ -10,16 +10,14 @@ with 'Object::Remote::Role::LogForwarder'; has _connections => ( is => 'ro', required => 1, default => sub { [] } ); has _remote_metadata => ( is => 'rw' ); -sub before_import { - my ($self, $controller, $importer, $spec) = @_; -} +sub before_import { } sub after_import { } sub _get_loggers { my ($self, %metadata) = @_; - my $package = $metadata{package}; - my $level = $metadata{level}; + my $package = $metadata{caller_package}; + my $level = $metadata{log_level}; my $is_level = "is_$level"; my $need_clean = 0; my @loggers; @@ -60,43 +58,43 @@ sub _invoke_logger { #metadata but does not have to wrap get_loggers #which has too many drawbacks sub _deliver_message { - my ($self, $level, $generator, $args, $metadata) = @_; - my @loggers = $self->_get_loggers(%$metadata); + my ($self, %message_info) = @_; + my @loggers = $self->_get_loggers(%message_info); + my $generator = $message_info{message_sub}; + my $args = $message_info{message_args}; + my $level = $message_info{log_level}; return unless @loggers > 0; - #this is the point where the user provided log message - #code block is executed + #this is the point where the user provided log message code block is executed my @content = $generator->(@$args); foreach my $logger (@loggers) { - $self->_invoke_logger($logger, $level, \@content, $metadata); + $self->_invoke_logger($logger, $level, \@content, \%message_info); } } sub handle_log_request { - my ($self, $metadata_in, $generator, @args) = @_; - my %metadata = %{$metadata_in}; - my $level = $metadata{level}; - my $package = $metadata{package}; + my ($self, %message_info) = @_; + my $level = $message_info{log_level}; + my $package = $message_info{caller_package}; my $need_clean = 0; #caller_level is useless when log forwarding is in place - #so we won't tempt people with using it for now - access - #to caller level will be available in the future - my $caller_level = delete $metadata{caller_level}; - $metadata{object_remote} = $self->_remote_metadata; - $metadata{timestamp} = time; - $metadata{pid} = $$; - $metadata{hostname} = hostname; + #so we won't tempt people with using it + my $caller_level = delete $message_info{caller_level}; + $message_info{object_remote} = $self->_remote_metadata; + $message_info{timestamp} = time; + $message_info{pid} = $$; + $message_info{hostname} = hostname; my @caller_info = caller($caller_level); - $metadata{filename} = $caller_info[1]; - $metadata{line} = $caller_info[2]; + $message_info{filename} = $caller_info[1]; + $message_info{line} = $caller_info[2]; @caller_info = caller($caller_level + 1); - $metadata{method} = $caller_info[3]; - $metadata{method} =~ s/^${package}::// if defined $metadata{method}; + $message_info{method} = $caller_info[3]; + $message_info{method} =~ s/^${package}::// if defined $message_info{method}; - $self->_deliver_message($level, $generator, [ @args ], \%metadata); + $self->_deliver_message(%message_info); } sub connect { diff --git a/lib/Object/Remote/Role/LogForwarder.pm b/lib/Object/Remote/Role/LogForwarder.pm index 4bf2ecc..5c2aecd 100644 --- a/lib/Object/Remote/Role/LogForwarder.pm +++ b/lib/Object/Remote/Role/LogForwarder.pm @@ -9,17 +9,17 @@ has _forward_destination => ( is => 'rw' ); has _forward_stop => ( is => 'ro', required => 1, default => sub { {} } ); after _deliver_message => sub { - my ($self, $level, $generator, $args, $metadata) = @_; - my $package = $metadata->{package}; +# my ($self, $level, $generator, $args, $metadata) = @_; + my ($self, %message_info) = @_; + my $package = $message_info{caller_package}; my $destination = $self->_forward_destination; - my %metadata = %$metadata; our $reentrant; - if (defined $metadata->{object_remote}) { - $metadata{object_remote} = { %{$metadata->{object_remote}} }; + if (defined $message_info{object_remote}) { + $message_info{object_remote} = { %{$message_info{object_remote}} }; } - $metadata{object_remote}->{forwarded} = 1; + $message_info{object_remote}->{forwarded} = 1; return unless $self->enable_forward; return unless defined $destination; @@ -32,7 +32,7 @@ after _deliver_message => sub { local $reentrant = $package; - eval { $destination->_deliver_message($level, $generator, $args, \%metadata) }; + eval { $destination->_deliver_message(%message_info) }; if ($@ && $@ !~ /^Attempt to use Object::Remote::Proxy backed by an invalid handle/) { die $@; diff --git a/t/logrouter.t b/t/logrouter.t index c1dcf86..4a486cd 100644 --- a/t/logrouter.t +++ b/t/logrouter.t @@ -8,10 +8,11 @@ use Object::Remote::Logging::Router; my $controller_name = 'Test::Log::Controller'; my $generator = sub { "Generator output" }; -my $metadata = { - controller => $controller_name, package => __PACKAGE__, level => 'test1', - caller_level => 0, -}; +my %metadata = ( + exporter => $controller_name, + caller_package => __PACKAGE__, caller_level => 0, + log_level => 'test1', message_sub => $generator, message_args => [], +); my $router = Object::Remote::Logging::Router->new; $router->_remote_metadata({ router => undef, connection_id => 'TestConnectionId' }); @@ -28,16 +29,18 @@ ok(scalar(@{$router->_connections} == 1), 'There is a single connection'); $logger->reset; my $linenum = __LINE__ + 1; -$router->handle_log_request($metadata, $generator); +$router->handle_log_request(%metadata); is($logger->feedback_output, "test1: Generator output\n", 'Rendered log message is correct'); ok($logger->feedback_input->[2]->{timestamp} > 0, 'Timestamp value is present'); delete $logger->feedback_input->[2]->{timestamp}; +is(ref $logger->feedback_input->[2]->{message_sub}, 'CODE', 'message sub did exist'); +delete $logger->feedback_input->[2]->{message_sub}; is_deeply($logger->feedback_input, [ 'test1', [ 'Generator output' ], { - controller => 'Test::Log::Controller', level => 'test1', - hostname => hostname(), pid => $$, package => __PACKAGE__, + exporter => 'Test::Log::Controller', log_level => 'test1', + hostname => hostname(), pid => $$, caller_package => __PACKAGE__, line => $linenum, method => undef, filename => __FILE__, - object_remote => { + message_args => [], object_remote => { connection_id => 'TestConnectionId', router => undef, }, }, @@ -45,7 +48,7 @@ is_deeply($logger->feedback_input, [ $logger->reset; undef($selector); -$router->handle_log_request($metadata, $generator); +$router->handle_log_request(%metadata); ok(scalar(@{$router->_connections}) == 0, 'Selector has been disconnected'); ok(! defined $logger->feedback_output, 'Logger has no output feedback'); ok(! defined $logger->feedback_input, 'Logger has no input feedback'); @@ -53,7 +56,7 @@ ok(! defined $logger->feedback_input, 'Logger has no input feedback'); $router->connect($logger); ok(scalar(@{$router->_connections} == 1), 'There is a single connection'); undef($logger); -$router->handle_log_request($metadata, $generator); +$router->handle_log_request(%metadata); ok(scalar(@{$router->_connections} == 1), 'Connection is still active'); done_testing;