summaryrefslogtreecommitdiff
blob: 375f537a59a314b80bcc6ecc13bc27badbcbf0ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

use Flow\Exception\FlowException;
use Psr\Log\LogLevel;

class MaintenanceDebugLogger extends Psr\Log\AbstractLogger {
	/**
	 * @var Maintenance The maintenance script to perform output through
	 */
	protected $maintenance;

	/**
	 * @var int The maximum logLevelPosition to output to the
	 *  maintenance object. Defaults to LogLevel::INFO
	 */
	protected $maxLevel = 7;

	/**
	 * @var int[string] Map from LogLevel constant to its position relative
	 *  to other constants.
	 */
	protected $logLevelPosition;

	public function __construct( Maintenance $maintenance ) {
		$this->maintenance = $maintenance;
		$this->logLevelPosition = array(
			LogLevel::EMERGENCY => 1,
			LogLevel::ALERT => 2,
			LogLevel::CRITICAL => 3,
			LogLevel::ERROR => 4,
			LogLevel::WARNING => 5,
			LogLevel::NOTICE => 6,
			LogLevel::INFO => 7,
			LogLevel::DEBUG => 8
		);
	}


	/**
	 * @param string $level A LogLevel constant. Logged messages less
	 *  severe than this level will not be output.
	 */
	public function setMaximumLevel( $level ) {
		if ( !isset( $this->logLevelPosition[$level] ) ) {
			throw new FlowException( "Invalid LogLevel: $level" );
		}
		$this->maxLevel = $this->logLevelPosition[$level];
	}

	/**
	 * {@inheritDoc}
	 */
	public function log( $level, $message, array $context = array() ) {
		$position = $this->logLevelPosition[$level];
		if ( $position > $this->maxLevel ) {
			return;
		}

		// TS_DB is used as it is a consistent length every time
		$ts = '[' . wfTimestamp( TS_DB ) . ']';
		$this->maintenance->outputChanneled( "$ts $message" );
	}
}