[freeside-commits] branch master updated. 3e28d2155b1e797cf77a30b6bb381bc61cec9d8b

Jonathan Prykop jonathan at 420.am
Tue Jul 26 14:46:40 PDT 2016


The branch, master has been updated
       via  3e28d2155b1e797cf77a30b6bb381bc61cec9d8b (commit)
       via  14a5274c8b8cd2700d7141c8c15903bac881d46c (commit)
      from  a1930173f49200333e347b87c01c3edabc1ebb9c (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 3e28d2155b1e797cf77a30b6bb381bc61cec9d8b
Author: Jonathan Prykop <jonathan at freeside.biz>
Date:   Tue Jul 26 16:44:10 2016 -0500

    RT#38217: Send email when logging conditions are met [fix to warning invocation in cust_bill_pkg_tax_location]

diff --git a/FS/FS/cust_bill_pkg_tax_location.pm b/FS/FS/cust_bill_pkg_tax_location.pm
index 9a1f22a..7c67c2d 100644
--- a/FS/FS/cust_bill_pkg_tax_location.pm
+++ b/FS/FS/cust_bill_pkg_tax_location.pm
@@ -338,7 +338,7 @@ sub upgrade_taxable_billpkgnum {
         } #for $i
       } else {
         # the more complicated case
-        $log->warn("mismatched charges and tax links in pkg#$pkgnum",
+        $log->warning("mismatched charges and tax links in pkg#$pkgnum",
           object => $cust_bill);
         my $tax_amount = sum(map {$_->amount} @tax_links);
         # remove all tax link records and recreate them to be 1:1 with 

commit 14a5274c8b8cd2700d7141c8c15903bac881d46c
Author: Jonathan Prykop <jonathan at freeside.biz>
Date:   Tue Jul 26 16:42:54 2016 -0500

    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 2fd0020..aed1f39 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 65b83bd..3faf47e 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 1d4df73..d432ee3 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 0f64dd4..007ea6f 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 0c98046..b79aba9 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 111200f..5b330f8 100644
--- a/httemplate/search/log.html
+++ b/httemplate/search/log.html
@@ -81,15 +81,15 @@ a:visited {text-decoration: none}
   <TD>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'),
     &>
   </TD>
@@ -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);
 </%once>
@@ -212,10 +209,10 @@ die "access denied"
   unless $curuser->access_right([ 'View system logs', 'Configuration' ]);
 
 my @menubar = ();
-push @menubar, qq(<A HREF="${fsurl}browse/log_email.html" STYLE="text-decoration: underline;">Configure conditions for sending email when logging</A>),
+push @menubar, qq(<A HREF="${fsurl}browse/log_email.html" STYLE="text-decoration: underline;">Configure conditions for sending email when logging</A>);
 
 $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) ];

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/Log.pm                        |   33 +++++++++++++++++++++---
 FS/FS/Upgrade.pm                    |    3 +++
 FS/FS/cust_bill_pkg_tax_location.pm |    2 +-
 FS/FS/log.pm                        |   47 ++++++++++++++++++++++++++++++++++-
 httemplate/browse/log_email.html    |    2 +-
 httemplate/edit/log_email.html      |    4 +--
 httemplate/search/log.html          |   31 +++++++++++------------
 7 files changed, 97 insertions(+), 25 deletions(-)




More information about the freeside-commits mailing list