1c239a7b556dbdbfd51c434a1fc8ec95defe23ce
[scpubgit/Object-Remote.git] / lib / Object / Remote / Logging.pm
1 package Object::Remote::Logging;
2
3 use Moo;
4 use Scalar::Util qw(blessed);
5 use Object::Remote::Logging::Logger;
6
7 extends 'Log::Contextual';
8
9 sub router {
10   our $Router_Instance ||= do {
11     require Object::Remote::Logging::Router;
12     Object::Remote::Logging::Router->new;
13   }
14 }
15
16 sub arg_levels {
17   return [qw( trace debug verbose info warn error )];
18 }
19
20 #this is invoked on all nodes
21 sub init_logging {
22   my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
23   return unless defined $level;
24   my $logger = Object::Remote::Logging::Logger->new(
25     min_level => lc($level),
26     level_names => Object::Remote::Logging::arg_levels(),
27   );
28
29   #TODO check on speed of string compare against a hash with a single key
30   router()->connect(sub { 
31     return unless $_[1]->{controller} eq __PACKAGE__;
32     #skip things from remote hosts because they log to STDERR
33     #when OBJECT_REMOTE_LOG_LEVEL is in effect
34     return if $_[1]->{remote}->{connection_id};
35     $logger
36   });
37 }
38
39 #this is invoked by the controlling node
40 #on the remote nodes
41 sub init_logging_forwarding {
42   my ($self, %controller_info) = @_;
43   
44   router()->_remote_metadata({ connection_id => $controller_info{connection_id} });
45   router()->_forward_destination($controller_info{router});
46 }
47
48 1;
49