Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / TestLogger.php
blob84a6adf2acadaedb73e4001febb5a57c621aedaf
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. @see setCollect()
42 * @param callable $filter Filter logs before collecting/printing. Signature is
43 * string|null function ( string $message, string $level, array $context );
44 * @param bool $collectContext Whether to keep the context passed to log.
45 * @since 1.29 @see setCollectContext()
47 public function __construct( $collect = false, $filter = null, $collectContext = false ) {
48 $this->collect = $collect;
49 $this->collectContext = $collectContext;
50 $this->filter = $filter;
53 /**
54 * Set the "collect" flag
55 * @param bool $collect
56 * @return TestLogger $this
58 public function setCollect( $collect ) {
59 $this->collect = $collect;
60 return $this;
63 /**
64 * Set the collectContext flag
66 * @param bool $collectContext
67 * @since 1.29
68 * @return TestLogger $this
70 public function setCollectContext( $collectContext ) {
71 $this->collectContext = $collectContext;
72 return $this;
75 /**
76 * Return the collected logs
77 * @return array Array of array( string $level, string $message ), or
78 * array( string $level, string $message, array $context ) if $collectContext was true.
80 public function getBuffer() {
81 return $this->buffer;
84 /**
85 * Clear the collected log buffer
87 public function clearBuffer() {
88 $this->buffer = [];
91 public function log( $level, $message, array $context = [] ) {
92 $message = trim( $message );
94 if ( $this->filter ) {
95 $message = call_user_func( $this->filter, $message, $level, $context );
96 if ( $message === null ) {
97 return;
101 if ( $this->collect ) {
102 if ( $this->collectContext ) {
103 $this->buffer[] = [ $level, $message, $context ];
104 } else {
105 $this->buffer[] = [ $level, $message ];
107 } else {
108 switch ( $level ) {
109 case LogLevel::DEBUG:
110 case LogLevel::INFO:
111 case LogLevel::NOTICE:
112 trigger_error( "LOG[$level]: $message", E_USER_NOTICE );
113 break;
115 case LogLevel::WARNING:
116 trigger_error( "LOG[$level]: $message", E_USER_WARNING );
117 break;
119 case LogLevel::ERROR:
120 case LogLevel::CRITICAL:
121 case LogLevel::ALERT:
122 case LogLevel::EMERGENCY:
123 trigger_error( "LOG[$level]: $message", E_USER_ERROR );
124 break;