Completed
Pull Request — master (#24551)
by Thomas
19:32 queued 48s
created

CommandLogger::debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
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 Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Console;
23
24
use OCP\ILogger;
25
use OCP\Util;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class CommandLogger implements ILogger {
29
30
	/** @var OutputInterface */
31
	private $output;
32
33
	/**
34
	 * CommandLogger constructor.
35
	 *
36
	 * @param OutputInterface $output
37
	 */
38
	public function __construct(OutputInterface $output) {
39
		$this->output = $output;
40
	}
41
42
	/**
43
	 * System is unusable.
44
	 *
45
	 * @param string $message
46
	 * @param array $context
47
	 * @return null
48
	 * @since 7.0.0
49
	 */
50
	public function emergency($message, array $context = []) {
51
		$this->log(Util::FATAL, $message, $context);
52
	}
53
54
	/**
55
	 * Action must be taken immediately.
56
	 *
57
	 * @param string $message
58
	 * @param array $context
59
	 * @return null
60
	 * @since 7.0.0
61
	 */
62
	public function alert($message, array $context = []) {
63
		$this->log(Util::ERROR, $message, $context);
64
	}
65
66
	/**
67
	 * Critical conditions.
68
	 *
69
	 * @param string $message
70
	 * @param array $context
71
	 * @return null
72
	 * @since 7.0.0
73
	 */
74
	public function critical($message, array $context = []) {
75
		$this->log(Util::ERROR, $message, $context);
76
	}
77
78
	/**
79
	 * Runtime errors that do not require immediate action but should typically
80
	 * be logged and monitored.
81
	 *
82
	 * @param string $message
83
	 * @param array $context
84
	 * @return null
85
	 * @since 7.0.0
86
	 */
87
	public function error($message, array $context = []) {
88
		$this->log(Util::ERROR, $message, $context);
89
	}
90
91
	/**
92
	 * Exceptional occurrences that are not errors.
93
	 *
94
	 * @param string $message
95
	 * @param array $context
96
	 * @return null
97
	 * @since 7.0.0
98
	 */
99
	public function warning($message, array $context = []) {
100
		$this->log(Util::WARN, $message, $context);
101
	}
102
103
	/**
104
	 * Normal but significant events.
105
	 *
106
	 * @param string $message
107
	 * @param array $context
108
	 * @return null
109
	 * @since 7.0.0
110
	 */
111
	public function notice($message, array $context = []) {
112
		$this->log(Util::INFO, $message, $context);
113
	}
114
115
	/**
116
	 * Interesting events.
117
	 *
118
	 * @param string $message
119
	 * @param array $context
120
	 * @return null
121
	 * @since 7.0.0
122
	 */
123
	public function info($message, array $context = []) {
124
		$this->log(Util::ERROR, $message, $context);
125
	}
126
127
	/**
128
	 * Detailed debug information.
129
	 *
130
	 * @param string $message
131
	 * @param array $context
132
	 * @return null
133
	 * @since 7.0.0
134
	 */
135
	public function debug($message, array $context = []) {
136
		$this->log(Util::DEBUG, $message, $context);
137
	}
138
139
	/**
140
	 * Logs with an arbitrary level.
141
	 *
142
	 * @param mixed $level
143
	 * @param string $message
144
	 * @param array $context
145
	 * @return mixed
146
	 * @since 7.0.0
147
	 */
148
	public function log($level, $message, array $context = []) {
149
		$minLevel = Util::INFO;
150
		$verbosity = $this->output->getVerbosity();
151
		if ($verbosity === OutputInterface::VERBOSITY_DEBUG) {
152
			$minLevel = Util::DEBUG;
153
		}
154
		if ($verbosity === OutputInterface::VERBOSITY_QUIET) {
155
			$minLevel = Util::ERROR;
156
		}
157
		if ($level < $minLevel) {
158
			return;
159
		}
160
161
		// interpolate $message as defined in PSR-3
162
		$replace = [];
163
		foreach ($context as $key => $val) {
164
			$replace['{' . $key . '}'] = $val;
165
		}
166
167
		// interpolate replacement values into the message and return
168
		$message = \strtr($message, $replace);
169
		$style = ($level > 2) ?'error' : 'info';
170
171
		$this->output->writeln("<$style>$message</$style>");
172
	}
173
174
	/**
175
	 * Logs an exception very detailed
176
	 * An additional message can we written to the log by adding it to the
177
	 * context.
178
	 *
179
	 * <code>
180
	 * $logger->logException($ex, [
181
	 *     'message' => 'Exception during cron job execution'
182
	 * ]);
183
	 * </code>
184
	 *
185
	 * @param \Exception | \Throwable $exception
186
	 * @param array $context
187
	 * @return void
188
	 * @since 8.2.0
189
	 */
190
	public function logException($exception, array $context = []) {
191
		$exception = [
192
			'Exception' => \get_class($exception),
193
			'Message' => $exception->getMessage(),
194
			'Code' => $exception->getCode(),
195
			'Trace' => $exception->getTraceAsString(),
196
			'File' => $exception->getFile(),
197
			'Line' => $exception->getLine(),
198
		];
199
		$exception['Trace'] = \preg_replace('!(login|checkPassword|updatePrivateKeyPassword)\(.*\)!', '$1(*** username and password replaced ***)', $exception['Trace']);
200
		$msg = isset($context['message']) ? $context['message'] : 'Exception';
201
		$msg .= ': ' . \json_encode($exception);
202
		$this->error($msg, $context);
203
	}
204
}
205