1
|
|
|
<?php namespace Understand\UnderstandLaravel5; |
2
|
|
|
|
3
|
|
|
use Illuminate\Config\Repository; |
4
|
|
|
use Exception; |
5
|
|
|
use Throwable; |
6
|
|
|
|
7
|
|
|
class ExceptionLogger |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Log writer |
12
|
|
|
* |
13
|
|
|
* @var Logger |
14
|
|
|
*/ |
15
|
|
|
protected $logger; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Exception encoder |
19
|
|
|
* |
20
|
|
|
* @var ExceptionEncoder |
21
|
|
|
*/ |
22
|
|
|
protected $encoder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Configuration |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Logger $logger |
33
|
|
|
* @param ExceptionEncoder $encoder |
34
|
|
|
* @param Repository $config |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
Logger $logger, |
38
|
|
|
ExceptionEncoder $encoder, |
39
|
|
|
Repository $config |
40
|
|
|
) |
41
|
|
|
{ |
42
|
|
|
$this->logger = $logger; |
43
|
|
|
$this->encoder = $encoder; |
44
|
|
|
$this->config = $config; |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->encoder->setProjectRoot($this->config->get('understand-laravel.project_root')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Send PHP exception to Understand.io |
51
|
|
|
* |
52
|
|
|
* @deprecated |
53
|
|
|
* @param mixed $error |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function log($error) |
57
|
|
|
{ |
58
|
|
|
if ( ! $this->canHandle($error)) |
59
|
|
|
{ |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$errorArr = $this->encoder->exceptionToArray($error); |
64
|
|
|
|
65
|
|
|
return $this->dispatch($errorArr); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $level |
70
|
|
|
* @param mixed $message |
71
|
|
|
* @param mixed $context |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function logError($level, $message, $context) |
75
|
|
|
{ |
76
|
|
|
if ( ! $this->canHandle($message)) |
77
|
|
|
{ |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($message instanceof Exception || $message instanceof Throwable) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$log = $this->encoder->exceptionToArray($message); |
84
|
|
|
} |
85
|
|
|
// integer, float, string or boolean as message |
86
|
|
|
else if (is_scalar($message)) |
87
|
|
|
{ |
88
|
|
|
$log = [ |
89
|
|
|
'message' => $message |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$log = $this->encoder->setCurrentStackTrace($log); |
93
|
|
|
} |
94
|
|
|
else |
95
|
|
|
{ |
96
|
|
|
$log = (array)$message; |
97
|
|
|
$log = $this->encoder->setCurrentStackTrace($log); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$log['level'] = $level; |
101
|
|
|
|
102
|
|
|
if ($context) |
103
|
|
|
{ |
104
|
|
|
$log['context'] = (array)$context; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->dispatch($log); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $errorArr |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function dispatch(array $errorArr) |
115
|
|
|
{ |
116
|
|
|
$errorArr['tags'] = ['error_log']; |
117
|
|
|
|
118
|
|
|
$additionalFields = $this->getMetaFields(); |
119
|
|
|
$customFields = $this->config->get('understand-laravel.errors.meta', []); |
|
|
|
|
120
|
|
|
|
121
|
|
|
return $this->logger->log($errorArr, $additionalFields, $customFields); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected function getMetaFields() |
128
|
|
|
{ |
129
|
|
|
return [ |
130
|
|
|
'session_id' => 'UnderstandFieldProvider::getSessionId', |
131
|
|
|
'request_id' => 'UnderstandFieldProvider::getProcessIdentifier', |
132
|
|
|
'group_id' => 'UnderstandFieldProvider::getGroupId', |
133
|
|
|
'user_id' => 'UnderstandFieldProvider::getUserId', |
134
|
|
|
'env' => 'UnderstandFieldProvider::getEnvironment', |
135
|
|
|
'url' => 'UnderstandFieldProvider::getUrl', |
136
|
|
|
'method' => 'UnderstandFieldProvider::getRequestMethod', |
137
|
|
|
'client_ip' => 'UnderstandFieldProvider::getClientIp', |
138
|
|
|
'user_agent' => 'UnderstandFieldProvider::getClientUserAgent', |
139
|
|
|
'laravel_version' => 'UnderstandFieldProvider::getLaravelVersion', |
140
|
|
|
'sql_queries' => 'UnderstandFieldProvider::getSqlQueries', |
141
|
|
|
'artisan_command' => 'UnderstandFieldProvider::getArtisanCommandName', |
142
|
|
|
'console' => 'UnderstandFieldProvider::getRunningInConsole', |
143
|
|
|
'logger_version' => 'UnderstandFieldProvider::getLoggerVersion', |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param mixed $error |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
protected function canHandle($error) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
return (bool)$this->config->get('understand-laravel.enabled'); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..