1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
<?php
namespace MediaWiki\Logger;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
/**
* Wrap another Spi and keep a copy of all log messages.
*
* This is developed for use by PHPUnit bootstrapping, to collect logs
* generated during a given unit test, and print them after a failing test.
*
* @internal For use in MediaWiki core only
* @ingroup Debug
*/
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 = [] ): void {
$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;
}
}
|