Commit | Line | Data |
5e2b2229 |
1 | package Object::Remote::Logging; |
2 | |
4e446335 |
3 | use Moo; |
4 | use Scalar::Util qw(blessed); |
5 | use Object::Remote::Logging::Logger; |
f4a85080 |
6 | use Exporter::Declare; |
5e2b2229 |
7 | |
4e446335 |
8 | extends 'Log::Contextual'; |
5e2b2229 |
9 | |
b43174a1 |
10 | exports(qw( router arg_levels )); |
f4a85080 |
11 | |
4e446335 |
12 | sub router { |
c0b2df05 |
13 | our $Router_Instance ||= do { |
14 | require Object::Remote::Logging::Router; |
15 | Object::Remote::Logging::Router->new; |
16 | } |
4e446335 |
17 | } |
5e2b2229 |
18 | |
9de32e1d |
19 | #log level descriptions |
20 | #info - standard log level - normal program output for the end user |
21 | #warn - output for program that is executing quietly |
22 | #error - output for program that is running more quietly |
23 | #fatal - it is not possible to continue execution; this level is as quiet as is possible |
24 | #verbose - output for program executing verbosely (-v) |
25 | #debug - output for program running more verbosely (-v -v) |
26 | #trace - output for program running extremely verbosely (-v -v -v) |
4e446335 |
27 | sub arg_levels { |
9de32e1d |
28 | #the order of the log levels is significant with the |
29 | #most verbose level being first in the list and the |
30 | #most quiet as the last item |
31 | return [qw( trace debug verbose info warn error fatal )]; |
4a9fa1a5 |
32 | } |
5e2b2229 |
33 | |
4e446335 |
34 | #this is invoked on all nodes |
4a9fa1a5 |
35 | sub init_logging { |
c0b2df05 |
36 | my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL}; |
0fe333eb |
37 | my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT}; |
eb49c7df |
38 | #TODO allow the selections value to be * so it selects everything |
39 | my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS}; |
40 | my %controller_should_log; |
41 | |
c0b2df05 |
42 | return unless defined $level; |
0fe333eb |
43 | $format = "[%l %r] %s" unless defined $format; |
eb49c7df |
44 | $selections = __PACKAGE__ unless defined $selections; |
45 | %controller_should_log = map { $_ => 1 } split(' ', $selections); |
46 | |
c0b2df05 |
47 | my $logger = Object::Remote::Logging::Logger->new( |
0fe333eb |
48 | min_level => lc($level), format => $format, |
c0b2df05 |
49 | level_names => Object::Remote::Logging::arg_levels(), |
50 | ); |
51 | |
c0b2df05 |
52 | router()->connect(sub { |
eb49c7df |
53 | my $controller = $_[1]->{controller}; |
466ee2c4 |
54 | return unless $controller_should_log{'*'} || $controller_should_log{$controller}; |
c0b2df05 |
55 | #skip things from remote hosts because they log to STDERR |
56 | #when OBJECT_REMOTE_LOG_LEVEL is in effect |
57 | return if $_[1]->{remote}->{connection_id}; |
58 | $logger |
59 | }); |
4a9fa1a5 |
60 | } |
61 | |
4e446335 |
62 | #this is invoked by the controlling node |
63 | #on the remote nodes |
4a9fa1a5 |
64 | sub init_logging_forwarding { |
4e446335 |
65 | my ($self, %controller_info) = @_; |
66 | |
67 | router()->_remote_metadata({ connection_id => $controller_info{connection_id} }); |
466ee2c4 |
68 | router()->_forward_destination($controller_info{router}) if $ENV{OBJECT_REMOTE_LOG_FORWARDING}; |
4a9fa1a5 |
69 | } |
5e2b2229 |
70 | |
71 | 1; |
72 | |