Repository navigation
Expand file tree
/
Copy pathLog.php
More file actions
91 lines (73 loc) · 2.34 KB
/
Copy pathLog.php
File metadata and controls
91 lines (73 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Phprest\ErrorHandler\Handler;
use ErrorException;
use Exception;
use League\BooBoo\Handler\HandlerInterface;
use Phprest\Exception\Exception as PhprestException;
use Psr\Log\LoggerInterface;
class Log implements HandlerInterface
{
protected ?LoggerInterface $logger;
public function __construct(LoggerInterface $logger = null)
{
if (null !== $logger) {
$this->setLogger($logger);
}
}
public function setLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
return $this;
}
/**
* @param Exception $exception
*/
public function handle($exception): void
{
if ($exception instanceof ErrorException) {
$this->handleErrorException($exception);
return;
}
if ($this->logger) {
$this->logger->critical($this->buildLogMessage($exception));
}
}
protected function handleErrorException(ErrorException $exception): bool
{
switch ($exception->getSeverity()) {
case E_ERROR:
case E_RECOVERABLE_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_PARSE:
$this->logger->error($this->buildLogMessage($exception));
break;
case E_WARNING:
case E_USER_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
$this->logger->warning($this->buildLogMessage($exception));
break;
case E_NOTICE:
case E_USER_NOTICE:
$this->logger->notice($this->buildLogMessage($exception));
break;
case E_STRICT:
case E_DEPRECATED:
case E_USER_DEPRECATED:
$this->logger->info($this->buildLogMessage($exception));
break;
}
return true;
}
protected function buildLogMessage(Exception $exception): string
{
$message = $exception->getMessage() . "({$exception->getCode()})";
if ($exception instanceof PhprestException && $exception->getDetails()) {
$message .= ' Details :: ' . json_encode($exception->getDetails());
}
$message .= ' Stack trace :: ' . $exception->getTraceAsString();
return $message;
}
}