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 |
Koha::Logger->setConsoleVerbosity(); #Clear overrides with empty params |
141 |
($LOGFILEOUT, $stdoutLogPtr) = _reopenStdoutScalarHandle($LOGFILEOUT, $stdoutLogPtr); |
142 |
try { |
143 |
Koha::Logger->setConsoleVerbosity('WARNNNG'); |
144 |
die "We should have died"; |
145 |
} catch { |
146 |
ok($_ =~ /verbosity must be a positive or negative digit/, "Bad \$verbosiness, but got instructions on how to properly give \$verbosiness"); |
147 |
}; |
148 |
$logger = Koha::Logger->get({category => "test-is-fun-6"}); |
149 |
_loggerBlarbAllLevels($logger); |
150 |
$log = t::Koha::Logger::slurpLog('wantArray'); |
151 |
@stdoutLog = split("\n", $stdoutLogHandle); |
152 |
is(scalar(@stdoutLog), 0, 'Bad config, defaulting. Stdout not printed to in default mode.'); |
153 |
ok($log->[0] =~ /warn/, 'Bad config, defaulting. file - warn ok'); |
154 |
ok($log->[1] =~ /error/, 'Bad config, defaulting. file - error ok'); |
155 |
ok($log->[2] =~ /fatal/, 'Bad config, defaulting. file - fatal ok'); |
156 |
t::Koha::Logger::clearLog(); |
157 |
|
158 |
close($LOGFILEOUT); |
159 |
} |
160 |
|
161 |
#### END OF Test for KD976 - Koha::Logger overload configuration for command line scripts verbosity levels #### |
162 |
############################################################################################################### |
163 |
|
164 |
sub _loggerBlarbAllLevels { |
165 |
my ($logger) = @_; |
166 |
$logger->trace('trace'); |
167 |
$logger->debug('debug'); |
168 |
$logger->info('info'); |
169 |
$logger->warn('warn'); |
170 |
$logger->error('error'); |
171 |
$logger->fatal('fatal'); |
172 |
} |
173 |
sub _reopenStdoutScalarHandle { |
174 |
my ($LOGFILEOUT, $pointerToScalar) = @_; |
175 |
$$pointerToScalar = ''; |
176 |
close($LOGFILEOUT) if $LOGFILEOUT; |
177 |
open($LOGFILEOUT, '>', $pointerToScalar) or die $!; |
178 |
$LOGFILEOUT->autoflush ( 1 ); |
179 |
select $LOGFILEOUT; #Use this as the default print target, so Console appender is redirected to this logfile |
180 |
return ($LOGFILEOUT, $pointerToScalar); |
181 |
} |
59 |
|
182 |
|
60 |
done_testing(); |
183 |
done_testing(); |
61 |
- |
|
|