wiki.techinc.nl/includes/debug/logger/LogCapturingSpi.php
libraryupgrader 5357695270 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0
  The following sniffs now pass and were enabled:
  * Generic.ControlStructures.InlineControlStructure
  * MediaWiki.PHPUnit.AssertCount.NotUsed

npm:
* svgo: 2.3.0 → 2.3.1
  * https://npmjs.com/advisories/1754 (CVE-2021-33587)

Change-Id: I2a9bbee2fecbf7259876d335f565ece4b3622426
2021-07-22 03:36:05 +00:00

101 lines
2.3 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;
public function __construct( $channel, LoggerInterface $logger, LogCapturingSpi $parent ) {
$this->channel = $channel;
$this->logger = $logger;
$this->parent = $parent;
}
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 );
}
};
}
/**
* @internal For use by MediaWikiIntegrationTestCase
* @return Spi
*/
public function getInnerSpi(): Spi {
return $this->inner;
}
/**
* @internal For use by MediaWikiIntegrationTestCase
* @param string $channel
* @param LoggerInterface|null $logger
* @return LoggerInterface|null
*/
public function setLoggerForTest( $channel, LoggerInterface $logger = null ) {
$ret = $this->singletons[$channel] ?? null;
$this->singletons[$channel] = $logger;
return $ret;
}
}