MediaWiki  1.34.0
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  // phpcs:ignore MediaWiki.Usage.NestedFunctions.NestedFunction
65  public function __construct( $channel, LoggerInterface $logger, LogCapturingSpi $parent ) {
66  $this->channel = $channel;
67  $this->logger = $logger;
68  $this->parent = $parent;
69  }
70 
71  // phpcs:ignore MediaWiki.Usage.NestedFunctions.NestedFunction
72  public function log( $level, $message, array $context = [] ) {
73  $this->parent->capture( [
74  'channel' => $this->channel,
75  'level' => $level,
76  'message' => $message,
77  'context' => $context
78  ] );
79  $this->logger->log( $level, $message, $context );
80  }
81  };
82  }
83 }
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\__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\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:36
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:38
MediaWiki\Logger\LogCapturingSpi\createLogger
createLogger( $channel)
Definition: LogCapturingSpi.php:54