From: Tyler Riddle Date: Thu, 6 Sep 2012 02:49:20 +0000 (-0700) Subject: integrated log level control into command line and configuration file X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=418a886a8619ed227244bcd0afee3c451e244843;p=scpubgit%2FSystem-Introspector.git integrated log level control into command line and configuration file --- diff --git a/bin/system-introspector b/bin/system-introspector index a0de0de..d50bd4f 100755 --- a/bin/system-introspector +++ b/bin/system-introspector @@ -8,13 +8,13 @@ use File::Tree::Snapshot; use System::Introspector::Config; use System::Introspector::Logger qw( :log ); -log_debug { "$0 started executing" }; - GetOptions( 'c|config=s' => \my $config_file, 's|storage=s' => \my $storage_dir, 'a|all' => \my $update_all, 'g|group=s' => \my @update_groups, + 'd|debug' => \my $debug, + 'l|log-level=s' => \my $log_level, 'h|help' => sub { pod2usage(0) }, ) or pod2usage(2); @@ -27,34 +27,31 @@ die "Requires --config\n" die "Requires --all or --group option\n" unless $update_all or @update_groups; -log_trace { "Completed parsing command line options" }; +#command line arguments override the logging configuration +#and --log-level overrides -d +if (defined($debug) && ! defined($log_level)) { + $log_level = 'DEBUG'; +} $config_file = "$storage_dir/main.conf" unless defined $config_file; -log_debug { "Configuration file: '$config_file'"; }; - -my $config = System::Introspector::Config->new( - config_file => $config_file, -); +my %config_args = (config_file => $config_file); +$config_args{log_level} = $log_level if defined $log_level; -log_trace { "Completed building configuration" }; +my $config = System::Introspector::Config->new(%config_args); $config->has_group($_) or die "Unknown group '$_'\n" for @update_groups; @update_groups = $config->groups if $update_all; - + my $state = System::Introspector::State->new( config => $config, root => $storage_dir, ); -log_trace { "Completed building introspector state" }; - -log_info { "Ready to start gathering results" }; $state->gather(@update_groups); -log_info { "Completed gathering results" }; __END__ @@ -88,6 +85,15 @@ Fetch all groups. Fetch the specified group. Can be used multiple times. +=head2 -d, --debug + +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 + =head2 -h, --help Display help. diff --git a/lib/System/Introspector/Config.pm b/lib/System/Introspector/Config.pm index 028e7ae..5fe04df 100644 --- a/lib/System/Introspector/Config.pm +++ b/lib/System/Introspector/Config.pm @@ -7,6 +7,8 @@ has config => (is => 'lazy'); has config_file => (is => 'ro', required => 1); +has log_level => (is => 'lazy'); + sub _build_config { my ($self) = @_; my $reader = Config::General->new($self->config_file); @@ -15,6 +17,10 @@ sub _build_config { return \%config; } +sub _build_log_level { + return $_[0]->config->{log_level}; +} + sub sudo_user { $_[0]->config->{sudo_user} } sub groups { sort keys %{ $_[0]->config->{group} || {} } } diff --git a/lib/System/Introspector/Gatherer.pm b/lib/System/Introspector/Gatherer.pm index 42466f3..8f82de5 100644 --- a/lib/System/Introspector/Gatherer.pm +++ b/lib/System/Introspector/Gatherer.pm @@ -7,6 +7,16 @@ 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]; +} sub gather_all { my ($self) = @_; diff --git a/lib/System/Introspector/Logger.pm b/lib/System/Introspector/Logger.pm index 9033be7..8c852a4 100644 --- a/lib/System/Introspector/Logger.pm +++ b/lib/System/Introspector/Logger.pm @@ -1,16 +1,23 @@ use strictures 1; -BEGIN { $ENV{SYSTEM_INTROSPECTOR_LOG_UPTO} = "TRACE" unless exists $ENV{SYSTEM_INTROSPECTOR_LOG_UPTO} }; - package System::Introspector::Logger; use base qw(Log::Contextual); use System::Introspector::Logger::Output; use Log::Contextual qw( set_logger ); -sub arg_default_logger { $_[1] || System::Introspector::Logger::Output->new({ - env_prefix => 'SYSTEM_INTROSPECTOR_LOG', -}) }; +sub init_logging { + my ($self, $level) = @_; + + $level = 'WARN' unless defined $level; + + #TODO: better way to specify log level? + $ENV{SYSTEM_INTROSPECTOR_LOG_UPTO} = $level; + + set_logger(System::Introspector::Logger::Output->new({ + env_prefix => 'SYSTEM_INTROSPECTOR_LOG', + })); +} 1; \ No newline at end of file diff --git a/lib/System/Introspector/State.pm b/lib/System/Introspector/State.pm index 9d20c80..22e2a45 100644 --- a/lib/System/Introspector/State.pm +++ b/lib/System/Introspector/State.pm @@ -14,6 +14,11 @@ sub user { $_[0]->config->user } sub sudo_user { $_[0]->config->sudo_user } +sub BUILD { + my ($self) = @_; + System::Introspector::Logger->init_logging($self->config->log_level); +} + sub gather { my ($self, @groups) = @_; log_debug { "Starting to gather results" }; @@ -135,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); } 1;