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