MediaWiki  master
LogCapturingSpi.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Logger;
4 
5 use Psr\Log\AbstractLogger;
6 use Psr\Log\LoggerInterface;
7 
13 class LogCapturingSpi implements Spi {
15  private $singletons;
17  private $inner;
19  private $logs = [];
20 
21  public function __construct( Spi $inner ) {
22  $this->inner = $inner;
23  }
24 
28  public function getLogs() {
29  return $this->logs;
30  }
31 
36  public function getLogger( $channel ) {
37  if ( !isset( $this->singletons[$channel] ) ) {
38  $this->singletons[$channel] = $this->createLogger( $channel );
39  }
40  return $this->singletons[$channel];
41  }
42 
46  public function capture( $log ) {
47  $this->logs[] = $log;
48  }
49 
54  private function createLogger( $channel ) {
55  $inner = $this->inner->getLogger( $channel );
56  return new class( $channel, $inner, $this ) extends AbstractLogger {
58  private $channel;
60  private $logger;
62  private $parent;
63 
64  public function __construct( $channel, LoggerInterface $logger, LogCapturingSpi $parent ) {
65  $this->channel = $channel;
66  $this->logger = $logger;
67  $this->parent = $parent;
68  }
69 
70  public function log( $level, $message, array $context = [] ) {
71  $this->parent->capture( [
72  'channel' => $this->channel,
73  'level' => $level,
74  'message' => $message,
75  'context' => $context
76  ] );
77  $this->logger->log( $level, $message, $context );
78  }
79  };
80  }
81 
86  public function getInnerSpi(): Spi {
87  return $this->inner;
88  }
89 
96  public function setLoggerForTest( $channel, LoggerInterface $logger = null ) {
97  $ret = $this->singletons[$channel] ?? null;
98  $this->singletons[$channel] = $logger;
99  return $ret;
100  }
101 }
MediaWiki\Logger\LogCapturingSpi
Wraps another spi to capture all logs generated.
Definition: LogCapturingSpi.php:13
MediaWiki\Logger\LogCapturingSpi\$logs
array $logs
Definition: LogCapturingSpi.php:19
MediaWiki\Logger\LogCapturingSpi\capture
capture( $log)
Definition: LogCapturingSpi.php:46
MediaWiki\Logger
Definition: ConsoleLogger.php:3
MediaWiki\Logger\LogCapturingSpi\setLoggerForTest
setLoggerForTest( $channel, LoggerInterface $logger=null)
Definition: LogCapturingSpi.php:96
MediaWiki\Logger\LogCapturingSpi\__construct
__construct(Spi $inner)
Definition: LogCapturingSpi.php:21
MediaWiki\Logger\LogCapturingSpi\getLogs
getLogs()
Definition: LogCapturingSpi.php:28
MediaWiki\Logger\Spi\getLogger
getLogger( $channel)
Get a logger instance.
MediaWiki\Logger\LogCapturingSpi\$inner
Spi $inner
Definition: LogCapturingSpi.php:17
MediaWiki\Logger\LogCapturingSpi\getInnerSpi
getInnerSpi()
Definition: LogCapturingSpi.php:86
MediaWiki\Logger\LogCapturingSpi\getLogger
getLogger( $channel)
Definition: LogCapturingSpi.php:36
MediaWiki\Logger\LogCapturingSpi\$singletons
LoggerInterface[] $singletons
Definition: LogCapturingSpi.php:15
MediaWiki\Logger\Spi
Service provider interface for \Psr\Log\LoggerInterface implementation libraries.
Definition: Spi.php:38
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:40
MediaWiki\Logger\LogCapturingSpi\createLogger
createLogger( $channel)
Definition: LogCapturingSpi.php:54