Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / mocks / TestLogger.php
blob3925048957eb1b86977cad596add2ac150727402
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 /** @var bool */
35 private $collect;
36 /** @var bool */
37 private $collectContext;
38 /** @var array */
39 private $buffer = [];
40 /** @var callable|null */
41 private $filter;
43 /**
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;
56 /**
57 * Set the "collect" flag
58 * @param bool $collect
59 * @return TestLogger $this
61 public function setCollect( $collect ) {
62 $this->collect = $collect;
63 return $this;
66 /**
67 * Set the collectContext flag
69 * @param bool $collectContext
70 * @since 1.29
71 * @return TestLogger $this
73 public function setCollectContext( $collectContext ) {
74 $this->collectContext = $collectContext;
75 return $this;
78 /**
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() {
84 return $this->buffer;
87 /**
88 * Clear the collected log buffer
90 public function clearBuffer() {
91 $this->buffer = [];
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 ) {
100 return;
104 if ( $this->collect ) {
105 if ( $this->collectContext ) {
106 $this->buffer[] = [ $level, $message, $context ];
107 } else {
108 $this->buffer[] = [ $level, $message ];
110 } else {
111 switch ( $level ) {
112 case LogLevel::DEBUG:
113 case LogLevel::INFO:
114 case LogLevel::NOTICE:
115 trigger_error( "LOG[$level]: $message", E_USER_NOTICE );
116 break;
118 case LogLevel::WARNING:
119 trigger_error( "LOG[$level]: $message", E_USER_WARNING );
120 break;
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 );
127 break;