new env vars: OBJECT_REMOTE_PERL_PATH and OBJECT_REMOTE_LOG_FORMAT
[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;
3a966220 7
21988035 8extends 'Log::Contextual';
3a966220 9
c3d5ef8a 10exports(qw( router ));
11
21988035 12sub router {
7d063a6a 13 our $Router_Instance ||= do {
14 require Object::Remote::Logging::Router;
15 Object::Remote::Logging::Router->new;
16 }
21988035 17}
3a966220 18
8bd147f3 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)
21988035 27sub arg_levels {
8bd147f3 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 )];
23591f5f 32}
3a966220 33
21988035 34#this is invoked on all nodes
23591f5f 35sub init_logging {
7d063a6a 36 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
c0d4da69 37 my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
7d063a6a 38 return unless defined $level;
c0d4da69 39 $format = "[%l %r] %s" unless defined $format;
7d063a6a 40 my $logger = Object::Remote::Logging::Logger->new(
c0d4da69 41 min_level => lc($level), format => $format,
7d063a6a 42 level_names => Object::Remote::Logging::arg_levels(),
43 );
44
45 #TODO check on speed of string compare against a hash with a single key
46 router()->connect(sub {
47 return unless $_[1]->{controller} eq __PACKAGE__;
48 #skip things from remote hosts because they log to STDERR
49 #when OBJECT_REMOTE_LOG_LEVEL is in effect
50 return if $_[1]->{remote}->{connection_id};
51 $logger
52 });
23591f5f 53}
54
21988035 55#this is invoked by the controlling node
56#on the remote nodes
23591f5f 57sub init_logging_forwarding {
21988035 58 my ($self, %controller_info) = @_;
59
60 router()->_remote_metadata({ connection_id => $controller_info{connection_id} });
61 router()->_forward_destination($controller_info{router});
23591f5f 62}
3a966220 63
641;
65