Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / TestLogger.php
blob7086a926be4b481c3b220af7a4ebd26b72fb23a8
1 <?php
2 /**
3 * Testing logger
5 * Copyright (C) 2015 Brad Jorsch <bjorsch@wikimedia.org>
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
22 * @file
23 * @author Brad Jorsch <bjorsch@wikimedia.org>
26 use Psr\Log\LogLevel;
28 /**
29 * A logger that may be configured to either buffer logs or to print them to
30 * the output where PHPUnit will complain about them.
32 * @since 1.27
34 class TestLogger extends \Psr\Log\AbstractLogger {
35 private $collect = false;
36 private $collectContext = false;
37 private $buffer = [];
38 private $filter = null;
40 /**
41 * @param bool $collect Whether to collect logs
42 * @param callable $filter Filter logs before collecting/printing. Signature is
43 * string|null function ( string $message, string $level, array $context );
45 public function __construct( $collect = false, $filter = null, $collectContext = false ) {
46 $this->collect = $collect;
47 $this->collectContext = $collectContext;
48 $this->filter = $filter;
51 /**
52 * Set the "collect" flag
53 * @param bool $collect
55 public function setCollect( $collect ) {
56 $this->collect = $collect;
59 /**
60 * Return the collected logs
61 * @return array Array of array( string $level, string $message ), or
62 * array( string $level, string $message, array $context ) if $collectContext was true.
64 public function getBuffer() {
65 return $this->buffer;
68 /**
69 * Clear the collected log buffer
71 public function clearBuffer() {
72 $this->buffer = [];
75 public function log( $level, $message, array $context = [] ) {
76 $message = trim( $message );
78 if ( $this->filter ) {
79 $message = call_user_func( $this->filter, $message, $level, $context );
80 if ( $message === null ) {
81 return;
85 if ( $this->collect ) {
86 if ( $this->collectContext ) {
87 $this->buffer[] = [ $level, $message, $context ];
88 } else {
89 $this->buffer[] = [ $level, $message ];
91 } else {
92 switch ( $level ) {
93 case LogLevel::DEBUG:
94 case LogLevel::INFO:
95 case LogLevel::NOTICE:
96 trigger_error( "LOG[$level]: $message", E_USER_NOTICE );
97 break;
99 case LogLevel::WARNING:
100 trigger_error( "LOG[$level]: $message", E_USER_WARNING );
101 break;
103 case LogLevel::ERROR:
104 case LogLevel::CRITICAL:
105 case LogLevel::ALERT:
106 case LogLevel::EMERGENCY:
107 trigger_error( "LOG[$level]: $message", E_USER_ERROR );
108 break;