Passed
Push — master ( 7d851f...e6959b )
by Robin
15:52 queued 12s
created

PsrLoggerAdapter::setEventDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
namespace OC\Log;
27
28
use OC\Log;
29
use OCP\EventDispatcher\IEventDispatcher;
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
	/** @var Log */
40
	private $logger;
41
42
	public function __construct(Log $logger) {
43
		$this->logger = $logger;
44
	}
45
46
	public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
47
		$this->logger->setEventDispatcher($eventDispatcher);
48
	}
49
50
	private function containsThrowable(array $context): bool {
51
		return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
52
	}
53
54
	/**
55
	 * System is unusable.
56
	 *
57
	 * @param string $message
58
	 * @param array $context
59
	 *
60
	 * @return void
61
	 */
62
	public function emergency($message, array $context = []): void {
63
		if ($this->containsThrowable($context)) {
64
			$this->logger->logException($context['exception'], array_merge(
65
				[
66
					'message' => $message,
67
					'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

67
					'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...
68
				],
69
				$context
70
			));
71
		} else {
72
			$this->logger->emergency($message, $context);
73
		}
74
	}
75
76
	/**
77
	 * Action must be taken immediately.
78
	 *
79
	 * Example: Entire website down, database unavailable, etc. This should
80
	 * trigger the SMS alerts and wake you up.
81
	 *
82
	 * @param string $message
83
	 * @param array $context
84
	 *
85
	 * @return void
86
	 */
87
	public function alert($message, array $context = []) {
88
		if ($this->containsThrowable($context)) {
89
			$this->logger->logException($context['exception'], array_merge(
90
				[
91
					'message' => $message,
92
					'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

92
					'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...
93
				],
94
				$context
95
			));
96
		} else {
97
			$this->logger->alert($message, $context);
98
		}
99
	}
100
101
	/**
102
	 * Critical conditions.
103
	 *
104
	 * Example: Application component unavailable, unexpected exception.
105
	 *
106
	 * @param string $message
107
	 * @param array $context
108
	 *
109
	 * @return void
110
	 */
111
	public function critical($message, array $context = []) {
112
		if ($this->containsThrowable($context)) {
113
			$this->logger->logException($context['exception'], array_merge(
114
				[
115
					'message' => $message,
116
					'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

116
					'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...
117
				],
118
				$context
119
			));
120
		} else {
121
			$this->logger->critical($message, $context);
122
		}
123
	}
124
125
	/**
126
	 * Runtime errors that do not require immediate action but should typically
127
	 * be logged and monitored.
128
	 *
129
	 * @param string $message
130
	 * @param array $context
131
	 *
132
	 * @return void
133
	 */
134
	public function error($message, array $context = []) {
135
		if ($this->containsThrowable($context)) {
136
			$this->logger->logException($context['exception'], array_merge(
137
				[
138
					'message' => $message,
139
					'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

139
					'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...
140
				],
141
				$context
142
			));
143
		} else {
144
			$this->logger->error($message, $context);
145
		}
146
	}
147
148
	/**
149
	 * Exceptional occurrences that are not errors.
150
	 *
151
	 * Example: Use of deprecated APIs, poor use of an API, undesirable things
152
	 * that are not necessarily wrong.
153
	 *
154
	 * @param string $message
155
	 * @param array $context
156
	 *
157
	 * @return void
158
	 */
159
	public function warning($message, array $context = []) {
160
		if ($this->containsThrowable($context)) {
161
			$this->logger->logException($context['exception'], array_merge(
162
				[
163
					'message' => $message,
164
					'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

164
					'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...
165
				],
166
				$context
167
			));
168
		} else {
169
			$this->logger->warning($message, $context);
170
		}
171
	}
172
173
	/**
174
	 * Normal but significant events.
175
	 *
176
	 * @param string $message
177
	 * @param array $context
178
	 *
179
	 * @return void
180
	 */
181
	public function notice($message, array $context = []) {
182
		if ($this->containsThrowable($context)) {
183
			$this->logger->logException($context['exception'], array_merge(
184
				[
185
					'message' => $message,
186
					'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

186
					'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...
187
				],
188
				$context
189
			));
190
		} else {
191
			$this->logger->notice($message, $context);
192
		}
193
	}
194
195
	/**
196
	 * Interesting events.
197
	 *
198
	 * Example: User logs in, SQL logs.
199
	 *
200
	 * @param string $message
201
	 * @param array $context
202
	 *
203
	 * @return void
204
	 */
205
	public function info($message, array $context = []) {
206
		if ($this->containsThrowable($context)) {
207
			$this->logger->logException($context['exception'], array_merge(
208
				[
209
					'message' => $message,
210
					'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

210
					'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...
211
				],
212
				$context
213
			));
214
		} else {
215
			$this->logger->info($message, $context);
216
		}
217
	}
218
219
	/**
220
	 * Detailed debug information.
221
	 *
222
	 * @param string $message
223
	 * @param array $context
224
	 *
225
	 * @return void
226
	 */
227
	public function debug($message, array $context = []) {
228
		if ($this->containsThrowable($context)) {
229
			$this->logger->logException($context['exception'], array_merge(
230
				[
231
					'message' => $message,
232
					'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

232
					'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...
233
				],
234
				$context
235
			));
236
		} else {
237
			$this->logger->debug($message, $context);
238
		}
239
	}
240
241
	/**
242
	 * Logs with an arbitrary level.
243
	 *
244
	 * @param mixed $level
245
	 * @param string $message
246
	 * @param array $context
247
	 *
248
	 * @return void
249
	 *
250
	 * @throws InvalidArgumentException
251
	 */
252
	public function log($level, $message, array $context = []) {
253
		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

253
		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

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