MediaWiki  master
LogCapturingSpi.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Logger;
4 
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 }
IContextSource $context
Definition: MediaWiki.php:37
Service provider interface for \Psr\Log\LoggerInterface implementation libraries. ...
Definition: Spi.php:36
getLogger( $channel)
Get a logger instance.
Wraps another spi to capture all logs generated.