MediaWiki REL1_40
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
15class ConsoleLogger extends AbstractLogger {
16 private const LEVELS = [
17 LogLevel::DEBUG => 0,
18 LogLevel::INFO => 1,
19 LogLevel::NOTICE => 2,
20 LogLevel::WARNING => 3,
21 LogLevel::ERROR => 4,
22 LogLevel::CRITICAL => 5,
23 LogLevel::ALERT => 6,
24 LogLevel::EMERGENCY => 7,
25 ];
26
27 private string $channel;
28 private ?string $minLevel;
29 private ?LoggerInterface $forwardTo;
30
36 public function __construct(
37 string $channel,
38 string $minLevel = null,
39 LoggerInterface $forwardTo = null
40 ) {
41 Assert::parameter( $minLevel === null || isset( self::LEVELS[$minLevel] ), '$minLevel',
42 'must be a valid, lowercase PSR-3 log level' );
43
44 $this->channel = $channel;
45 $this->minLevel = $minLevel;
46 $this->forwardTo = $forwardTo;
47 }
48
52 public function log( $level, $message, array $context = [] ) {
53 if ( !$this->minLevel || self::LEVELS[$level] >= self::LEVELS[$this->minLevel] ) {
54 fwrite( STDERR, "[$level] " .
55 LegacyLogger::format( $this->channel, $message, $context ) );
56 }
57 if ( $this->forwardTo ) {
58 $this->forwardTo->log( $level, $message, $context );
59 }
60 }
61}
A logger which writes to the terminal.
log( $level, $message, array $context=[])
__construct(string $channel, string $minLevel=null, LoggerInterface $forwardTo=null)
static format( $channel, $message, $context)
Format a message.