Debian Codegraph @

tests/phpunit/mocks/TestLogger.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright (C) 2015 Wikimedia Foundation and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * A logger that may be configured to either buffer logs or to print them to
25 * the output where PHPUnit will complain about them.
26 *
27 * @since 1.27
28 */
29 class TestLogger extends \Psr\Log\AbstractLogger {
30 /** @var bool */
31 private $collect;
32 /** @var bool */
33 private $collectContext;
34 /** @var array */
35 private $buffer = [];
36 /** @var callable|null */
37 private $filter;
38
39 /**
40 * @param bool $collect Whether to collect logs. @see setCollect()
41 * @param callable|null $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()).
45 */
46 public function __construct( $collect = false, $filter = null, $collectContext = false ) {
47 $this->collect = $collect;
48 $this->collectContext = $collectContext;
49 $this->filter = $filter;
50 }
51
52 /**
53 * Set the "collect" flag
54 * @param bool $collect
55 * @return TestLogger $this
56 */
57 public function setCollect( $collect ) {
58 $this->collect = $collect;
59 return $this;
60 }
61
62 /**
63 * Set the collectContext flag
64 *
65 * @param bool $collectContext
66 * @since 1.29
67 * @return TestLogger $this
68 */
69 public function setCollectContext( $collectContext ) {
70 $this->collectContext = $collectContext;
71 return $this;
72 }
73
74 /**
75 * Return the collected logs
76 * @return array Array of [ string $level, string $message ], or
77 * [ string $level, string $message, array $context ] if $collectContext was true.
78 */
79 public function getBuffer() {
80 return $this->buffer;
81 }
82
83 /**
84 * Clear the collected log buffer
85 */
86 public function clearBuffer() {
87 $this->buffer = [];
88 }
89
90 public function log( $level, $message, array $context = [] ): void {
91 $message = trim( $message );
92
93 if ( $this->filter ) {
94 $message = ( $this->filter )( $message, $level, $context );
95 if ( $message === null ) {
96 return;
97 }
98 }
99
100 if ( $this->collect ) {
101 if ( $this->collectContext ) {
102 $this->buffer[] = [ $level, $message, $context ];
103 } else {
104 $this->buffer[] = [ $level, $message ];
105 }
106 } else {
107 echo "LOG[$level]: $message\n";
108 }
109 }
110 }