Lines 19-32
use Modern::Perl;
Link Here
|
19 |
use Test::More; |
19 |
use Test::More; |
20 |
|
20 |
|
21 |
use Log::Log4perl; |
21 |
use Log::Log4perl; |
|
|
22 |
use Scalar::Util qw(blessed); |
23 |
use Try::Tiny; |
24 |
|
22 |
use C4::Context; |
25 |
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) |
26 |
C4::Context->setCommandlineEnvironment(); #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 |
27 |
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 |
|
28 |
|
26 |
#Initialize the Log4perl to write to /tmp/log4perl_test.log so we can clean it later |
29 |
#Initialize the Log4perl to write to /tmp/log4perl_test.log so we can clean it later |
27 |
Log::Log4perl::init( t::Koha::Logger::getLog4perlConfig() ); |
30 |
Log::Log4perl::init( t::Koha::Logger::getLog4perlConfig() ); |
28 |
|
31 |
|
29 |
use Koha::Logger; |
32 |
require Koha::Logger; |
30 |
use t::Koha::Logger; |
33 |
use t::Koha::Logger; |
31 |
|
34 |
|
32 |
|
35 |
|
Lines 56-60
t::Koha::Logger::clearLog();
Link Here
|
56 |
#### END OF Test for Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set #### |
59 |
#### END OF Test for Bug 16304 - Koha::Logger, lazy load loggers so environment has time to get set #### |
57 |
######################################################################################################### |
60 |
######################################################################################################### |
58 |
|
61 |
|
|
|
62 |
############################################################################################### |
63 |
#### KD976 - Koha::Logger overload configuration for command line scripts verbosity levels #### |
64 |
subtest "Overload Logger configurations", \&overloadLoggerConfigurations; |
65 |
sub overloadLoggerConfigurations { |
66 |
my ($logger, $log, $LOGFILEOUT, $stdoutLogHandle, $stdoutLogPtr, @stdoutLog); |
67 |
$stdoutLogPtr = \$stdoutLogHandle; |
68 |
|
69 |
#Test increasing log verbosity |
70 |
($LOGFILEOUT, $stdoutLogPtr) = _reopenStdoutScalarHandle($LOGFILEOUT, $stdoutLogPtr); |
71 |
Koha::Logger->setConsoleVerbosity(1); |
72 |
$logger = Koha::Logger->get({category => "test-is-fun-1"}); |
73 |
_loggerBlarbAllLevels($logger); |
74 |
$log = t::Koha::Logger::slurpLog('wantArray'); |
75 |
@stdoutLog = split("\n", $stdoutLogHandle); |
76 |
ok($log->[0] =~ /info/, 'Increment by 1. file - info ok'); |
77 |
ok($stdoutLog[0] =~ /info/, 'Increment by 1. stdout - info ok'); |
78 |
ok($log->[1] =~ /warn/, 'Increment by 1. file - warn ok'); |
79 |
ok($stdoutLog[1] =~ /warn/, 'Increment by 1. stdout - warn ok'); |
80 |
ok($log->[2] =~ /error/, 'Increment by 1. file - error ok'); |
81 |
ok($stdoutLog[2] =~ /error/, 'Increment by 1. stdout - error ok'); |
82 |
ok($log->[3] =~ /fatal/, 'Increment by 1. file - fatal ok'); |
83 |
ok($stdoutLog[3] =~ /fatal/, 'Increment by 1. stdout - fatal ok'); |
84 |
t::Koha::Logger::clearLog(); |
85 |
|
86 |
#Test decreasing log verbosity |
87 |
($LOGFILEOUT, $stdoutLogPtr) = _reopenStdoutScalarHandle($LOGFILEOUT, $stdoutLogPtr); |
88 |
Koha::Logger->setConsoleVerbosity(-1); |
89 |
$logger = Koha::Logger->get({category => "test-is-fun-2"}); |
90 |
_loggerBlarbAllLevels($logger); |
91 |
$log = t::Koha::Logger::slurpLog('wantArray'); |
92 |
@stdoutLog = split("\n", $stdoutLogHandle); |
93 |
ok($log->[0] =~ /error/, 'Decrement by 1. file - error ok'); |
94 |
ok($stdoutLog[0] =~ /error/, 'Decrement by 1. stdout - error ok'); |
95 |
ok($log->[1] =~ /fatal/, 'Decrement by 1. file - fatal ok'); |
96 |
ok($stdoutLog[1] =~ /fatal/, 'Decrement by 1. stdout - fatal ok'); |
97 |
t::Koha::Logger::clearLog(); |
98 |
|
99 |
#Test increasing log verbosity multiple levels |
100 |
($LOGFILEOUT, $stdoutLogPtr) = _reopenStdoutScalarHandle($LOGFILEOUT, $stdoutLogPtr); |
101 |
Koha::Logger->setConsoleVerbosity(2); |
102 |
$logger = Koha::Logger->get({category => "test-is-fun-3"}); |
103 |
_loggerBlarbAllLevels($logger); |
104 |
$log = t::Koha::Logger::slurpLog('wantArray'); |
105 |
@stdoutLog = split("\n", $stdoutLogHandle); |
106 |
ok($log->[0] =~ /debug/, 'Increment by 1. file - debug ok'); |
107 |
ok($stdoutLog[0] =~ /debug/, 'Increment by 1. stdout - debug ok'); |
108 |
ok($log->[1] =~ /info/, 'Increment by 1. file - info ok'); |
109 |
ok($stdoutLog[1] =~ /info/, 'Increment by 1. stdout - info ok'); |
110 |
ok($log->[2] =~ /warn/, 'Increment by 1. file - warn ok'); |
111 |
ok($stdoutLog[2] =~ /warn/, 'Increment by 1. stdout - warn ok'); |
112 |
ok($log->[3] =~ /error/, 'Increment by 1. file - error ok'); |
113 |
ok($stdoutLog[3] =~ /error/, 'Increment by 1. stdout - error ok'); |
114 |
ok($log->[4] =~ /fatal/, 'Increment by 1. file - fatal ok'); |
115 |
ok($stdoutLog[4] =~ /fatal/, 'Increment by 1. stdout - fatal ok'); |
116 |
t::Koha::Logger::clearLog(); |
117 |
|
118 |
#Test decreasing log verbosity beyond FATAL, this results to no output |
119 |
($LOGFILEOUT, $stdoutLogPtr) = _reopenStdoutScalarHandle($LOGFILEOUT, $stdoutLogPtr); |
120 |
Koha::Logger->setConsoleVerbosity(-3); |
121 |
$logger = Koha::Logger->get({category => "test-is-fun-4"}); |
122 |
_loggerBlarbAllLevels($logger); |
123 |
$log = t::Koha::Logger::slurpLog('wantArray'); |
124 |
@stdoutLog = split("\n", $stdoutLogHandle); |
125 |
is(scalar(@$log), 0, 'Decrement overboard. no logging'); |
126 |
t::Koha::Logger::clearLog(); |
127 |
|
128 |
#Test static log level |
129 |
($LOGFILEOUT, $stdoutLogPtr) = _reopenStdoutScalarHandle($LOGFILEOUT, $stdoutLogPtr); |
130 |
Koha::Logger->setConsoleVerbosity('FATAL'); |
131 |
$logger = Koha::Logger->get({category => "test-is-fun-5"}); |
132 |
_loggerBlarbAllLevels($logger); |
133 |
$log = t::Koha::Logger::slurpLog('wantArray'); |
134 |
@stdoutLog = split("\n", $stdoutLogHandle); |
135 |
ok($log->[0] =~ /fatal/, 'Static log level. file - fatal ok'); |
136 |
ok($stdoutLog[0] =~ /fatal/, 'Static log level. stdout - fatal ok'); |
137 |
t::Koha::Logger::clearLog(); |
138 |
|
139 |
#Test bad log level, then continue using the default config unhindered |
140 |
($LOGFILEOUT, $stdoutLogPtr) = _reopenStdoutScalarHandle($LOGFILEOUT, $stdoutLogPtr); |
141 |
try { |
142 |
Koha::Logger->setConsoleVerbosity('WARNNNG'); |
143 |
die "We should have died"; |
144 |
} catch { |
145 |
ok($_ =~ /verbosity must be a positive or negative digit/, "Bad \$verbosiness, but got instructions on how to properly give \$verbosiness"); |
146 |
}; |
147 |
$logger = Koha::Logger->get({category => "test-is-fun-6"}); |
148 |
_loggerBlarbAllLevels($logger); |
149 |
$log = t::Koha::Logger::slurpLog('wantArray'); |
150 |
@stdoutLog = split("\n", $stdoutLogHandle); |
151 |
ok($log->[0] =~ /fatal/, 'Bad config, defaulting. file - fatal ok'); |
152 |
ok($stdoutLog[0] =~ /fatal/, 'Bad config, defaulting. stdout - fatal ok'); |
153 |
t::Koha::Logger::clearLog(); |
154 |
|
155 |
close($LOGFILEOUT); |
156 |
} |
157 |
|
158 |
#### END OF Test for KD976 - Koha::Logger overload configuration for command line scripts verbosity levels #### |
159 |
############################################################################################################### |
160 |
|
161 |
sub _loggerBlarbAllLevels { |
162 |
my ($logger) = @_; |
163 |
$logger->trace('trace'); |
164 |
$logger->debug('debug'); |
165 |
$logger->info('info'); |
166 |
$logger->warn('warn'); |
167 |
$logger->error('error'); |
168 |
$logger->fatal('fatal'); |
169 |
} |
170 |
sub _reopenStdoutScalarHandle { |
171 |
my ($LOGFILEOUT, $pointerToScalar) = @_; |
172 |
$$pointerToScalar = ''; |
173 |
close($LOGFILEOUT) if $LOGFILEOUT; |
174 |
open($LOGFILEOUT, '>', $pointerToScalar) or die $!; |
175 |
$LOGFILEOUT->autoflush ( 1 ); |
176 |
select $LOGFILEOUT; #Use this as the default print target, so Console appender is redirected to this logfile |
177 |
return ($LOGFILEOUT, $pointerToScalar); |
178 |
} |
59 |
|
179 |
|
60 |
done_testing(); |
180 |
done_testing(); |
61 |
- |
|
|