From: Tyler Riddle Date: Wed, 10 Oct 2012 16:17:45 +0000 (-0700) Subject: vastly improved command line logging configuration and warn on errors by default X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d0331dad141492fa1714940ab7dab60549395cd2;p=scpubgit%2FSystem-Introspector.git vastly improved command line logging configuration and warn on errors by default --- diff --git a/bin/system-introspector b/bin/system-introspector index e5f939c..2e5db02 100755 --- a/bin/system-introspector +++ b/bin/system-introspector @@ -6,13 +6,17 @@ use Pod::Usage; use System::Introspector::State; use File::Tree::Snapshot; use System::Introspector::Config; -use System::Introspector::Logger qw(:log); +use System::Introspector::Logger qw(:log); +use System::Introspector::Logger::VerboseOutput; +use System::Introspector::Logger::Output; +use System::Introspector::Logger::WarnOutput; GetOptions( 'c|config=s' => \my $config_file, 's|storage=s' => \my $storage_dir, 'a|all' => \my $update_all, 'g|group=s' => \my @update_groups, + 'v|verbose' => \my $verbose, 'd|debug' => \my $debug, 'l|log-level=s' => \my $log_level, 'h|help' => sub { pod2usage(0) }, @@ -32,13 +36,34 @@ if (defined($debug) && ! defined($log_level)) { $log_level = 'debug'; } -my $log_destination; +my ($stderr_destination, $stdout_destination, $warn_destination); + +$warn_destination = Object::Remote::LogDestination->new({ + logger => System::Introspector::Logger::WarnOutput->new({ + levels_upto => 'warn', + }), +}); + +$warn_destination->connect(System::Introspector::Logger->arg_router); + if (defined($log_level)) { - $log_destination = Object::Remote::LogDestination->new( - logger => Log::Contextual::SimpleLogger->new({ levels_upto => $log_level }), + $stderr_destination = Object::Remote::LogDestination->new( + logger => System::Introspector::Logger::Output->new({ + levels_upto => $log_level, + }), ); - $log_destination->connect(System::Introspector::Logging->arg_router); + $stderr_destination->connect(System::Introspector::Logger->arg_router); +} + +if (defined($verbose)) { + $stdout_destination = Object::Remote::LogDestination->new( + logger => System::Introspector::Logger::VerboseOutput->new({ + levels_upto => 'info', + }), + ); + + $stdout_destination->connect(System::Introspector::Logger->arg_router); } $config_file = "$storage_dir/main.conf" unless defined $config_file; @@ -90,14 +115,18 @@ Fetch all groups. Fetch the specified group. Can be used multiple times. +=head2 -v, --verbose + +Output progress information to STDOUT + =head2 -d, --debug -Force the log level to DEBUG unless -l or --log-level is specified +Force the log level to debug unless -l or --log-level is specified =head2 -l , --log-level -Set the log level to the value specified; this will override -d. Valid levels are -TRACE DEBUG INFO WARN ERROR FATAL +Set the log level to the value specified and output logs to STDERR; this +will override -d. Valid levels are trace debug info warn error fatal =head2 -h, --help diff --git a/lib/System/Introspector/Logger/Output.pm b/lib/System/Introspector/Logger/Output.pm index cd7b8bf..6b86597 100644 --- a/lib/System/Introspector/Logger/Output.pm +++ b/lib/System/Introspector/Logger/Output.pm @@ -2,30 +2,31 @@ package System::Introspector::Logger::Output; use strictures 1; -#using simple logger because the configuration -#interface is slightly nicer -use Log::Contextual::SimpleLogger; -use base qw ( Log::Contextual::SimpleLogger ); +use base qw( Log::Contextual::SimpleLogger ); -sub set_execution_context { - my ($self, $context_name) = @_; +sub _output { + my ($self, @msg) = @_; - die "must specify an execution context name" - unless defined $context_name; + print STDERR @msg or die "Could not print to STDERR: $!"; +} + +sub _format { + my ($self, $level, @msg) = @_; + my $logbuf = join('', @msg); + my @timedata = localtime; + my $message = sprintf("[%0.2i:%0.2i:%0.2i %s] %s", $timedata[2], $timedata[1], $timedata[0], $level, $logbuf); - $self->{_introspector}->{context_name} = $context_name; + return $message; } +#TODO this should be called _format 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$/; - my $context = $self->{_introspector}->{context_name}; - $context = 'undefined' unless defined $context; - warn "[$level $time] [$context] $message"; + my $formatted = $self->_format(@_); + + $formatted .= "\n" unless $formatted =~ /\n$/; + + $self->_output($formatted); } 1; diff --git a/lib/System/Introspector/Logger/VerboseOutput.pm b/lib/System/Introspector/Logger/VerboseOutput.pm new file mode 100644 index 0000000..be5d434 --- /dev/null +++ b/lib/System/Introspector/Logger/VerboseOutput.pm @@ -0,0 +1,20 @@ +package System::Introspector::Logger::VerboseOutput; + +use strictures 1; + +use base qw(System::Introspector::Logger::Output); + +sub _output { + my ($self, $msg) = @_; + + print $msg or die "Could not print: $!"; +} + +sub _format { + my ($self, $level, $msg) = @_; + + return join('', $msg); +} + +1; + diff --git a/lib/System/Introspector/Logger/WarnOutput.pm b/lib/System/Introspector/Logger/WarnOutput.pm new file mode 100644 index 0000000..0964631 --- /dev/null +++ b/lib/System/Introspector/Logger/WarnOutput.pm @@ -0,0 +1,14 @@ +package System::Introspector::Logger::WarnOutput; + +use strictures 1; + +use base qw(System::Introspector::Logger::VerboseOutput ); + +sub output { + my ($self, $msg) = @_; + + warn $msg; +} + +1; + diff --git a/lib/System/Introspector/State.pm b/lib/System/Introspector/State.pm index 21b2eef..2bbfcdf 100644 --- a/lib/System/Introspector/State.pm +++ b/lib/System/Introspector/State.pm @@ -23,7 +23,7 @@ sub gather { for my $group (@groups) { my @hosts = $self->config->hosts; - log_info { my $c = scalar(@hosts); "This gather run is for $c hosts" }; + log_debug { my $c = scalar(@hosts); "This gather run is for $c hosts" }; while(scalar(@hosts) > 0) { my (@scan, @waiting); @@ -44,7 +44,7 @@ sub gather { eval { $to_fetch = [$host, $self->fetch($host, $group)] }; if ($@) { - log_trace { "Could not start fetching for $host: '$@'" }; + log_error { "Could not start fetching for $host: $@" }; next; } @@ -60,7 +60,7 @@ sub gather { eval { ($report) = await_all @futures }; if ($@) { - log_error { "Failure when probing '$host': $@" }; + log_error { "Failure when probing $host: $@" }; next; } @@ -120,12 +120,12 @@ sub storage { sub _store { my ($self, $host, $group, $gathered) = @_; - log_info { "Storing data for group '$group' on '$host'" }; + log_info { "Storing data for group $group on $host" }; my $storage = $self->storage($host, $group); my $ok = eval { my @files; for my $class ($gathered->probe_names) { - log_trace { "Storing data for probe name '$class'" }; + log_info { "Storing data for probe name $class" }; my $file = sprintf '%s.json', join '/', map lc, map { s{([a-z0-9])([A-Z])}{${1}_${2}}g; @@ -136,13 +136,12 @@ sub _store { log_trace { "Collecting probe data for '$class' from '$host'" }; my $data = $gathered->get_probe_data($class); log_debug { "Generated file name for storage: '$file'; Writing state to '$full_path'" }; - Dlog_trace { "Input to storage engine: $_" } $data; print $fh encode_json($gathered->get_probe_data($class)); push @files, $full_path; log_trace { "Finished storing data for '$class'" }; } $self->_cleanup($storage, [@files]); - log_trace { "Comitting stored data" }; + log_info { "Comitting stored data" }; $storage->commit; };