Bug 14167: Add Koha::Logger based on Log4perl
authorKyle M Hall <kyle@bywatersolutions.com>
Thu, 7 May 2015 15:20:50 +0000 (11:20 -0400)
committerTomas Cohen Arazi <tomascohen@unc.edu.ar>
Tue, 21 Jul 2015 13:15:40 +0000 (10:15 -0300)
Koha needs a better logger, and it seems like the best solution would be
to take advantage of Log4perl which is already a fully featured logger.
We use Log4perl to selectively decide what statements should be logged,
and where they should go!

Test plan:
0) Install Log::Log4perl via packages or cpan
1) Apply this patch and the example renewal patch
2) Copy etc/log4perl.conf to your koha conf directory, edit the paths
   to match your current error logs
3) Edit your koha-conf file and add the
   <log4perl_conf>/path/to/log4perl.conf</log4perl_conf> line
4) Watch your intranet and opac error logs
5) Perform a renewal via the staff interface, note there is nothing new
   in the log file
7) Update the log4perl.conf, change the log level from WARN to TRACE
   for both the staff and opac sides
8) Perform a renewal via the staff interface, note the logged lines
9) Perform a renewal via the opac, note the logged lines

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Amended this patch: Moved the renewal stuff to a separate example patch.
And upgraded the DEBUG level to WARN in the log4perl config file.
Signed-off-by: Tomas Cohen Arazi <tomascohen@unc.edu.ar>
C4/Installer/PerlDependencies.pm
Koha/Logger.pm [new file with mode: 0644]
etc/koha-conf.xml
etc/log4perl.conf [new file with mode: 0644]

index 9ddeca8..27554e4 100644 (file)
@@ -737,6 +737,11 @@ our $PERL_DEPS = {
         'required' => '0',
         'min_ver'  => '0.89',
     },
+    'Log::Log4perl' => {
+        'usage'    => 'Core',
+        'required' => '1',
+        'min_ver'  => '1.29',
+    },
 };
 
 1;
diff --git a/Koha/Logger.pm b/Koha/Logger.pm
new file mode 100644 (file)
index 0000000..2fcbb6f
--- /dev/null
@@ -0,0 +1,75 @@
+package Koha::Logger;
+
+# Copyright 2015 ByWater Solutions
+# kyle@bywatersolutions.com
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+=head1 NAME
+
+Koha::Log
+
+=head1 SYNOPSIS
+
+  use Koha::Log;
+
+=head1 FUNCTIONS
+
+=cut
+
+use Modern::Perl;
+
+use Log::Log4perl;
+use Carp;
+
+use C4::Context;
+
+BEGIN {
+    Log::Log4perl->wrapper_register(__PACKAGE__);
+
+    my $conf;
+    if ( exists $ENV{"LOG4PERL_CONF"} and $ENV{'LOG4PERL_CONF'} and -s $ENV{"LOG4PERL_CONF"} ) {
+
+        # Check for web server level configuration first
+        $conf = $ENV{"LOG4PERL_CONF"};
+    }
+    else {
+        # If no web server level config exists, look in the koha conf file for one
+        $conf = C4::Context->config("log4perl_conf");
+    }
+
+    Log::Log4perl->init_once($conf);
+}
+
+sub get {
+    my ( $class, $category, $interface ) = @_;
+
+    croak("No category passed in!") unless $category;
+
+    $interface ||= C4::Context->interface();
+
+    return Log::Log4perl->get_logger("$interface.$category");
+}
+
+=head1 AUTHOR
+
+Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>
+
+=cut
+
+1;
+
+__END__
index 289d51d..cb6ed05 100644 (file)
@@ -114,6 +114,7 @@ __PAZPAR2_TOGGLE_XML_POST__
  <zebra_lockdir>__ZEBRA_LOCK_DIR__</zebra_lockdir>
  <use_zebra_facets>1</use_zebra_facets>
  <queryparser_config>__KOHA_CONF_DIR__/searchengine/queryparser.yaml</queryparser_config>
+ <log4perl_conf>__KOHA_CONF_DIR__/log4perl.conf</log4perl_conf>
 
  <!-- true type font mapping accoding to type from $font_types in C4/Creators/Lib.pm -->
  <ttf>
diff --git a/etc/log4perl.conf b/etc/log4perl.conf
new file mode 100644 (file)
index 0000000..efc4707
--- /dev/null
@@ -0,0 +1,13 @@
+log4perl.logger.intranet = WARN, INTRANET
+log4perl.appender.INTRANET=Log::Log4perl::Appender::File
+log4perl.appender.INTRANET.filename=__LOG_DIR__/intranet-error.log
+log4perl.appender.INTRANET.mode=append
+log4perl.appender.INTRANET.layout=PatternLayout
+log4perl.appender.INTRANET.layout.ConversionPattern=[%d] [%p] %m %l %n
+
+log4perl.logger.opac = WARN, OPAC
+log4perl.appender.OPAC=Log::Log4perl::Appender::File
+log4perl.appender.OPAC.filename=__LOG_DIR__/opac-error.log
+log4perl.appender.OPAC.mode=append
+log4perl.appender.OPAC.layout=PatternLayout
+log4perl.appender.OPAC.layout.ConversionPattern=[%d] [%p] %m %l %n