Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
MaintenanceDebugLogger | |
0.00% |
0 / 20 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
setMaximumLevel | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
log | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Flow\Maintenance; |
4 | |
5 | use Flow\Exception\FlowException; |
6 | use MediaWiki\Maintenance\Maintenance; |
7 | use Psr\Log\AbstractLogger; |
8 | use Psr\Log\LogLevel; |
9 | |
10 | class MaintenanceDebugLogger extends AbstractLogger { |
11 | /** |
12 | * @var Maintenance The maintenance script to perform output through |
13 | */ |
14 | protected $maintenance; |
15 | |
16 | /** |
17 | * @var int The maximum logLevelPosition to output to the |
18 | * maintenance object. Defaults to LogLevel::INFO |
19 | */ |
20 | protected $maxLevel = 7; |
21 | |
22 | /** |
23 | * @var int[] Map from LogLevel constant to its position relative |
24 | * to other constants. |
25 | */ |
26 | protected $logLevelPosition; |
27 | |
28 | public function __construct( Maintenance $maintenance ) { |
29 | $this->maintenance = $maintenance; |
30 | $this->logLevelPosition = [ |
31 | LogLevel::EMERGENCY => 1, |
32 | LogLevel::ALERT => 2, |
33 | LogLevel::CRITICAL => 3, |
34 | LogLevel::ERROR => 4, |
35 | LogLevel::WARNING => 5, |
36 | LogLevel::NOTICE => 6, |
37 | LogLevel::INFO => 7, |
38 | LogLevel::DEBUG => 8 |
39 | ]; |
40 | } |
41 | |
42 | /** |
43 | * @param string $level A LogLevel constant. Logged messages less |
44 | * severe than this level will not be output. |
45 | */ |
46 | public function setMaximumLevel( $level ) { |
47 | if ( !isset( $this->logLevelPosition[$level] ) ) { |
48 | throw new FlowException( "Invalid LogLevel: $level" ); |
49 | } |
50 | $this->maxLevel = $this->logLevelPosition[$level]; |
51 | } |
52 | |
53 | /** |
54 | * @inheritDoc |
55 | */ |
56 | public function log( $level, $message, array $context = [] ) { |
57 | $position = $this->logLevelPosition[$level]; |
58 | if ( $position > $this->maxLevel ) { |
59 | return; |
60 | } |
61 | |
62 | // TS_DB is used as it is a consistent length every time |
63 | $ts = '[' . wfTimestamp( TS_DB ) . ']'; |
64 | $this->maintenance->outputChanneled( "$ts $message" ); |
65 | } |
66 | } |