File Coverage

blib/lib/Object/Remote/Logging/Router.pm
Criterion Covered Total %
statement 58 58 100.0
branch 12 14 85.7
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 81 87 93.1


line stmt bran cond sub pod time code
1             package Object::Remote::Logging::Router;
2              
3 18     18   56757 use Moo;
  18         9019  
  18         99  
4 18     18   5859 use Scalar::Util qw(weaken);
  18         36  
  18         810  
5 18     18   4722 use Sys::Hostname;
  18         10488  
  18         11474  
6              
7             with 'Log::Contextual::Role::Router';
8             with 'Object::Remote::Role::LogForwarder';
9              
10             has _connections => ( is => 'ro', required => 1, default => sub { [] } );
11             has _remote_metadata => ( is => 'rw' );
12              
13       145 0   sub before_import { }
14              
15       145 0   sub after_import { }
16              
17             sub _get_loggers {
18 11708     11708   34372 my ($self, %metadata) = @_;
19 11708         16902 my $package = $metadata{caller_package};
20 11708         14460 my $level = $metadata{message_level};
21 11708         18063 my $is_level = "is_$level";
22 11708         13324 my $need_clean = 0;
23 11708         13813 my @loggers;
24              
25 11708         13493 foreach my $selector (@{$self->_connections}) {
  11708         25268  
26 68 100       117 unless(defined $selector) {
27 8         9 $need_clean = 1;
28 8         34 next;
29             }
30              
31 60         331 foreach my $logger ($selector->($package, { %metadata })) {
32 60 50       171 next unless defined $logger;
33 60 50       162 next unless $logger->$is_level;
34 60         416 push(@loggers, $logger);
35             }
36             }
37              
38 11708 100       21693 $self->_clean_connections if $need_clean;
39              
40 11708         28082 return @loggers;
41             }
42              
43             #overloadable so a router can invoke a logger
44             #in a different way
45             sub _invoke_logger {
46 60     60   116 my ($self, $logger, $level_name, $content, $metadata) = @_;
47             #Invoking the logger like this gets all available data to the
48             #logging object with out losing any information from the datastructure.
49             #This is not a backwards compatible way to invoke the loggers
50             #but it enables a lot of flexibility in the logger.
51             #The l-c router could have this method invoke the logger in
52             #a backwards compatible way and router sub classes invoke
53             #it in non-backwards compatible ways if desired
54 60         174 $logger->$level_name($content, $metadata);
55             }
56              
57             #overloadable so forwarding can have the updated
58             #metadata but does not have to wrap get_loggers
59             #which has too many drawbacks
60             sub _deliver_message {
61             my ($self, %message_info) = @_;
62             my @loggers = $self->_get_loggers(%message_info);
63             my $generator = $message_info{message_sub};
64             my $args = $message_info{message_args};
65             my $level = $message_info{message_level};
66              
67             return unless @loggers > 0;
68             #this is the point where the user provided log message code block is executed
69             my @content = $generator->(@$args);
70             foreach my $logger (@loggers) {
71             $self->_invoke_logger($logger, $level, \@content, \%message_info);
72             }
73             }
74              
75             sub handle_log_request {
76 11708     11708 0 970578 my ($self, %message_info) = @_;
77 11708         17417 my $level = $message_info{message_level};
78 11708         14577 my $package = $message_info{caller_package};
79 11708         13940 my $need_clean = 0;
80              
81             #caller_level is useless when log forwarding is in place
82             #so we won't tempt people with using it
83 11708         16958 my $caller_level = delete $message_info{caller_level};
84 11708         21156 $message_info{object_remote} = $self->_remote_metadata;
85 11708         16400 $message_info{timestamp} = time;
86 11708         22007 $message_info{pid} = $$;
87 11708         23603 $message_info{hostname} = hostname;
88              
89 11708         76824 my @caller_info = caller($caller_level);
90 11708         21536 $message_info{filename} = $caller_info[1];
91 11708         16041 $message_info{line} = $caller_info[2];
92              
93 11708         48434 @caller_info = caller($caller_level + 1);
94 11708         20443 $message_info{method} = $caller_info[3];
95 11708 100       82643 $message_info{method} =~ s/^${package}::// if defined $message_info{method};
96              
97 11708         217381 $self->_deliver_message(%message_info);
98             }
99              
100             sub connect {
101 17     17 0 1210 my ($self, $destination, $is_weak) = @_;
102 17         23 my $wrapped;
103              
104 17 100       43 if (ref($destination) ne 'CODE') {
105 2     3   10 $wrapped = sub { $destination };
  3         8  
106             } else {
107 15         18 $wrapped = $destination;
108             }
109              
110 17         23 push(@{$self->_connections}, $wrapped);
  17         49  
111 17 100       69 weaken($self->_connections->[-1]) if $is_weak;
112             }
113              
114             sub _clean_connections {
115 8     8   14 my ($self) = @_;
116 8         22 @{$self->{_connections}} = grep { defined } @{$self->{_connections}};
  8         22  
  36         57  
  8         18  
117             }
118              
119             1;