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