wiki.techinc.nl/includes/debug/logger/LogCapturingSpi.php
Timo Tijhof 9cb8bb611b debug: Improve docs, fix ingroup tags, clean up tests
* Remove redundant file-level description and ensure the class desc
  and ingroup tag are on the class block instead.
  Ref https://gerrit.wikimedia.org/r/q/owner:Krinkle+message:ingroup

* Widen `@covers` annotations in unit tests.
  Ref https://gerrit.wikimedia.org/r/q/owner:krinkle+is:merged+message:covers

* Create "Debug" documentation group, covering the debug/ directory.
  This will show up on doc.wikimedia.org under "Modules", where
  each class is listed, and the class page will also link back to
  the group as part of its breadcrumb navigation.

  Test with `php maintenance/mwdocgen.php --file docs/,includes/debug/`
  and then view /w/docs/html/

* Improve docs of various classes and explain relationships better.
  In particular, reformat to ensure each class has a oneline
  description that captures its essential function.

Change-Id: I5d1143a9244b7fd888e1dc31f0fd7965272aa900
2023-10-05 11:54:30 +00:00

105 lines
2.4 KiB
PHP

<?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 = [] ) {
$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;
}
}