5 * Copyright (C) 2015 Wikimedia Foundation and contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
28 * A logger that may be configured to either buffer logs or to print them to
29 * the output where PHPUnit will complain about them.
33 class TestLogger
extends \Psr\Log\AbstractLogger
{
37 private $collectContext;
40 /** @var callable|null */
44 * @param bool $collect Whether to collect logs. @see setCollect()
45 * @param callable|null $filter Filter logs before collecting/printing. Signature is
46 * string|null function ( string $message, string $level, array $context );
47 * @param bool $collectContext Whether to keep the context passed to log
48 * (since 1.29, @see setCollectContext()).
50 public function __construct( $collect = false, $filter = null, $collectContext = false ) {
51 $this->collect
= $collect;
52 $this->collectContext
= $collectContext;
53 $this->filter
= $filter;
57 * Set the "collect" flag
58 * @param bool $collect
59 * @return TestLogger $this
61 public function setCollect( $collect ) {
62 $this->collect
= $collect;
67 * Set the collectContext flag
69 * @param bool $collectContext
71 * @return TestLogger $this
73 public function setCollectContext( $collectContext ) {
74 $this->collectContext
= $collectContext;
79 * Return the collected logs
80 * @return array Array of [ string $level, string $message ], or
81 * [ string $level, string $message, array $context ] if $collectContext was true.
83 public function getBuffer() {
88 * Clear the collected log buffer
90 public function clearBuffer() {
94 public function log( $level, $message, array $context = [] ) {
95 $message = trim( $message );
97 if ( $this->filter
) {
98 $message = ( $this->filter
)( $message, $level, $context );
99 if ( $message === null ) {
104 if ( $this->collect
) {
105 if ( $this->collectContext
) {
106 $this->buffer
[] = [ $level, $message, $context ];
108 $this->buffer
[] = [ $level, $message ];
112 case LogLevel
::DEBUG
:
114 case LogLevel
::NOTICE
:
115 trigger_error( "LOG[$level]: $message", E_USER_NOTICE
);
118 case LogLevel
::WARNING
:
119 trigger_error( "LOG[$level]: $message", E_USER_WARNING
);
122 case LogLevel
::ERROR
:
123 case LogLevel
::CRITICAL
:
124 case LogLevel
::ALERT
:
125 case LogLevel
::EMERGENCY
:
126 trigger_error( "LOG[$level]: $message", E_USER_ERROR
);