|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\ErrorHandler; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Psr\Log\LogLevel; |
|
7
|
|
|
use Psr\Log\NullLogger; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait for logging errors and exceptions |
|
11
|
|
|
*/ |
|
12
|
|
|
trait Logging |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var LoggerInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $logger; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Set the logger for logging errors |
|
22
|
|
|
* |
|
23
|
|
|
* @param LoggerInterface $logger |
|
24
|
|
|
*/ |
|
25
|
116 |
|
public function setLogger(LoggerInterface $logger) |
|
26
|
|
|
{ |
|
27
|
116 |
|
$this->logger = $logger; |
|
28
|
116 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set the logger for logging errors |
|
32
|
|
|
* |
|
33
|
|
|
* @return LoggerInterface |
|
34
|
|
|
*/ |
|
35
|
68 |
|
public function getLogger() |
|
36
|
|
|
{ |
|
37
|
68 |
|
if (!isset($this->logger)) { |
|
38
|
6 |
|
$this->logger = new NullLogger(); |
|
39
|
3 |
|
} |
|
40
|
|
|
|
|
41
|
68 |
|
return $this->logger; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Log an error or exception |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Exception|\Error $error |
|
49
|
|
|
*/ |
|
50
|
66 |
|
public function log($error) |
|
51
|
|
|
{ |
|
52
|
66 |
|
if ($error instanceof \Error || $error instanceof \ErrorException) { |
|
53
|
50 |
|
return $this->logError($error); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
16 |
|
if ($error instanceof \Exception) { |
|
57
|
12 |
|
return $this->logException($error); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
$message = "Unable to log a " . (is_object($error) ? get_class($error) . ' ' : '') . gettype($error); |
|
61
|
4 |
|
$this->getLogger()->log(LogLevel::WARNING, $message); |
|
62
|
4 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Log an error |
|
66
|
|
|
* |
|
67
|
|
|
* @param \Error|\ErrorException $error |
|
68
|
|
|
*/ |
|
69
|
50 |
|
protected function logError($error) |
|
70
|
|
|
{ |
|
71
|
50 |
|
$code = $error instanceof \ErrorException ? $error->getSeverity() : E_ERROR; |
|
72
|
50 |
|
$level = $this->getLogLevel($code); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
50 |
|
$message = sprintf('%s: %s at %s line %s', $this->codeToString($code), $error->getMessage(), |
|
|
|
|
|
|
75
|
50 |
|
$error->getFile(), $error->getLine()); |
|
76
|
|
|
|
|
77
|
|
|
$context = [ |
|
78
|
50 |
|
'error' => $error, |
|
79
|
50 |
|
'code' => $code, |
|
80
|
50 |
|
'message' => $error->getMessage(), |
|
81
|
50 |
|
'file' => $error->getFile(), |
|
82
|
50 |
|
'line' => $error->getLine() |
|
83
|
25 |
|
]; |
|
84
|
|
|
|
|
85
|
50 |
|
$this->getLogger()->log($level, $message, $context); |
|
86
|
50 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Log an exception |
|
90
|
|
|
* |
|
91
|
|
|
* @param \Exception $exception |
|
92
|
|
|
*/ |
|
93
|
12 |
|
protected function logException(\Exception $exception) |
|
94
|
|
|
{ |
|
95
|
12 |
|
$level = $this->getLogLevel(); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
12 |
|
$message = sprintf('Uncaught Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), |
|
98
|
12 |
|
$exception->getFile(), $exception->getLine()); |
|
99
|
|
|
|
|
100
|
12 |
|
$context = compact('exception'); |
|
101
|
|
|
|
|
102
|
12 |
|
$this->getLogger()->log($level, $message, $context); |
|
103
|
12 |
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.