wiki.techinc.nl/includes/debug/logger/LogCapturingSpi.php
Amir Sarabadani f8efd21621 Fix lowercase in namespace of LogCapturingSpi.php
Basically turning Mediawiki to MediaWiki as all other classes exist
in that namespace

Bug: T217128
Change-Id: I95039a1a54c9900a2f2414b5a6ccce5fb6e5e612
2019-02-26 13:17:09 +01:00

83 lines
1.9 KiB
PHP

<?php
namespace MediaWiki\Logger;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
/**
* Wraps another spi to capture all logs generated. This can be
* used, for example, to collect all logs generated during a
* unit test and report them when the test fails.
*/
class LogCapturingSpi implements Spi {
/** @var LoggerInterface[] */
private $singletons;
/** @var Spi */
private $inner;
/** @var array */
private $logs = [];
public function __construct( Spi $inner ) {
$this->inner = $inner;
}
/**
* @return array
*/
public function getLogs() {
return $this->logs;
}
/**
* @param string $channel
* @return LoggerInterface
*/
public function getLogger( $channel ) {
if ( !isset( $this->singletons[$channel] ) ) {
$this->singletons[$channel] = $this->createLogger( $channel );
}
return $this->singletons[$channel];
}
/**
* @param array $log
*/
public function capture( $log ) {
$this->logs[] = $log;
}
/**
* @param string $channel
* @return LoggerInterface
*/
private function createLogger( $channel ) {
$inner = $this->inner->getLogger( $channel );
return new class( $channel, $inner, $this ) extends AbstractLogger {
/** @var string */
private $channel;
/** @var LoggerInterface */
private $logger;
/** @var LogCapturingSpi */
private $parent;
// phpcs:ignore MediaWiki.Usage.NestedFunctions.NestedFunction
public function __construct( $channel, LoggerInterface $logger, LogCapturingSpi $parent ) {
$this->channel = $channel;
$this->logger = $logger;
$this->parent = $parent;
}
// phpcs:ignore MediaWiki.Usage.NestedFunctions.NestedFunction
public function log( $level, $message, array $context = [] ) {
$this->parent->capture( [
'channel' => $this->channel,
'level' => $level,
'message' => $message,
'context' => $context
] );
$this->logger->log( $level, $message, $context );
}
};
}
}