1 package Object::Remote::Logging;
4 use Scalar::Util qw(blessed);
5 use Object::Remote::Logging::Logger;
7 use Carp qw(carp croak);
9 extends 'Log::Contextual';
11 exports(qw( ____ router arg_levels ));
12 #exception log - log a message then die with that message
13 export_tag elog => ('____');
14 #fatal log - log a message then call exit(1)
15 export_tag flog => ('____');
18 our $Router_Instance ||= do {
19 require Object::Remote::Logging::Router;
20 Object::Remote::Logging::Router->new;
24 #log level descriptions
25 #info - standard log level - normal program output for the end user
26 #warn - output for program that is executing quietly
27 #error - output for program that is running more quietly
28 #fatal - it is not possible to continue execution; this level is as quiet as is possible
29 #verbose - output for program executing verbosely (-v)
30 #debug - output for program running more verbosely (-v -v)
31 #trace - output for program running extremely verbosely (-v -v -v)
33 #the order of the log levels is significant with the
34 #most verbose level being first in the list and the
35 #most quiet as the last item
36 return [qw( trace debug verbose info warn error fatal )];
40 my ($class, $importer, $spec) = @_;
41 my $router = $class->router;
43 $class->SUPER::before_import($importer, $spec);
45 my @levels = @{$class->arg_levels($spec->config->{levels})};
46 for my $level (@levels) {
47 if ($spec->config->{elog}) {
48 $spec->add_export("&Elog_$level", sub (&) {
49 my ($code, @args) = @_;
50 $router->handle_log_request({
52 package => scalar(caller),
56 #TODO this should get fed into a logger so it can be formatted
60 if ($spec->config->{flog}) {
61 #TODO that prototype isn't right
62 $spec->add_export("&Flog_$level", sub (&@) {
63 my ($code, $exit_value) = @_;
64 $exit_value = 1 unless defined $exit_value;
65 $router->handle_log_request({
67 package => scalar(caller),
71 #TODO this should get fed into a logger so it can be formatted
79 #this is invoked on all nodes
81 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
82 my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
83 #TODO allow the selections value to be * so it selects everything
84 my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
85 my %controller_should_log;
87 return unless defined $level;
88 $format = "[%l %r] %s" unless defined $format;
89 $selections = __PACKAGE__ unless defined $selections;
90 %controller_should_log = map { $_ => 1 } split(' ', $selections);
92 my $logger = Object::Remote::Logging::Logger->new(
93 min_level => lc($level), format => $format,
94 level_names => Object::Remote::Logging::arg_levels(),
97 router()->connect(sub {
98 my $controller = $_[1]->{controller};
99 return unless $controller_should_log{'*'} || $controller_should_log{$controller};
100 #skip things from remote hosts because they log to STDERR
101 #when OBJECT_REMOTE_LOG_LEVEL is in effect
102 return if $_[1]->{remote}->{connection_id};
107 #this is invoked by the controlling node
109 sub init_logging_forwarding {
110 my ($self, %controller_info) = @_;
112 router()->_remote_metadata({ connection_id => $controller_info{connection_id} });
113 router()->_forward_destination($controller_info{router}) if $ENV{OBJECT_REMOTE_LOG_FORWARDING};