View | Details | Raw Unified | Return to bug 16304
Collapse All | Expand All

(-)a/Koha/Logger.pm (+21 lines)
Lines 45-50 BEGIN { Link Here
45
    Log::Log4perl->wrapper_register(__PACKAGE__);
45
    Log::Log4perl->wrapper_register(__PACKAGE__);
46
}
46
}
47
47
48
=head2 new
49
50
    my $logger = Koha::Logger->new($params);
51
52
See get() for available $params.
53
Prepares the logger for lazyLoading if uncertain whether or not the environment is set.
54
This is meant to be used to instantiate package-level loggers.
55
56
=cut
57
58
sub new {
59
    my ($class, $params) = @_;
60
    my $self = {lazyLoad => $params}; #Mark self as lazy loadable
61
    bless $self, $class;
62
    return $self;
63
}
64
48
=head2 get
65
=head2 get
49
66
50
    Returns a logger object (based on log4perl).
67
    Returns a logger object (based on log4perl).
Lines 86-91 sub AUTOLOAD { Link Here
86
    my $method = $Koha::Logger::AUTOLOAD;
103
    my $method = $Koha::Logger::AUTOLOAD;
87
    $method =~ s/^Koha::Logger:://;
104
    $method =~ s/^Koha::Logger:://;
88
105
106
    if ($self->{lazyLoad}) { #We have created this logger to be lazy loadable
107
        $self = ref($self)->get( $self->{lazyLoad} ); #Lazy load me!
108
    }
109
89
    if ( !exists $self->{logger} ) {
110
    if ( !exists $self->{logger} ) {
90
111
91
        #do not use log4perl; no print to stderr
112
        #do not use log4perl; no print to stderr
(-)a/t/Koha/Logger.pm (+83 lines)
Line 0 Link Here
1
package t::Koha::Logger;
2
3
use Modern::Perl;
4
5
my $logfile = '/tmp/log4perl_test.log';
6
7
=head2 getLog4perlConfig
8
9
@returns {ScalarRef} Get the default and reasonable test config String
10
11
=cut
12
13
sub getLog4perlConfig {
14
    my $cfg = qq(
15
        layout_class   = Log::Log4perl::Layout::PatternLayout
16
        layout_pattern = [%c] [%d] [%p] %m %l %n
17
18
        log4perl.logger.intranet = WARN, INTRANET
19
        log4perl.appender.INTRANET=Log::Log4perl::Appender::File
20
        log4perl.appender.INTRANET.filename=$logfile
21
        log4perl.appender.INTRANET.mode=append
22
        log4perl.appender.INTRANET.layout=\${layout_class}
23
        log4perl.appender.INTRANET.layout.ConversionPattern=\${layout_pattern}
24
25
        log4perl.logger.opac = WARN, OPAC
26
        log4perl.appender.OPAC=Log::Log4perl::Appender::File
27
        log4perl.appender.OPAC.filename=$logfile
28
        log4perl.appender.OPAC.mode=append
29
        log4perl.appender.OPAC.layout=\${layout_class}
30
        log4perl.appender.OPAC.layout.ConversionPattern=\${layout_pattern}
31
32
        log4perl.logger.commandline = WARN, CLI
33
        log4perl.appender.CLI=Log::Log4perl::Appender::File
34
        log4perl.appender.CLI.filename=$logfile
35
        log4perl.appender.CLI.mode=append
36
        log4perl.appender.CLI.layout=\${layout_class}
37
        log4perl.appender.CLI.layout.ConversionPattern=\${layout_pattern}
38
    );
39
    return \$cfg;
40
}
41
42
43
=head2 slurpLog
44
45
@param {Boolean} $asArrayRef, should we return an ArrayRef of log file lines or just the logfile contents as String?
46
@returns {ArrayRef of Strings or String}
47
48
=cut
49
50
sub slurpLog {
51
    my ($asArrayRef) = @_;
52
    open(my $FH, '<', $logfile) or die $!;
53
    my @log = <$FH>;
54
    close($FH);
55
    return \@log if $asArrayRef;
56
    return join("\n", @log);
57
}
58
59
=head2 getFirstLogRow
60
61
@returns {String}, first line in the log file
62
63
=cut
64
65
sub getFirstLogRow {
66
    open(my $FH, '<', $logfile) or die $!;
67
    my $firstRow = <$FH>;
68
    close($FH);
69
    return $firstRow;
70
}
71
72
=head2 clearLog
73
74
Empties the test log
75
76
=cut
77
78
sub clearLog {
79
    open(my $FH, '>', $logfile) or die $!;
80
    close($FH);
81
}
82
83
1; #Satisfy insanity
(-)a/t/Koha/Logger.t (+60 lines)
Line 0 Link Here
1
# Copyright 2016 KohaSuomi
2
#
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
use Test::More;
20
21
use Log::Log4perl;
22
use C4::Context;
23
C4::Context->interface('commandline'); #Set the context already prior to loading Koha::Logger (this actually doesn't fix the logger interface definition issue. Just doing it like this to show this doesn't work)
24
is(C4::Context->interface(), 'commandline', "Current Koha interface is 'commandline'"); #Show in test output what we are expecting to make test plan more understandable
25
26
#Initialize the Log4perl to write to /tmp/log4perl_test.log so we can clean it later
27
Log::Log4perl::init( t::Koha::Logger::getLog4perlConfig() );
28
29
use Koha::Logger;
30
use t::Koha::Logger;
31
32
33
##################################################################################################
34
#### Test for Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set  ####
35
use t::Koha::Logger_Invoker; #Inside the invoker we already get the Koha's interface from C4::Context->interface. Unfortunately use-definitions are resolved before other code is executed, so the context is not yet properly set.
36
use t::Koha::Logger_InvokerLazyLoad; #We set the reference to the Koha::Logger as package-level, but dont load the Log::Log4perl-object yet with uninitialized environment
37
38
subtest "Logger_Invoker package-level logger has a bad interface", \&Logger_Invoker_badInterface;
39
sub Logger_Invoker_badInterface {
40
    t::Koha::Logger_Invoker::arbitrarySubroutineWritingToLog();
41
42
    my $log = t::Koha::Logger::slurpLog('wantArray');
43
    ok($log->[0] =~ /\[opac\./, "Log entry doesn't have 'commandline'-interface, but the default 'opac' instead");
44
}
45
t::Koha::Logger::clearLog();
46
47
subtest "Logger_InvokerLazyLoad package-level logger has the correct interface", \&Logger_InvokerLazyLoad_correctInterface;
48
sub Logger_InvokerLazyLoad_correctInterface {
49
    t::Koha::Logger_InvokerLazyLoad::arbitrarySubroutineWritingToLog();
50
51
    my $log = t::Koha::Logger::slurpLog('wantArray');
52
    ok($log->[0] =~ /\[commandline\./, "Log entry has 'commandline'-interface");
53
}
54
t::Koha::Logger::clearLog();
55
56
#### END OF Test for Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set  ####
57
#########################################################################################################
58
59
60
done_testing();
(-)a/t/Koha/Logger_Invoker.pm (+11 lines)
Line 0 Link Here
1
package t::Koha::Logger_Invoker;
2
3
use Modern::Perl;
4
5
use Koha::Logger;
6
7
my $logger = Koha::Logger->get({category => __PACKAGE__});
8
9
sub arbitrarySubroutineWritingToLog {
10
    $logger->error('A run-of-a-mill -error');
11
}
(-)a/t/Koha/Logger_InvokerLazyLoad.pm (+11 lines)
Line 0 Link Here
1
package t::Koha::Logger_InvokerLazyLoad;
2
3
use Modern::Perl;
4
5
use Koha::Logger;
6
7
my $logger = Koha::Logger->new({category => __PACKAGE__});
8
9
sub arbitrarySubroutineWritingToLog {
10
    $logger->error('A run-of-a-mill -error');
11
}
(-)a/t/Koha/Logger_performance.t (-27 / +6 lines)
Lines 24-29 use Time::HiRes; Link Here
24
use C4::Context;
24
use C4::Context;
25
use Koha::Logger;
25
use Koha::Logger;
26
26
27
use t::Koha::Logger;
28
27
C4::Context->interface('intranet');
29
C4::Context->interface('intranet');
28
30
29
my $acceptedDelay; #This is baselined from the vanilla Log4perl subtest
31
my $acceptedDelay; #This is baselined from the vanilla Log4perl subtest
Lines 39-47 my $acceptedPerformanceLoss = 1.33; #33% Link Here
39
=cut
41
=cut
40
42
41
#Initialize the Log4perl to write to /tmp/log4perl_test.log so we can clean it later
43
#Initialize the Log4perl to write to /tmp/log4perl_test.log so we can clean it later
42
my $conf = join("\n",<DATA>);
44
Log::Log4perl::init( t::Koha::Logger::getLog4perlConfig() );
43
Log::Log4perl::init(\$conf);
44
45
45
46
subtest "Log4perl vanilla, 10000 errors", \&Log4perlVanilla;
46
subtest "Log4perl vanilla, 10000 errors", \&Log4perlVanilla;
47
sub Log4perlVanilla {
47
sub Log4perlVanilla {
Lines 100-125 sub _logErrors { Link Here
100
100
101
sub verifyThatLogWasWritten {
101
sub verifyThatLogWasWritten {
102
    #Verify that we actually wrote something and the Koha::Logger configurations work
102
    #Verify that we actually wrote something and the Koha::Logger configurations work
103
    open(my $FH, '<', '/tmp/log4perl_test.log') or die $!;
103
    ok(t::Koha::Logger::getFirstLogRow() =~ /The incredible burden of building good logging faculties/, "Log writing confirmed");
104
    my $firstRow = <$FH>;
104
    t::Koha::Logger::clearLog(); #Clean up the temp log file or it will grow big quickly
105
    close($FH);
105
}
106
    ok($firstRow =~ /The incredible burden of building good logging faculties/, "Log writing confirmed");
107
    #Clean up the temp log file or it will grow big quickly
108
    open($FH, '>', '/tmp/log4perl_test.log') or die $!;
109
    close($FH);
110
}
111
112
__DATA__
113
log4perl.logger.intranet = WARN, INTRANET
114
log4perl.appender.INTRANET=Log::Log4perl::Appender::File
115
log4perl.appender.INTRANET.filename=/tmp/log4perl_test.log
116
log4perl.appender.INTRANET.mode=append
117
log4perl.appender.INTRANET.layout=PatternLayout
118
log4perl.appender.INTRANET.layout.ConversionPattern=[%d] [%p] %m %l %n
119
120
log4perl.logger.opac = WARN, OPAC
121
log4perl.appender.OPAC=Log::Log4perl::Appender::File
122
log4perl.appender.OPAC.filename=/tmp/log4perl_test.log
123
log4perl.appender.OPAC.mode=append
124
log4perl.appender.OPAC.layout=PatternLayout
125
log4perl.appender.OPAC.layout.ConversionPattern=[%d] [%p] %m %l %n
126
- 

Return to bug 16304