From: Robert 'phaylon' Sedlacek Date: Wed, 3 Oct 2012 16:54:07 +0000 (+0000) Subject: move logging output to Log::Contextual and add a --verbose/-v option X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=72599a55f7d85754e51a71cc582aa3f82a1c93b8;p=scpubgit%2FSystem-Introspector-Report.git move logging output to Log::Contextual and add a --verbose/-v option --- diff --git a/bin/system-introspector-report b/bin/system-introspector-report index 505c025..6b1032e 100755 --- a/bin/system-introspector-report +++ b/bin/system-introspector-report @@ -5,6 +5,8 @@ use Pod::Usage; use Try::Tiny; use System::Introspector::Report::Source; use System::Introspector::Report::Config; +use Log::Contextual qw( set_logger ); +use Log::Contextual::SimpleLogger; GetOptions( 's|storage=s' => \my $storage_dir, @@ -12,6 +14,7 @@ GetOptions( 'r|report=s' => \my @report_types, 'p|publish=s' => \my @publish_types, 'a|all' => \my $all_reports, + 'v|verbose+' => \my $log_verbose, 'h|help' => sub { pod2usage(0) }, ) or pod2usage(2); @@ -21,6 +24,16 @@ die "$0 requires --storage (-s) to be specified\n" die "$0 requires --config (-c) to be specified\n" unless defined $config_file; +my @additional_log_levels = qw( debug trace ); +my $logger = Log::Contextual::SimpleLogger->new({ + levels => [ + qw( error fatal info ), + grep { defined } + @additional_log_levels[0 .. (($log_verbose || 0) - 1)], + ], +}); +set_logger $logger; + my $config = System::Introspector::Report::Config ->new(config_file => $config_file); diff --git a/lib/System/Introspector/Report/Publish/MediaWiki.pm b/lib/System/Introspector/Report/Publish/MediaWiki.pm index 274b6c1..b6ecab9 100644 --- a/lib/System/Introspector/Report/Publish/MediaWiki.pm +++ b/lib/System/Introspector/Report/Publish/MediaWiki.pm @@ -1,6 +1,7 @@ package System::Introspector::Report::Publish::MediaWiki; use Moo; use Try::Tiny; +use Log::Contextual qw( :log ); use aliased 'System::Introspector::Report::Publish::MediaWiki::Connection'; has page_options => ( @@ -40,11 +41,11 @@ sub _build_connection { sub publish { my ($self, $reports) = @_; my $pages = $self->page_options; - printf "Pushing reports to MediaWiki at '%s'\n", $self->api_uri; + log_info { "Pushing reports to MediaWiki at '%s'", $self->api_uri }; for my $page (sort keys %$pages) { $self->_publish_page($reports, $page, $pages->{$page}); } - print "Finished pushing to MediaWiki\n"; + log_info { "Finished pushing to MediaWiki" }; return 1; } @@ -89,27 +90,27 @@ sub _publish_page { } my $sorted = $self->_sort_reports($reports, $options->{report} || []); unless (scalar @$sorted) { - print "Skipping page '$page_name': No reports to publish\n"; + log_debug { "Skipping page '$page_name': No reports to publish" }; return 1; } - print "Updating page '$page_name'\n"; + log_debug { "Updating page '$page_name'" }; my $do_create = $options->{create}; return try { my $page = $self->get_page($page_name); if ($page->is_new and not $do_create) { - print "Skipping page '$page_name': Does not yet exist\n"; + log_debug { "Skipping page '$page_name': Does not yet exist" }; return 1; } my $is_changed = $page->update($sorted); unless ($is_changed) { - print "Not pushing page '$page_name': No changes\n"; + log_Debug { "Not pushing page '$page_name': No changes" }; return 1; } $self->put_page($page); return 1; } catch { - print "Error during page update: $_\n"; + log_error { "Error during page update: $_" }; return 1; }; }