Koha/t/Logger.t
Kyle M Hall 26014e62da
Bug 35907: Add ability to log all custom report runs with or without query
Because of the way Koha::Logger has been used to log to different categories based on the interface and caller, it can be extremely hard to log all of a particular log statement to one place.

For custom report runs, the category is plack-intranet.C4::Reports::Guided when run from the web interface, cron.C4::Reports::Guided when run from runreport.pl, and plack-intranet.C4::Auth when run from svc/report.

We should add a more standardized report run log, both with and without the full query, so that administrators can log all report runs to a centralized location. If an administrator were to need the "point of entry" for reports, it is easy to include via parameters in PatternLayout.

Test Plan:
1) Apply this patch
2) Modify your log4perl file, add the following:

log4perl.logger.reports.execute.time = INFO, REPORTTIME
log4perl.appender.REPORTTIME=Log::Log4perl::Appender::File
log4perl.appender.REPORTTIME.filename=/tmp/report-time.log
log4perl.appender.REPORTTIME.mode=append
log4perl.appender.REPORTTIME.layout=PatternLayout
log4perl.appender.REPORTTIME.layout.ConversionPattern=[%d] [%p] [%P] %m%n
log4perl.appender.REPORTTIME.utf8=1

log4perl.logger.reports.execute.query = INFO, REPORTQUERY
log4perl.appender.REPORTQUERY=Log::Log4perl::Appender::File
log4perl.appender.REPORTQUERY.filename=/tmp/report-query.log
log4perl.appender.REPORTQUERY.mode=append
log4perl.appender.REPORTQUERY.layout=PatternLayout
log4perl.appender.REPORTQUERY.layout.ConversionPattern=[%d] [%p] [%P] %m%n
log4perl.appender.REPORTQUERY.utf8=1

3) Restart all the things!
4) Run a report somehow:
   CLI: ./misc/cronjobs/runreport.pl 1
   API: /cgi-bin/koha/svc/report?id=1
   Web: /cgi-bin/koha/reports/guided_reports.pl?reports=1&phase=Run this report
5) Note the report runs are logged to /tmp/report-time.log and /tmp/report-query.log

Signed-off-by: Brendan Lawlor <blawlor@clamsnet.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-05-10 16:45:45 +02:00

102 lines
3.5 KiB
Perl
Executable file

#!/usr/bin/perl
# 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, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use C4::Context;
use Koha::Logger;
use t::lib::Mocks;
use File::Temp qw/tempfile/;
use Test::More tests => 1;
use Test::Warn;
use Test::Exception;
subtest 'Test01 -- Simple tests for Koha::Logger' => sub {
plan tests => 13;
my $ret;
t::lib::Mocks::mock_config('log4perl_conf', undef);
throws_ok { Koha::Logger->get } qr/Configuration not defined/, 'Logger did not init correctly without config';
my $log = mytempfile();
my $config_file = mytempfile( <<"HERE"
log4perl.logger.intranet = WARN, INTRANET
log4perl.appender.INTRANET=Log::Log4perl::Appender::File
log4perl.appender.INTRANET.filename=$log
log4perl.appender.INTRANET.mode=append
log4perl.appender.INTRANET.layout=PatternLayout
log4perl.appender.INTRANET.layout.ConversionPattern=[%d] [%p] %m %l%n
HERE
);
t::lib::Mocks::mock_config('log4perl_conf', $config_file);
my $login = getlogin || getpwuid($<) || q{};
SKIP: {
skip "Running as root user", 1 if $login eq 'root';
system("chmod 400 $log");
throws_ok { Koha::Logger->get } qr/Permission denied/, 'Logger did not init correctly without permission';
system("chmod 700 $log");
}
my $logger = Koha::Logger->get( { interface => 'intranet' } );
is( exists $logger->{logger}, 1, 'Log4perl config found');
is( $logger->warn('Message 1'), 1, '->warn returned a value' );
warning_is { $ret = $logger->catastrophe }
"ERROR: Unsupported method catastrophe",
"Undefined method raises warning";
is( $ret, undef, "'catastrophe' method undefined");
Koha::Logger->put_mdc( 'foo', 'bar' );
is( Koha::Logger->get_mdc( 'foo' ), 'bar', "MDC value via put_mdc is correct" );
Koha::Logger->put_mdc( 'foo', undef );
is( Koha::Logger->get_mdc( 'foo' ), undef, "Updated MDC value to undefined via put_mdc is correct" );
Koha::Logger->put_mdc( 'foo', 'baz' );
is( Koha::Logger->get_mdc( 'foo' ), 'baz', "Updated MDC value via put_mdc is correct" );
Koha::Logger->clear_mdc();
is( Koha::Logger->get_mdc( 'foo' ), undef, "MDC value was cleared by clear_mdc" );
is(
Koha::Logger->get( { interface => 'cli', category => 'Test.Category' } )->category(), "cli.Test.Category",
"Category is cli.Test.Category"
);
$ENV{'PLACK_ENV'} = 1;
is(
Koha::Logger->get( { interface => 'cli', category => 'Test.Category' } )->category(),
"plack-cli.Test.Category", "Category under plack is is plack-cli.Test.Category"
);
is(
Koha::Logger->get( { interface => 'cli', category => 'Test.Category', prefix => 0 } )->category(),
"cli.Test.Category", "Category under plack with prefixing disabled is is cli.Test.Category"
);
delete $ENV{'PLACK_ENV'};
};
sub mytempfile {
my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 );
print $fh $_[0]//'';
close $fh;
return $fn;
}