From: Jonathan Prykop Date: Tue, 26 Jul 2016 21:42:54 +0000 (-0500) Subject: RT#38217: Send email when logging conditions are met [removed unwanted log levels... X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=14a5274c8b8cd2700d7141c8c15903bac881d46c RT#38217: Send email when logging conditions are met [removed unwanted log levels, preserved level num mapping] --- diff --git a/FS/FS/Log.pm b/FS/FS/Log.pm index 2fd002093..aed1f3969 100644 --- a/FS/FS/Log.pm +++ b/FS/FS/Log.pm @@ -5,13 +5,20 @@ use FS::Record qw(qsearch qsearchs); use FS::Conf; use FS::Log::Output; use FS::log; -use vars qw(@STACK @LEVELS); +use vars qw(@STACK %LEVELS); # override the stringification of @_ with something more sensible. BEGIN { - @LEVELS = qw(debug info notice warning error critical alert emergency); + # subset of Log::Dispatch levels + %LEVELS = ( + 0 => 'debug', + 1 => 'info', + 3 => 'warning', + 4 => 'error', + 5 => 'critical' + ); - foreach my $l (@LEVELS) { + foreach my $l (values %LEVELS) { my $sub = sub { my $self = shift; $self->log( level => $l, message => @_ ); @@ -100,4 +107,24 @@ sub DESTROY { splice(@STACK, $self->{'index'}, 1); # delete the stack entry } +=item levelnums + +Subroutine. Returns ordered list of level nums. + +=cut + +sub levelnums { + sort keys %LEVELS; +} + +=item levelmap + +Subroutine. Returns ordered map of level num => level name. + +=cut + +sub levelmap { + map { $_ => $LEVELS{$_} } levelnums; +} + 1; diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index 65b83bd5f..3faf47e24 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -352,6 +352,9 @@ sub upgrade_data { tie my %hash, 'Tie::IxHash', + #remap log levels + 'log' => [], + #cust_main (remove paycvv from history, locations, cust_payby, etc) 'cust_main' => [], diff --git a/FS/FS/log.pm b/FS/FS/log.pm index 1d4df730a..d432ee3c6 100644 --- a/FS/FS/log.pm +++ b/FS/FS/log.pm @@ -6,6 +6,8 @@ use FS::Record qw( qsearch qsearchs dbdef ); use FS::UID qw( dbh driver_name ); use FS::log_context; use FS::log_email; +use FS::upgrade_journal; +use Tie::IxHash; =head1 NAME @@ -115,7 +117,7 @@ sub insert { 'msgtype' => 'admin', 'to' => $log_email->to_addr, 'substitutions' => { - 'loglevel' => $FS::Log::LEVELS[$self->level], # which has hopefully been loaded... + 'loglevel' => $FS::Log::LEVELS{$self->level}, # which has hopefully been loaded... 'logcontext' => $log_email->context, # use the one that triggered the email 'logmessage' => $self->message, }, @@ -383,6 +385,49 @@ sub search { }; } +sub _upgrade_data { + my ($class, %opts) = @_; + + return if FS::upgrade_journal->is_done('log__remap_levels'); + + tie my %levelmap, 'Tie::IxHash', + 2 => 1, #notice -> info + 6 => 5, #alert -> critical + 7 => 5, #emergency -> critical + ; + + # this method should never autocommit + # should have been set in upgrade, but just in case... + local $FS::UID::AutoCommit = 0; + + # in practice, only debug/info/warning/error appear to have been used, + # so this probably won't do anything, but just in case + foreach my $old (keys %levelmap) { + # FS::log has no replace method + my $sql = 'UPDATE log SET level=' . dbh->quote($levelmap{$old}) . ' WHERE level=' . dbh->quote($old); + warn $sql unless $opts{'quiet'}; + my $sth = dbh->prepare($sql) or die dbh->errstr; + $sth->execute() or die $sth->errstr; + $sth->finish(); + } + + foreach my $log_email ( + qsearch('log_email',{ 'min_level' => 2 }), + qsearch('log_email',{ 'min_level' => 6 }), + qsearch('log_email',{ 'min_level' => 7 }), + ) { + $log_email->min_level($levelmap{$log_email->min_level}); + my $error = $log_email->replace; + if ($error) { + dbh->rollback; + die $error; + } + } + + FS::upgrade_journal->set_done('log__remap_levels'); + +} + =back =head1 BUGS diff --git a/httemplate/browse/log_email.html b/httemplate/browse/log_email.html index 0f64dd454..007ea6f74 100644 --- a/httemplate/browse/log_email.html +++ b/httemplate/browse/log_email.html @@ -21,7 +21,7 @@ ], 'fields' => [ 'logemailnum', sub { $_[0]->context || '(all)' }, - sub { $FS::Log::LEVELS[$_[0]->min_level] }, + sub { $FS::Log::LEVELS{$_[0]->min_level} }, 'msgname', 'to_addr', $actions, diff --git a/httemplate/edit/log_email.html b/httemplate/edit/log_email.html index 0c98046d3..b79aba986 100644 --- a/httemplate/edit/log_email.html +++ b/httemplate/edit/log_email.html @@ -16,8 +16,8 @@ }, { 'field' => 'min_level', 'type' => 'select', - 'options' => [ 0..7 ], - 'labels' => { map {$_ => $FS::Log::LEVELS[$_]} 0..7 }, + 'options' => [ &FS::Log::levelnums ], + 'labels' => { &FS::Log::levelmap }, 'curr_value' => scalar($cgi->param('min_level')), }, 'to_addr', diff --git a/httemplate/search/log.html b/httemplate/search/log.html index 111200f55..5b330f899 100644 --- a/httemplate/search/log.html +++ b/httemplate/search/log.html @@ -81,15 +81,15 @@ a:visited {text-decoration: none} Level <& /elements/select.html, field => 'min_level', - options => [ 0..7 ], - labels => { map {$_ => $FS::Log::LEVELS[$_]} 0..7 }, + options => [ &FS::Log::levelnums ], + labels => { &FS::Log::levelmap }, curr_value => $cgi->param('min_level'), &> to <& /elements/select.html, field => 'max_level', - options => [ 0..7 ], - labels => { map {$_ => $FS::Log::LEVELS[$_]} 0..7 }, + options => [ &FS::Log::levelnums ], + labels => { &FS::Log::levelmap }, curr_value => $cgi->param('max_level'), &> @@ -128,7 +128,7 @@ a:visited {text-decoration: none} <%once> my $date_sub = sub { time2str('%Y-%m-%d %T', $_[0]->_date) }; -my $level_sub = sub { $FS::Log::LEVELS[$_[0]->level] }; +my $level_sub = sub { $FS::Log::LEVELS{$_[0]->level} }; my $context_sub = sub { my $log = shift; @@ -191,18 +191,15 @@ my $object_link_sub = sub { } }; -my @colors = ( - '404040', #debug - '0000aa', #info - '00aa00', #notice - 'aa0066', #warning - '000000', #error - 'aa0000', #critical - 'ff0000', #alert - 'ff0000', #emergency +my %colors = ( + 0 => '404040', #debug, gray + 1 => '000000', #info, black + 3 => '0000aa', #warning, blue + 4 => 'aa0066', #error, purple + 5 => 'ff0000', #critical, red ); -my $color_sub = sub { $colors[ $_[0]->level ]; }; +my $color_sub = sub { $colors{ $_[0]->level }; }; my @contexts = ('', sort FS::log_context->contexts); @@ -212,10 +209,10 @@ die "access denied" unless $curuser->access_right([ 'View system logs', 'Configuration' ]); my @menubar = (); -push @menubar, qq(Configure conditions for sending email when logging), +push @menubar, qq(Configure conditions for sending email when logging); $cgi->param('min_level', 0) unless defined($cgi->param('min_level')); -$cgi->param('max_level', 7) unless defined($cgi->param('max_level')); +$cgi->param('max_level', 5) unless defined($cgi->param('max_level')); my %search = (); $search{'date'} = [ FS::UI::Web::parse_beginning_ending($cgi) ];