rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / TestLogger.php
blob21d1bf2504e80875024547f20b1222af661fe0b7
1 <?php
2 /**
3 * Testing logger
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
22 * @file
25 use Psr\Log\LogLevel;
27 /**
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.
31 * @since 1.27
33 class TestLogger extends \Psr\Log\AbstractLogger {
34 private $collect = false;
35 private $collectContext = false;
36 private $buffer = [];
37 private $filter = null;
39 /**
40 * @param bool $collect Whether to collect logs. @see setCollect()
41 * @param callable $filter Filter logs before collecting/printing. Signature is
42 * string|null function ( string $message, string $level, array $context );
43 * @param bool $collectContext Whether to keep the context passed to log.
44 * @since 1.29 @see setCollectContext()
46 public function __construct( $collect = false, $filter = null, $collectContext = false ) {
47 $this->collect = $collect;
48 $this->collectContext = $collectContext;
49 $this->filter = $filter;
52 /**
53 * Set the "collect" flag
54 * @param bool $collect
55 * @return TestLogger $this
57 public function setCollect( $collect ) {
58 $this->collect = $collect;
59 return $this;
62 /**
63 * Set the collectContext flag
65 * @param bool $collectContext
66 * @since 1.29
67 * @return TestLogger $this
69 public function setCollectContext( $collectContext ) {
70 $this->collectContext = $collectContext;
71 return $this;
74 /**
75 * Return the collected logs
76 * @return array Array of array( string $level, string $message ), or
77 * array( string $level, string $message, array $context ) if $collectContext was true.
79 public function getBuffer() {
80 return $this->buffer;
83 /**
84 * Clear the collected log buffer
86 public function clearBuffer() {
87 $this->buffer = [];
90 public function log( $level, $message, array $context = [] ) {
91 $message = trim( $message );
93 if ( $this->filter ) {
94 $message = call_user_func( $this->filter, $message, $level, $context );
95 if ( $message === null ) {
96 return;
100 if ( $this->collect ) {
101 if ( $this->collectContext ) {
102 $this->buffer[] = [ $level, $message, $context ];
103 } else {
104 $this->buffer[] = [ $level, $message ];
106 } else {
107 switch ( $level ) {
108 case LogLevel::DEBUG:
109 case LogLevel::INFO:
110 case LogLevel::NOTICE:
111 trigger_error( "LOG[$level]: $message", E_USER_NOTICE );
112 break;
114 case LogLevel::WARNING:
115 trigger_error( "LOG[$level]: $message", E_USER_WARNING );
116 break;
118 case LogLevel::ERROR:
119 case LogLevel::CRITICAL:
120 case LogLevel::ALERT:
121 case LogLevel::EMERGENCY:
122 trigger_error( "LOG[$level]: $message", E_USER_ERROR );
123 break;