Passed
Push — master ( b1ca74...11e228 )
by Christoph
60:05 queued 47:35
created

PsrLoggerAdapter::error()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OC\Log;
28
29
use OC\Log;
30
use OCP\ILogger;
31
use OCP\Log\IDataLogger;
32
use Psr\Log\InvalidArgumentException;
33
use Psr\Log\LoggerInterface;
34
use Throwable;
35
use function array_key_exists;
36
use function array_merge;
37
38
final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
39
40
	/** @var Log */
41
	private $logger;
42
43
	public function __construct(Log $logger) {
44
		$this->logger = $logger;
45
	}
46
47
	private function containsThrowable(array $context): bool {
48
		return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
49
	}
50
51
	/**
52
	 * System is unusable.
53
	 *
54
	 * @param string $message
55
	 * @param array $context
56
	 *
57
	 * @return void
58
	 */
59
	public function emergency($message, array $context = []): void {
60
		if ($this->containsThrowable($context)) {
61
			$this->logger->logException($context['exception'], array_merge(
62
				[
63
					'message' => $message,
64
					'level' => ILogger::FATAL,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::FATAL has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::FATAL,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
65
				],
66
				$context
67
			));
68
		} else {
69
			$this->logger->emergency($message, $context);
70
		}
71
	}
72
73
	/**
74
	 * Action must be taken immediately.
75
	 *
76
	 * Example: Entire website down, database unavailable, etc. This should
77
	 * trigger the SMS alerts and wake you up.
78
	 *
79
	 * @param string $message
80
	 * @param array $context
81
	 *
82
	 * @return void
83
	 */
84
	public function alert($message, array $context = []) {
85
		if ($this->containsThrowable($context)) {
86
			$this->logger->logException($context['exception'], array_merge(
87
				[
88
					'message' => $message,
89
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

89
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
90
				],
91
				$context
92
			));
93
		} else {
94
			$this->logger->alert($message, $context);
95
		}
96
	}
97
98
	/**
99
	 * Critical conditions.
100
	 *
101
	 * Example: Application component unavailable, unexpected exception.
102
	 *
103
	 * @param string $message
104
	 * @param array $context
105
	 *
106
	 * @return void
107
	 */
108
	public function critical($message, array $context = []) {
109
		if ($this->containsThrowable($context)) {
110
			$this->logger->logException($context['exception'], array_merge(
111
				[
112
					'message' => $message,
113
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

113
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
114
				],
115
				$context
116
			));
117
		} else {
118
			$this->logger->critical($message, $context);
119
		}
120
	}
121
122
	/**
123
	 * Runtime errors that do not require immediate action but should typically
124
	 * be logged and monitored.
125
	 *
126
	 * @param string $message
127
	 * @param array $context
128
	 *
129
	 * @return void
130
	 */
131
	public function error($message, array $context = []) {
132
		if ($this->containsThrowable($context)) {
133
			$this->logger->logException($context['exception'], array_merge(
134
				[
135
					'message' => $message,
136
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

136
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
137
				],
138
				$context
139
			));
140
		} else {
141
			$this->logger->error($message, $context);
142
		}
143
	}
144
145
	/**
146
	 * Exceptional occurrences that are not errors.
147
	 *
148
	 * Example: Use of deprecated APIs, poor use of an API, undesirable things
149
	 * that are not necessarily wrong.
150
	 *
151
	 * @param string $message
152
	 * @param array $context
153
	 *
154
	 * @return void
155
	 */
156
	public function warning($message, array $context = []) {
157
		if ($this->containsThrowable($context)) {
158
			$this->logger->logException($context['exception'], array_merge(
159
				[
160
					'message' => $message,
161
					'level' => ILogger::WARN,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::WARN has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

161
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::WARN,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
162
				],
163
				$context
164
			));
165
		} else {
166
			$this->logger->warning($message, $context);
167
		}
168
	}
169
170
	/**
171
	 * Normal but significant events.
172
	 *
173
	 * @param string $message
174
	 * @param array $context
175
	 *
176
	 * @return void
177
	 */
178
	public function notice($message, array $context = []) {
179
		if ($this->containsThrowable($context)) {
180
			$this->logger->logException($context['exception'], array_merge(
181
				[
182
					'message' => $message,
183
					'level' => ILogger::INFO,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

183
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::INFO,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
184
				],
185
				$context
186
			));
187
		} else {
188
			$this->logger->notice($message, $context);
189
		}
190
	}
191
192
	/**
193
	 * Interesting events.
194
	 *
195
	 * Example: User logs in, SQL logs.
196
	 *
197
	 * @param string $message
198
	 * @param array $context
199
	 *
200
	 * @return void
201
	 */
202
	public function info($message, array $context = []) {
203
		if ($this->containsThrowable($context)) {
204
			$this->logger->logException($context['exception'], array_merge(
205
				[
206
					'message' => $message,
207
					'level' => ILogger::INFO,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

207
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::INFO,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
208
				],
209
				$context
210
			));
211
		} else {
212
			$this->logger->info($message, $context);
213
		}
214
	}
215
216
	/**
217
	 * Detailed debug information.
218
	 *
219
	 * @param string $message
220
	 * @param array $context
221
	 *
222
	 * @return void
223
	 */
224
	public function debug($message, array $context = []) {
225
		if ($this->containsThrowable($context)) {
226
			$this->logger->logException($context['exception'], array_merge(
227
				[
228
					'message' => $message,
229
					'level' => ILogger::DEBUG,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

229
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::DEBUG,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
230
				],
231
				$context
232
			));
233
		} else {
234
			$this->logger->debug($message, $context);
235
		}
236
	}
237
238
	/**
239
	 * Logs with an arbitrary level.
240
	 *
241
	 * @param mixed $level
242
	 * @param string $message
243
	 * @param array $context
244
	 *
245
	 * @return void
246
	 *
247
	 * @throws InvalidArgumentException
248
	 */
249
	public function log($level, $message, array $context = []) {
250
		if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) {
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

250
		if (!is_int($level) || $level < /** @scrutinizer ignore-deprecated */ ILogger::DEBUG || $level > ILogger::FATAL) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The constant OCP\ILogger::FATAL has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

250
		if (!is_int($level) || $level < ILogger::DEBUG || $level > /** @scrutinizer ignore-deprecated */ ILogger::FATAL) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
251
			throw new InvalidArgumentException('Nextcloud allows only integer log levels');
252
		}
253
		if ($this->containsThrowable($context)) {
254
			$this->logger->logException($context['exception'], array_merge(
255
				[
256
					'message' => $message,
257
					'level' => $level,
258
				],
259
				$context
260
			));
261
		} else {
262
			$this->logger->log($level, $message, $context);
263
		}
264
	}
265
266
	public function logData(string $message, array $data, array $context = []): void {
267
		$this->logger->logData($message, $data, $context);
268
	}
269
}
270