Remote/Logging.pm

filter: Statementstmt 20.8% 15 / 72 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 41.6% 5 / 12 Podpod n/a totaltotal 15.5% 20 / 129 CC 21 SCAR 34.5
CC
21
Cov
14%
init_logging
77.0
_parse_selections
30.0
__ANON__
20.0
CRAP 31.6
« Remote/Connector/LocalSudo.pm ↑ Summary Remote/ModuleLoader.pm »
1 package Object::Remote::Logging;
2  
3 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Object::Remote::Logging::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 extends 'Log::Contextual';
8  
9 our @EXPORT_OK = qw(router arg_levels);
10  
11 0 ▶sub import {
Subroutine import: not called CC=4 SCAR=26.6
12 0 my $class = shift;
13 0 my $caller = caller;
14 0 my @args = ($class);
15  
16 0 our $DID_INIT;
17  
18 0 ▶ unless($DID_INIT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Object::Remote::Logging::DID_INIT) 0 0 0
19 0 $DID_INIT = 1;
20 0 init_logging();
21 }
22  
23 0 for my $arg (@_) {
24 0 ▶ if (grep $_ eq $arg, @EXPORT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($_ eq $arg), @Object::Remote::Logging::EXPORT_OK)) { } 0 0 0
25 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 0 *{$caller . '::' . $arg} = \&{$arg};
27 }
28 else {
29 0 push @args, $arg;
30 }
31 }
32 0 @_ = @args;
33 0 goto &Log::Contextual::import;
34 }
35  
36 0 ▶sub router {
Subroutine router: not called CC=2 SCAR=17.9
37 0 ▶ our $Router_Instance ||= do {
Condition0%0/3
our $Router_Instance ||= do { require Object::Remote::Logging::Router; "Object::Remote::Logging::Router"->new }
l!l&&r!l&&!r
000
MC/DC0%0/2
our $Router_Instance ||= do { require Object::Remote::Logging::Router; "Object::Remote::Logging::Router"->new }
our $Router_Instance do { require Object::Remote::Logging::Router; "Object::Remote::Logging::Router"->new }
Truth table0%0/3
A ||= B
A= our $Router_InstanceB= do { require Object::Remote::Logging::Router; "Object::Remote::Logging::Router"->new }
ABresult
1X1
011
000
38 0 require Object::Remote::Logging::Router;
39 0 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 0 ▶sub arg_levels {
Subroutine arg_levels: not called CC=1 SCAR=6.9
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 0 return [qw( trace debug verbose info warn error fatal )];
56 }
57  
58 0 ▶sub _parse_selections {
Subroutine _parse_selections: not called CC=5 SCAR=34.0
59 0 my ($selections_string) = @_;
60 0 my %log_ok;
61  
62 #example string:
63 #" * -Object::Remote::Logging Foo::Bar::Baz "
64 0 foreach(split(/\s+/, $selections_string)) {
65 0 ▶ next if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 0 0 0
66 0 ▶ if ($_ eq '*') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq '*') { } 0 0 0
elsif (s/^-//) { } 0 0 0
67 0 $log_ok{$_} = 1;
68 } elsif (s/^-//) {
69 0 $log_ok{$_} = 0;
70 } else {
71 0 $log_ok{$_} = 1;
72 }
73 }
74  
75 0 return %log_ok;
76 }
77  
78 #this is invoked on all nodes
79 0 ▶sub init_logging {
Subroutine init_logging: not called CC=9 SCAR=43.4
80 0 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
81 0 my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
82 0 my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
83 0 my $test_logging = $ENV{OBJECT_REMOTE_TEST_LOGGER};
84 0 my %controller_should_log;
85  
86 0 ▶ unless (defined $ENV{OBJECT_REMOTE_LOG_FORWARDING} && $ENV{OBJECT_REMOTE_LOG_FORWARDING} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} and $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} ne "") 0 0 0
Condition0%0/3
defined $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} and $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} and $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} ne ""
defined $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} ne ""
Truth table0%0/3
A and B
A= defined $ENV{"OBJECT_REMOTE_LOG_FORWARDING"}B= $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} ne ""
ABresult
0X0
100
111
87 0 $ENV{OBJECT_REMOTE_LOG_FORWARDING} = 0;
88 }
89  
90 0 ▶ if ($test_logging) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_logging) 0 0 0
91 0 require Object::Remote::Logging::TestLogger;
92 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 0 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.2
100 0 ▶ if (defined $Object::Remote::FatNode::REMOTE_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Object::Remote::FatNode::REMOTE_NODE) 0 0 0
101 #the connection id for the remote node comes in later
102 #as the controlling node inits remote logging
103 0 router()->_remote_metadata({ connection_id => undef });
104 }
105 }
106  
107 0 ▶ return unless defined $level && $level ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and $level ne "" 0 0 0
Condition0%0/3
defined $level and $level ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $level and $level ne ""
defined $level $level ne ""
Truth table0%0/3
A and B
A= defined $levelB= $level ne ""
ABresult
0X0
100
111
108  
109 0 ▶ $format = "[%l %r] %s" unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 0 0
110 0 ▶ $selections = __PACKAGE__ unless defined $selections;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $selections 0 0 0
111 0 %controller_should_log = _parse_selections($selections);
112  
113 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 0 ▶ router()->connect(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
119 0 my $controller = $_[1]->{exporter};
120 0 my $will_log = $controller_should_log{$controller};
121 0 my $remote_info = $_[1]->{object_remote};
122  
123 0 ▶ $will_log = $controller_should_log{'*'} unless defined $will_log;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $will_log 0 0 0
124  
125 0 ▶ return unless $will_log;
Branch: 1 branch
BranchTrueFalseTotal
unless $will_log 0 0 0
126 #skip things from remote hosts because they log to STDERR
127 #when OBJECT_REMOTE_LOG_LEVEL is in effect
128 0 ▶ return if $remote_info->{forwarded};
Branch: 1 branch
BranchTrueFalseTotal
if $remote_info->{"forwarded"} 0 0 0
129 0 return $logger;
130 0 });
131 }
132  
133 #this is invoked by the controlling node
134 #on the remote nodes
135 0 ▶sub init_remote_logging {
Subroutine init_remote_logging: not called CC=2 SCAR=17.9
136 0 my ($self, %controller_info) = @_;
137  
138 0 router()->_remote_metadata(\%controller_info);
139 0 ▶ router()->_forward_destination($controller_info{router}) if $ENV{OBJECT_REMOTE_LOG_FORWARDING};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"OBJECT_REMOTE_LOG_FORWARDING"} 0 0 0
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<Log::Contextual> 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<Object::Remote::Logging::Logger>
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<Object::Remote::Logging::Logger>. 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 separated 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<Object::Remote::Logging::Logger> 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<Object::Remote::Logging::Router> that is in use. The router
235 instance is used in combination with L<Object::Remote::Logging::Logger> objects to
236 select then render and output log messages.
237  
238 =item log_<level> and Dlog_<level>
239  
240 These methods come direct from L<Log::Contextual>; 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_<level> and DlogS_<level>
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
« Remote/Connector/LocalSudo.pm ↑ Summary Remote/ModuleLoader.pm »