|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Bart Visscher <[email protected]> |
|
9
|
|
|
* @author Björn Schießle <[email protected]> |
|
10
|
|
|
* @author Christoph Wurst <[email protected]> |
|
11
|
|
|
* @author Joas Schilling <[email protected]> |
|
12
|
|
|
* @author Julius Härtl <[email protected]> |
|
13
|
|
|
* @author Morris Jobke <[email protected]> |
|
14
|
|
|
* @author Thomas Müller <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* @license AGPL-3.0 |
|
17
|
|
|
* |
|
18
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
19
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* as published by the Free Software Foundation. |
|
21
|
|
|
* |
|
22
|
|
|
* This program is distributed in the hope that it will be useful, |
|
23
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
24
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
25
|
|
|
* GNU Affero General Public License for more details. |
|
26
|
|
|
* |
|
27
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
28
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
namespace OC\Log; |
|
32
|
|
|
|
|
33
|
|
|
use Error; |
|
34
|
|
|
use OCP\ILogger; |
|
35
|
|
|
use Psr\Log\LoggerInterface; |
|
36
|
|
|
use Throwable; |
|
37
|
|
|
|
|
38
|
|
|
class ErrorHandler { |
|
39
|
|
|
private LoggerInterface $logger; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(LoggerInterface $logger) { |
|
42
|
|
|
$this->logger = $logger; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Remove password in URLs |
|
47
|
|
|
*/ |
|
48
|
|
|
private static function removePassword(string $msg): string { |
|
49
|
|
|
return preg_replace('#//(.*):(.*)@#', '//xxx:xxx@', $msg); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Fatal errors handler |
|
54
|
|
|
*/ |
|
55
|
|
|
public function onShutdown(): void { |
|
56
|
|
|
$error = error_get_last(); |
|
57
|
|
|
if ($error) { |
|
58
|
|
|
$msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line']; |
|
59
|
|
|
$this->logger->critical(self::removePassword($msg), ['app' => 'PHP']); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Uncaught exception handler |
|
65
|
|
|
*/ |
|
66
|
|
|
public function onException(Throwable $exception): void { |
|
67
|
|
|
$class = get_class($exception); |
|
68
|
|
|
$msg = $exception->getMessage(); |
|
69
|
|
|
$msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine(); |
|
70
|
|
|
$this->logger->critical(self::removePassword($msg), ['app' => 'PHP']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Recoverable errors handler |
|
75
|
|
|
*/ |
|
76
|
|
|
public function onError(int $number, string $message, string $file, int $line): bool { |
|
77
|
|
|
if (!(error_reporting() & $number)) { |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
$msg = $message . ' at ' . $file . '#' . $line; |
|
81
|
|
|
$e = new Error(self::removePassword($msg)); |
|
82
|
|
|
$this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']); |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Recoverable handler which catch all errors, warnings and notices |
|
88
|
|
|
*/ |
|
89
|
|
|
public function onAll(int $number, string $message, string $file, int $line): bool { |
|
90
|
|
|
$msg = $message . ' at ' . $file . '#' . $line; |
|
91
|
|
|
$e = new Error(self::removePassword($msg)); |
|
92
|
|
|
$this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']); |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private static function errnoToLogLevel(int $errno): int { |
|
97
|
|
|
switch ($errno) { |
|
98
|
|
|
case E_USER_WARNING: |
|
99
|
|
|
return ILogger::WARN; |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
case E_DEPRECATED: |
|
102
|
|
|
case E_USER_DEPRECATED: |
|
103
|
|
|
return ILogger::DEBUG; |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
case E_USER_NOTICE: |
|
106
|
|
|
return ILogger::INFO; |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
case E_USER_ERROR: |
|
109
|
|
|
default: |
|
110
|
|
|
return ILogger::ERROR; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
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.