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},
};
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;
#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 {
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;
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 $@;
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' });
$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,
},
},
$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');
$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;