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 64 65 66 67 68
|
<?php
namespace MediaWiki\Logger;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Wikimedia\Assert\Assert;
/**
* Write logs to command-line output (STDERR).
*
* The output is supposed to be human-readable, and should be changed as necessary
* to better achieve that goal.
*
* This is developed for use in maintenance/eval.php.
*
* @internal For use in MediaWiki core only
* @since 1.30
* @ingroup Debug
*/
class ConsoleLogger extends AbstractLogger {
private const LEVELS = [
LogLevel::DEBUG => 0,
LogLevel::INFO => 1,
LogLevel::NOTICE => 2,
LogLevel::WARNING => 3,
LogLevel::ERROR => 4,
LogLevel::CRITICAL => 5,
LogLevel::ALERT => 6,
LogLevel::EMERGENCY => 7,
];
private string $channel;
private ?string $minLevel;
private ?LoggerInterface $forwardTo;
/**
* @param string $channel log channel name.
* @param string|null $minLevel Minimum PSR-3 level below which messages are ignored.
* @param LoggerInterface|null $forwardTo Other logger to forward to.
*/
public function __construct(
string $channel,
?string $minLevel = null,
?LoggerInterface $forwardTo = null
) {
Assert::parameter( $minLevel === null || isset( self::LEVELS[$minLevel] ), '$minLevel',
'must be a valid, lowercase PSR-3 log level' );
$this->channel = $channel;
$this->minLevel = $minLevel;
$this->forwardTo = $forwardTo;
}
/**
* @inheritDoc
*/
public function log( $level, $message, array $context = [] ): void {
if ( !$this->minLevel || self::LEVELS[$level] >= self::LEVELS[$this->minLevel] ) {
fwrite( STDERR, "[$level] " .
LegacyLogger::format( $this->channel, $message, $context ) );
}
if ( $this->forwardTo ) {
$this->forwardTo->log( $level, $message, $context );
}
}
}
|