MediaWiki master
ConsoleLogger.php
Go to the documentation of this file.
1<?php
2
4
5use Psr\Log\AbstractLogger;
6use Psr\Log\LoggerInterface;
7use Psr\Log\LogLevel;
8use Wikimedia\Assert\Assert;
9
22class ConsoleLogger extends AbstractLogger {
23 private const LEVELS = [
24 LogLevel::DEBUG => 0,
25 LogLevel::INFO => 1,
26 LogLevel::NOTICE => 2,
27 LogLevel::WARNING => 3,
28 LogLevel::ERROR => 4,
29 LogLevel::CRITICAL => 5,
30 LogLevel::ALERT => 6,
31 LogLevel::EMERGENCY => 7,
32 ];
33
34 private string $channel;
35 private ?string $minLevel;
36 private ?LoggerInterface $forwardTo;
37
43 public function __construct(
44 string $channel,
45 ?string $minLevel = null,
46 ?LoggerInterface $forwardTo = null
47 ) {
48 Assert::parameter( $minLevel === null || isset( self::LEVELS[$minLevel] ), '$minLevel',
49 'must be a valid, lowercase PSR-3 log level' );
50
51 $this->channel = $channel;
52 $this->minLevel = $minLevel;
53 $this->forwardTo = $forwardTo;
54 }
55
59 public function log( $level, $message, array $context = [] ) {
60 if ( !$this->minLevel || self::LEVELS[$level] >= self::LEVELS[$this->minLevel] ) {
61 fwrite( STDERR, "[$level] " .
62 LegacyLogger::format( $this->channel, $message, $context ) );
63 }
64 if ( $this->forwardTo ) {
65 $this->forwardTo->log( $level, $message, $context );
66 }
67 }
68}
Write logs to command-line output (STDERR).
log( $level, $message, array $context=[])
__construct(string $channel, ?string $minLevel=null, ?LoggerInterface $forwardTo=null)
static format( $channel, $message, $context)
Format a message.