|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://philecms.github.io/ |
|
4
|
|
|
* @license http://opensource.org/licenses/MIT |
|
5
|
|
|
* @package Phile\Plugin\Phile\ErrorHandler |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Phile\Plugin\Phile\ErrorHandler; |
|
9
|
|
|
|
|
10
|
|
|
use Phile\ServiceLocator\ErrorHandlerInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Error handler logging to files |
|
14
|
|
|
*/ |
|
15
|
|
|
class ErrorLog implements ErrorHandlerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor |
|
19
|
|
|
* |
|
20
|
|
|
* @param string|null $logfile path to error.log-file; null: PHP default error log |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(?string $logfile = null) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($logfile) { |
|
25
|
|
|
ini_set('error_log', $logfile); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function handleError( |
|
30
|
|
|
int $errno, |
|
31
|
|
|
string $errstr, |
|
32
|
|
|
?string $errfile, |
|
33
|
|
|
?int $errline |
|
34
|
|
|
): bool { |
|
35
|
|
|
$this->log($errno, $errstr, $errfile, $errline); |
|
36
|
|
|
|
|
37
|
|
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function handleException(\Throwable $exception) |
|
41
|
|
|
{ |
|
42
|
|
|
$code = (int)$exception->getCode(); |
|
43
|
|
|
$message = $exception->getMessage(); |
|
44
|
|
|
$file = $exception->getFile(); |
|
45
|
|
|
$line = $exception->getLine(); |
|
46
|
|
|
$this->log($code, $message, $file, $line); |
|
47
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); |
|
48
|
|
|
exit; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function handleShutdown() |
|
52
|
|
|
{ |
|
53
|
|
|
$error = error_get_last(); |
|
54
|
|
|
if ($error === null) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
$this->log($error['type'], $error['message'], $error['file'], $error['line']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function log(int $code, string $message, ?string $file, ?int $line): void |
|
61
|
|
|
{ |
|
62
|
|
|
error_log("[{$code}] {$message} in {$file} on line {$line}"); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|