use System::Introspector::Logger qw( :log );
has introspectors => (is => 'ro', required => 1);
-has log_level => ( is => 'ro');
#the gatherer is the entry point on the remote host
#where logging has not been initialized yet so
#it must be initialized again before the probe can
#run
sub init_logging {
- System::Introspector::Logger->init_logging($_[1]);
- return $_[0];
+ my ($self, $log_level, $context) = @_;
+ System::Introspector::Logger->init_logging($log_level, $context);
+ return $self;
}
sub gather_all {
use System::Introspector::Logger::Output;
use Log::Contextual qw( set_logger );
+#always pass in log level and execution context;
+#specifying a logger is used when logging during
+#testing so a null output logging class can be used
sub init_logging {
- my ($self, $level, $logger) = @_;
+ my ($self, $level, $context, $logger) = @_;
$level = 'WARN' unless defined $level;
unless(defined($logger)) {
$logger = System::Introspector::Logger::Output->new({
env_prefix => 'SYSTEM_INTROSPECTOR_LOG',
});
+
+ $logger->set_execution_context($context);
}
#TODO: better way to specify log level?
use Log::Contextual::WarnLogger;
use base qw ( Log::Contextual::WarnLogger );
+sub set_execution_context {
+ my ($self, $context_name) = @_;
+
+ die "must specify an execution context name"
+ unless defined $context_name;
+
+ $self->{_introspector}->{context_name} = $context_name;
+}
+
sub _log {
- my $self = shift;
- my $level = shift;
- my $message = join( "\n", @_ );
- my @timedata = localtime;
- my $time = sprintf("%0.2i:%0.2i:%0.2i", $timedata[2], $timedata[1], $timedata[0]);
- $message .= "\n" unless $message =~ /\n$/;
- our ($hostname);
- #this is just a stub for right now - the configured hostname and the hostname the
- #system reports don't have to match - the user would be expecting the configured name
- $hostname = scalar(`hostname`) unless defined $hostname;
- chomp($hostname);
- warn "[$level $time] [$hostname] $message";
+ my $self = shift;
+ my $level = shift;
+ my $message = join( "\n", @_ );
+ my @timedata = localtime;
+ my $time = sprintf("%0.2i:%0.2i:%0.2i", $timedata[2], $timedata[1], $timedata[0]);
+ $message .= "\n" unless $message =~ /\n$/;
+ my $context = $self->{_introspector}->{context_name};
+ $context = 'undefined' unless defined $context;
+ warn "[$level $time] [$context] $message";
}
1;
requires 'gather';
+#wrap the gather() method with entry and exit traces so if
+#a probe in the future doesn't have explicit logging support at
+#least there will be some log that the probe has run
before gather => sub {
my ($self) = @_;
};
after gather => sub {
- my ($self) = @_;
+ my ($self) = @_;
log_trace { "gather() has completed on instance of " . ref($self) };
};
sub BUILD {
my ($self) = @_;
- System::Introspector::Logger->init_logging($self->config->log_level);
+ System::Introspector::Logger->init_logging($self->config->log_level, 'controller');
}
sub gather {
host => $arg{host},
sudo_user => $arg{sudo} && $self->sudo_user,
introspectors => $arg{introspectors},
- )->init_logging($self->config->log_level);
+ )->init_logging($self->config->log_level, "probe:$arg{host}");
}
1;
use System::Introspector::Logger qw();
#make sure to enable execution of every logging code block
#by setting the log level as high as it can go
-System::Introspector::Logger->init_logging('TRACE',
+System::Introspector::Logger->init_logging('TRACE', 'test',
System::Introspector::Logger::TestOutput->new({
levels_upto => 'trace'
}),