File Coverage

blib/lib/Object/Remote/Logging.pm
Criterion Covered Total %
statement 54 72 75.0
branch 13 30 43.3
condition 4 9 44.4
subroutine 10 12 83.3
pod 2 4 50.0
total 83 127 65.3


line stmt bran cond sub pod time code
1             package Object::Remote::Logging;
2              
3 17     17   268403 use Moo;
  17         111690  
  17         118  
4 17     17   32441 use Object::Remote::Logging::Logger;
  17         73  
  17         675  
5 17     17   146 use Exporter ();
  17         36  
  17         2404  
6              
7             extends 'Log::Contextual';
8              
9             our @EXPORT_OK = qw(router arg_levels);
10              
11             sub import {
12 145     145   125777 my $class = shift;
13 145         544 my $caller = caller;
14 145         464 my @args = ($class);
15              
16 145         300 our $DID_INIT;
17              
18 145 100       704 unless($DID_INIT) {
19 17         37 $DID_INIT = 1;
20 17         71 init_logging();
21             }
22              
23 145         624 for my $arg (@_) {
24 358 100       1360 if (grep $_ eq $arg, @EXPORT_OK) {
25 17     17   144 no strict 'refs';
  17         30  
  17         17714  
26 102         196 *{$caller . '::' . $arg} = \&{$arg};
  102         940  
  102         23505  
27             }
28             else {
29 256         635 push @args, $arg;
30             }
31             }
32 145         473 @_ = @args;
33 145         1067 goto &Log::Contextual::import;
34             }
35              
36             sub router {
37 278   66 278 1 284780 our $Router_Instance ||= do {
38 17         10701 require Object::Remote::Logging::Router;
39 17         175 Object::Remote::Logging::Router->new;
40             }
41             }
42              
43             #log level descriptions
44             #info - standard log level - normal program output for the end user
45             #warn - output for program that is executing quietly
46             #error - output for program that is running more quietly
47             #fatal - it is not possible to continue execution; this level is as quiet as is possible
48             #verbose - output for program executing verbosely (-v)
49             #debug - output for program running more verbosely (-v -v)
50             #trace - output for program running extremely verbosely (-v -v -v)
51             sub arg_levels {
52             #the order of the log levels is significant with the
53             #most verbose level being first in the list and the
54             #most quiet as the last item
55 160     160 1 13918 return [qw( trace debug verbose info warn error fatal )];
56             }
57              
58             sub _parse_selections {
59 1     1   1765 my ($selections_string) = @_;
60 1         3 my %log_ok;
61              
62             #example string:
63             #" * -Object::Remote::Logging Foo::Bar::Baz "
64 1         5 foreach(split(/\s+/, $selections_string)) {
65 3 50       12 next if $_ eq '';
66 3 100       15 if ($_ eq '*') {
    100          
67 1         3 $log_ok{$_} = 1;
68             } elsif (s/^-//) {
69 1         4 $log_ok{$_} = 0;
70             } else {
71 1         4 $log_ok{$_} = 1;
72             }
73             }
74              
75 1         8 return %log_ok;
76             }
77              
78             #this is invoked on all nodes
79             sub init_logging {
80 17     17 0 59 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
81 17         43 my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
82 17         67 my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
83 17         40 my $test_logging = $ENV{OBJECT_REMOTE_TEST_LOGGER};
84 17         35 my %controller_should_log;
85              
86 17 50 33     131 unless (defined $ENV{OBJECT_REMOTE_LOG_FORWARDING} && $ENV{OBJECT_REMOTE_LOG_FORWARDING} ne '') {
87 17         269 $ENV{OBJECT_REMOTE_LOG_FORWARDING} = 0;
88             }
89              
90 17 50       93 if ($test_logging) {
91 0         0 require Object::Remote::Logging::TestLogger;
92 0         0 router->connect(Object::Remote::Logging::TestLogger->new(
93             min_level => 'trace', max_level => 'error',
94             level_names => Object::Remote::Logging->arg_levels(),
95             ));
96             }
97              
98             {
99 17     17   44 no warnings 'once';
  17         148  
  17         31  
  17         7689  
100 17 50       105 if (defined $Object::Remote::FatNode::REMOTE_NODE) {
101             #the connection id for the remote node comes in later
102             #as the controlling node inits remote logging
103 0         0 router()->_remote_metadata({ connection_id => undef });
104             }
105             }
106              
107 17 50 33     130 return unless defined $level && $level ne '';
108              
109 0 0       0 $format = "[%l %r] %s" unless defined $format;
110 0 0       0 $selections = __PACKAGE__ unless defined $selections;
111 0         0 %controller_should_log = _parse_selections($selections);
112              
113 0         0 my $logger = Object::Remote::Logging::Logger->new(
114             min_level => lc($level), format => $format,
115             level_names => Object::Remote::Logging::arg_levels(),
116             );
117              
118             router()->connect(sub {
119 0     0   0 my $controller = $_[1]->{exporter};
120 0         0 my $will_log = $controller_should_log{$controller};
121 0         0 my $remote_info = $_[1]->{object_remote};
122              
123 0 0       0 $will_log = $controller_should_log{'*'} unless defined $will_log;
124              
125 0 0       0 return unless $will_log;
126             #skip things from remote hosts because they log to STDERR
127             #when OBJECT_REMOTE_LOG_LEVEL is in effect
128 0 0       0 return if $remote_info->{forwarded};
129 0         0 return $logger;
130 0         0 });
131             }
132              
133             #this is invoked by the controlling node
134             #on the remote nodes
135             sub init_remote_logging {
136 0     0 0 0 my ($self, %controller_info) = @_;
137              
138 0         0 router()->_remote_metadata(\%controller_info);
139 0 0       0 router()->_forward_destination($controller_info{router}) if $ENV{OBJECT_REMOTE_LOG_FORWARDING};
140             }
141              
142             1;
143              
144             =head1 NAME
145              
146             Object::Remote::Logging - Logging subsystem for Object::Remote
147              
148             =head1 SYNOPSIS
149              
150             use Object::Remote::Logging qw( :log :dlog arg_levels router );
151              
152             $levels = [qw( trace debug verbose info warn error fatal )];
153             $levels = arg_levels(); #same result
154              
155             $ENV{OBJECT_REMOTE_LOG_LEVEL} = 'trace'; #or other level name
156             $ENV{OBJECT_REMOTE_LOG_FORMAT} = '%l %t: %p::%m %s'; #and more
157             #Output logs from two specific logging pacakges
158             $ENV{OBJECT_REMOTE_LOG_SELECTIONS} = 'Object::Remote::Logging Some::Other::Package';
159             #Output all log messages except those generated by Object::Remote
160             $ENV{OBJECT_REMOTE_LOG_SELECTIONS} = '* -Object::Remote::Logging';
161             $ENV{OBJECT_REMOTE_LOG_FORWARDING} = 1; #default 0
162              
163             log_info { 'Trace log event' };
164             Dlog_verbose { "Debug event with Data::Dumper::Concise: $_" } { foo => 'bar' };
165              
166             =head1 DESCRIPTION
167              
168             This is the logging framework for Object::Remote implemented as an extension of
169             L with a slightly incompatible API. This system allows
170             developers using Object::Remote and end users of that software to control
171             Object::Remote logging so operation can be tracked if needed. This is also
172             the API used to generate log messages inside the Object::Remote source code.
173              
174             The rest of the logging system comes from L
175             which implements log rendering and output and Object::Remote::Logging::Router
176             which delivers log events to the loggers.
177              
178             =head1 USAGE
179              
180             Object::Remote logging output is not enabled by default. If you need to immediately start
181             debugging set the OBJECT_REMOTE_LOG_LEVEL environment variable to either 'trace'
182             or 'debug'. This will enable logging to STDERR on the local and all remote Perl
183             interpreters. By default STDERR for all remote interpreters is passed through
184             unmodified so this is sufficient to receive logs generated anywhere Object::Remote
185             is running.
186              
187             Every time the local interpreter creates a new Object::Remote::Connection the connection
188             is given an id that is unique to that connection on the local interpreter. The connection
189             id and other metadata is available in the log output via a log format string that can
190             be set via the OBJECT_REMOTE_LOG_FORMAT environment variable. The format string and
191             available metadata is documented in L. Setting this
192             environment variable on the local interpreter will cause it to be propagated to the
193             remote interpreter so all logs will be formated the same way.
194              
195             This system is designed so any module can create their own logging packages using it.
196             With out any additional configuration the consumers of this logging system will
197             automatically be enabled via OBJECT_REMOTE_LOG_LEVEL and formated with
198             OBJECT_REMOTE_LOG_FORMAT but those additional log messages are not sent to STDERR.
199             By setting the OBJECT_REMOTE_LOG_SELECTIONS environment variable to a list of logging
200             package names seperated by spaces then logs generated using those packages
201             will be sent to STDERR. If the asterisk character (*) is used in the place of a package
202             name then all package names will be selected by default instead of ignored. An individual
203             package name can be turned off by prefixing the name with a hypen character (-). This is
204             also a configuration item that is forwarded to the remote interpreters so all logging
205             is consistent.
206              
207             Regardless of OBJECT_REMOTE_LOG_LEVEL the logging system is still active and loggers
208             can access the stream of log messages to format and output them. Internally
209             OBJECT_REMOTE_LOG_LEVEL causes an L to be built
210             and connected to the Object::Remote::Logging::Router instance. It is also possible
211             to manually build a logger instance and connect it to the router. See the
212             Object::Remote::Logging documentation for more information.
213              
214             The logging system also supports a method of forwarding log messages from remote
215             interpreters to the local interpreter. Forwarded log messages are generated in the
216             remote interpreter and the logger for the message is invoked in the local interpreter.
217             Packages using or extending Object::Remote::Logging will have log messages forwarded automatically.
218             Loggers receive forwarded log messages exactly the same way as non-forwarded messages
219             except a forwarded message includes extra metadata about the remote connection. Log
220             forwarding is disabled by default because it comes with a performance hit; to enable
221             it set the OBJECT_REMOTE_LOG_FORWARDING environment variable to 1.
222              
223             =head1 EXPORTABLE SUBROUTINES
224              
225             =over 4
226              
227             =item arg_levels
228              
229             Returns an array reference that contains the ordered list of level names
230             with the lowest log level first and the highest log level last.
231              
232             =item router
233              
234             Returns the instance of L that is in use. The router
235             instance is used in combination with L objects to
236             select then render and output log messages.
237              
238             =item log_ and Dlog_
239              
240             These methods come direct from L; see that documentation for a
241             complete reference. For each of the log level names there are subroutines with the log_
242             and Dlog_ prefix that will generate the log message. The first argument is a code block
243             that returns the log message contents and the optional further arguments are both passed
244             to the block as the argument list and returned from the log method as a list.
245              
246             log_trace { "A fine log message $_[0] " } 'if I do say so myself';
247             %hash = Dlog_trace { "Very handy: $_" } ( foo => 'bar' );
248              
249             =item logS_ and DlogS_
250              
251             Works just like log_ and Dlog_ except returns only the first argument as a scalar value.
252              
253             my $beverage = logS_info { "Customer ordered $_[0]" } 'Coffee';
254              
255             =back
256              
257             =head1 LEVEL NAMES
258              
259             Object::Remote uses an ordered list of log level names with the lowest level
260             first and the highest level last. The list of level names can be accessed via
261             the arg_levels method which is exportable to the consumer of this class. The log
262             level names are:
263              
264             =over 4
265              
266             =item trace
267              
268             As much information about operation as possible including multiple line dumps of
269             large content. Tripple verbose operation (-v -v -v).
270              
271             =item debug
272              
273             Messages about operations that could hang as well as internal state changes,
274             results from method invocations, and information useful when looking for faults.
275             Double verbose operation (-v -v).
276              
277             =item verbose
278              
279             Additional optional messages to the user that can be enabled at their will. Single
280             verbose operation (-v).
281              
282             =item info
283              
284             Messages from normal operation that are intended to be displayed to the end
285             user if quiet operation is not indicated and more verbose operation is not
286             in effect.
287              
288             =item warn
289              
290             Something wasn't supposed to happen but did. Operation was not impacted but
291             otherwise the event is noteworthy. Single quiet operation (-q).
292              
293             =item error
294              
295             Something went wrong. Operation of the system may continue but some operation
296             has most definitely failed. Double quiet operation (-q -q).
297              
298             =item fatal
299              
300             Something went wrong and recovery is not possible. The system should stop operating
301             as soon as possible. Tripple quiet operation (-q -q -q).
302              
303             =back