export router function in ::Logging; add time of log event to metadata and render...
[scpubgit/Object-Remote.git] / lib / Object / Remote / Logging.pm
CommitLineData
5e2b2229 1package Object::Remote::Logging;
2
4e446335 3use Moo;
4use Scalar::Util qw(blessed);
5use Object::Remote::Logging::Logger;
f4a85080 6use Exporter::Declare;
5e2b2229 7
4e446335 8extends 'Log::Contextual';
5e2b2229 9
f4a85080 10exports(qw( router ));
11
4e446335 12sub 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 27sub 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 35sub init_logging {
c0b2df05 36 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
37 return unless defined $level;
38 my $logger = Object::Remote::Logging::Logger->new(
39 min_level => lc($level),
40 level_names => Object::Remote::Logging::arg_levels(),
41 );
42
43 #TODO check on speed of string compare against a hash with a single key
44 router()->connect(sub {
45 return unless $_[1]->{controller} eq __PACKAGE__;
46 #skip things from remote hosts because they log to STDERR
47 #when OBJECT_REMOTE_LOG_LEVEL is in effect
48 return if $_[1]->{remote}->{connection_id};
49 $logger
50 });
4a9fa1a5 51}
52
4e446335 53#this is invoked by the controlling node
54#on the remote nodes
4a9fa1a5 55sub init_logging_forwarding {
4e446335 56 my ($self, %controller_info) = @_;
57
58 router()->_remote_metadata({ connection_id => $controller_info{connection_id} });
59 router()->_forward_destination($controller_info{router});
4a9fa1a5 60}
5e2b2229 61
621;
63