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