It was added in 2016 as part of SessionManager (Ic1ffea74f3, later re-attributed to Ibb3e023e4e) by Anomie. It wasn't used at the time. Basically, it is used for cases where we expect no logs, and the presence of logs will then produce a failing test. The point is to do something that PHPUnit will "complain" about by showing it. Emitting a notice or warning is one way, another is to simply echo it, since output is also unexpected and shown. The somewhat arbitrary mapping from log levels to PHP error codes isn't used. Later in 2017, he introduced the same class in php-session-serializer (I2ebf59414ef9a9) where this part was already removed and replaced by "echo". The version in core still used trigger_error. == Why now == Triggering E_USER_ERROR is deprecated in PHP 8.4+. Bug: T379445 Change-Id: Id1e1db80bc8fea39cd192716597e5e4a6f4966b0 (cherry picked from commit 5a1a8c14bd418c846498cd4ed99b77e45a0fcca1)
110 lines
3 KiB
PHP
110 lines
3 KiB
PHP
<?php
|
|
/**
|
|
* Copyright (C) 2015 Wikimedia Foundation and contributors
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
*
|
|
* @file
|
|
*/
|
|
|
|
/**
|
|
* A logger that may be configured to either buffer logs or to print them to
|
|
* the output where PHPUnit will complain about them.
|
|
*
|
|
* @since 1.27
|
|
*/
|
|
class TestLogger extends \Psr\Log\AbstractLogger {
|
|
/** @var bool */
|
|
private $collect;
|
|
/** @var bool */
|
|
private $collectContext;
|
|
/** @var array */
|
|
private $buffer = [];
|
|
/** @var callable|null */
|
|
private $filter;
|
|
|
|
/**
|
|
* @param bool $collect Whether to collect logs. @see setCollect()
|
|
* @param callable|null $filter Filter logs before collecting/printing. Signature is
|
|
* string|null function ( string $message, string $level, array $context );
|
|
* @param bool $collectContext Whether to keep the context passed to log
|
|
* (since 1.29, @see setCollectContext()).
|
|
*/
|
|
public function __construct( $collect = false, $filter = null, $collectContext = false ) {
|
|
$this->collect = $collect;
|
|
$this->collectContext = $collectContext;
|
|
$this->filter = $filter;
|
|
}
|
|
|
|
/**
|
|
* Set the "collect" flag
|
|
* @param bool $collect
|
|
* @return TestLogger $this
|
|
*/
|
|
public function setCollect( $collect ) {
|
|
$this->collect = $collect;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Set the collectContext flag
|
|
*
|
|
* @param bool $collectContext
|
|
* @since 1.29
|
|
* @return TestLogger $this
|
|
*/
|
|
public function setCollectContext( $collectContext ) {
|
|
$this->collectContext = $collectContext;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Return the collected logs
|
|
* @return array Array of [ string $level, string $message ], or
|
|
* [ string $level, string $message, array $context ] if $collectContext was true.
|
|
*/
|
|
public function getBuffer() {
|
|
return $this->buffer;
|
|
}
|
|
|
|
/**
|
|
* Clear the collected log buffer
|
|
*/
|
|
public function clearBuffer() {
|
|
$this->buffer = [];
|
|
}
|
|
|
|
public function log( $level, $message, array $context = [] ) {
|
|
$message = trim( $message );
|
|
|
|
if ( $this->filter ) {
|
|
$message = ( $this->filter )( $message, $level, $context );
|
|
if ( $message === null ) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
if ( $this->collect ) {
|
|
if ( $this->collectContext ) {
|
|
$this->buffer[] = [ $level, $message, $context ];
|
|
} else {
|
|
$this->buffer[] = [ $level, $message ];
|
|
}
|
|
} else {
|
|
echo "LOG[$level]: $message\n";
|
|
}
|
|
}
|
|
}
|