From: Tyler Riddle Date: Thu, 6 Sep 2012 15:39:37 +0000 (-0700) Subject: finish integration of execution context logging feature X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=23d700d071761deb573c8b50dbabb50f1a723917;p=scpubgit%2FSystem-Introspector.git finish integration of execution context logging feature --- diff --git a/lib/System/Introspector/Gatherer.pm b/lib/System/Introspector/Gatherer.pm index 8f82de5..6354eae 100644 --- a/lib/System/Introspector/Gatherer.pm +++ b/lib/System/Introspector/Gatherer.pm @@ -7,15 +7,15 @@ use Module::Runtime qw( use_module ); 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 { diff --git a/lib/System/Introspector/Logger.pm b/lib/System/Introspector/Logger.pm index 03ced6a..1303a59 100644 --- a/lib/System/Introspector/Logger.pm +++ b/lib/System/Introspector/Logger.pm @@ -6,14 +6,19 @@ use base qw(Log::Contextual); 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? diff --git a/lib/System/Introspector/Logger/Output.pm b/lib/System/Introspector/Logger/Output.pm index e66390d..5c9b495 100644 --- a/lib/System/Introspector/Logger/Output.pm +++ b/lib/System/Introspector/Logger/Output.pm @@ -5,19 +5,25 @@ package System::Introspector::Logger::Output; 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; diff --git a/lib/System/Introspector/Role/Probe.pm b/lib/System/Introspector/Role/Probe.pm index 246ad83..f9d3425 100644 --- a/lib/System/Introspector/Role/Probe.pm +++ b/lib/System/Introspector/Role/Probe.pm @@ -8,6 +8,9 @@ use Moo::Role; 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) = @_; @@ -15,7 +18,7 @@ before gather => sub { }; after gather => sub { - my ($self) = @_; + my ($self) = @_; log_trace { "gather() has completed on instance of " . ref($self) }; }; diff --git a/lib/System/Introspector/State.pm b/lib/System/Introspector/State.pm index 22e2a45..089b69f 100644 --- a/lib/System/Introspector/State.pm +++ b/lib/System/Introspector/State.pm @@ -16,7 +16,7 @@ sub sudo_user { $_[0]->config->sudo_user } 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 { @@ -140,7 +140,7 @@ sub _create_gatherer { 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; diff --git a/t/logsetup.pl b/t/logsetup.pl index 5cba5a1..e46d2cb 100644 --- a/t/logsetup.pl +++ b/t/logsetup.pl @@ -15,7 +15,7 @@ package main; 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' }),