From 1a8ea961faa996502dce120c5c152488d75b40fb Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Sun, 24 Apr 2016 16:21:22 +0300 Subject: [PATCH] Bug 16329 - Koha::Logger->sql(). Unified interface to log SQL statements Instead of whenever needing to log SQL statements, making a custom logging output/function, introduce a centralized subroutine to handle all sql-statement logging. How the SQL-statements should be logged remains to be seen, but currently simply presenting very simple formatting we can discuss about. --- Koha/Logger.pm | 17 +++++++++++++++++ t/Koha/Logger.t | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Koha/Logger.pm b/Koha/Logger.pm index 02fa301..1cd2ac4 100644 --- a/Koha/Logger.pm +++ b/Koha/Logger.pm @@ -91,6 +91,23 @@ sub get { return $self; } +=head2 sql + + $logger->sql('debug', $sql, $params) if $logger->is_debug(); + +Log SQL-statements using a unified interface. +@param {String} Log level +@param {String} SQL-command +@param {ArrayRef} SQL prepared statement parameters +@returns whatever Log::Log4perl returns + +=cut + +sub sql { + my ($self, $level, $sql, $params) = @_; + return $self->$level("$sql -- @$params"); +} + =head1 INTERNALS =head2 AUTOLOAD diff --git a/t/Koha/Logger.t b/t/Koha/Logger.t index 32dae09..af4580e 100644 --- a/t/Koha/Logger.t +++ b/t/Koha/Logger.t @@ -180,6 +180,19 @@ sub returnValuePassthrough { } t::Koha::Logger::clearLog(); + +subtest "Koha::Logger->sql()", \&sqlsql; +sub sqlsql { + my $logger = Koha::Logger->get({category => "sqlsql-1"}); + my $retval = $logger->sql('fatal', 'SELECT * FROM a WHERE b=? AND c=? OR d=?', [1,2,3]) if $logger->is_fatal(); + my $log = t::Koha::Logger::slurpLog('wantArray'); + is(scalar(@$log), 1, "One entry written"); + is($retval, 1, "Koha::Logger->sql() returns 'true' per Log4perl best practices"); + ok($log->[0] =~ /SELECT \* FROM a WHERE b=\? AND c=\? OR d=\? -- 1 2 3/); + t::Koha::Logger::clearLog(); +} + + sub _loggerBlarbAllLevels { my ($logger) = @_; $logger->trace('trace'); -- 1.9.1