From f7f0f01dd657b83d8c58dcdcba24b7066f789caf Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Wed, 20 Apr 2016 20:26:18 +0300 Subject: [PATCH] Bug 16313 - Koha::Logger doesn't forward the Log::Log4perl::Logger return value Log::Log4perl::Logger->$method returns undef if the log level is not logged or something bad happened and true or something similar on success. This allows chaining different loggers to trigger if Log4perl failed to log. Koha::Logger is hardwired to return 1 if Log4perl is configured properly regardless of whether the Log4perl actually succeeded in printing the log message. The following is a Log4perl best practice: $Koha::Logger->debug() or syslog("LOG_DEBUG", ...); which should revert to syslog() to log if Log4perl cannot for some reason do the logging. (eg. is disabled) This patch makes the Koha::Logger work with Log4perl better in tandem and return the proper return values. See unit tests. --- Koha/Logger.pm | 3 +-- t/Koha/Logger.t | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Koha/Logger.pm b/Koha/Logger.pm index f37865d..a4c91a9 100644 --- a/Koha/Logger.pm +++ b/Koha/Logger.pm @@ -118,8 +118,7 @@ sub AUTOLOAD { warn "$method: $line" if $line; } elsif ( $self->{logger}->can($method) ) { #use log4perl - $self->{logger}->$method($line); - return 1; + return $self->{logger}->$method($line); } else { # we should not really get here warn "ERROR: Unsupported method $method"; diff --git a/t/Koha/Logger.t b/t/Koha/Logger.t index d13d10e..32dae09 100644 --- a/t/Koha/Logger.t +++ b/t/Koha/Logger.t @@ -161,6 +161,25 @@ sub overloadLoggerConfigurations { #### END OF Test for KD976 - Koha::Logger overload configuration for command line scripts verbosity levels #### ############################################################################################################### +subtest "Return value passthrough", \&returnValuePassthrough; +sub returnValuePassthrough { + my ($logger, $log, $retval); + + $logger = Koha::Logger->get({category => "retval-madness-1"}); + $retval = $logger->trace('This is not printed'); + $log = t::Koha::Logger::slurpLog('wantArray'); + is(scalar(@$log), 0, "No trace written"); + is($retval, undef, "Koha::Logger returns undef per Log4perl best practices"); + t::Koha::Logger::clearLog(); + + $retval = $logger->error('This is printed'); + $log = t::Koha::Logger::slurpLog('wantArray'); + is(scalar(@$log), 1, "Error written"); + is($retval, 1, "Koha::Logger returns 1 per Log4perl best practices"); + t::Koha::Logger::clearLog(); +} +t::Koha::Logger::clearLog(); + sub _loggerBlarbAllLevels { my ($logger) = @_; $logger->trace('trace'); -- 1.9.1