Skip to content

Commit af17616

Browse files
committed
Parse exceptions before sending it to the queue worker to support logging of error/exceptions trough queues.
Use getTraceAsString() instead of getTrace() on exceptions to avoid serialization issues with Closures.
1 parent 98d378d commit af17616

File tree

3 files changed

+65
-43
lines changed

3 files changed

+65
-43
lines changed

src/LogToDB.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,11 @@ public function getModel()
147147
public function newFromMonolog(array $record)
148148
{
149149
if (!empty($this->connection)) {
150+
if (!empty($record['context']) && !empty($record['context']['exception'])) {
151+
$record['context']['exception'] = $this->parseIfException($record['context']['exception']);
152+
}
153+
150154
if ($this->config['queue']) {
151-
if (!empty($record['context']['exception'])) {
152-
//Check for exception, they can't be queued.
153-
$exception = $record['context']['exception'];
154-
if (get_class($exception) === \Exception::class
155-
|| is_subclass_of($exception, \Exception::class)) {
156-
dispatch_now(new SaveNewLogEvent($this, $record));
157-
}
158-
}
159155
if (empty($this->config['queue_name']) && empty($this->config['queue_connection'])) {
160156
dispatch(new SaveNewLogEvent($this, $record));
161157
} else if (!empty($this->config['queue_name']) && !empty($this->config['queue_connection'])) {
@@ -190,7 +186,53 @@ public function newFromMonolog(array $record)
190186
* @param string $config
191187
* @return mixed|null
192188
*/
193-
public function getConfig(string $config) {
189+
public function getConfig(string $config)
190+
{
194191
return $this->config[$config] ?? null;
195192
}
193+
194+
/**
195+
* Parse the exception class
196+
*
197+
* @param mixed $exception
198+
* @return mixed
199+
*/
200+
private function parseIfException($exception)
201+
{
202+
if (is_object($exception)) {
203+
if (get_class($exception) === \Exception::class
204+
|| is_subclass_of($exception, \Exception::class)
205+
|| strpos(get_class($exception), "Exception") !== false) {
206+
207+
$newexception = [];
208+
209+
if (method_exists($exception, 'getMessage')) {
210+
$newexception['message'] = $exception->getMessage();
211+
}
212+
if (method_exists($exception, 'getCode')) {
213+
$newexception['code'] = $exception->getCode();
214+
}
215+
if (method_exists($exception, 'getFile')) {
216+
$newexception['file'] = $exception->getFile();
217+
}
218+
if (method_exists($exception, 'getLine')) {
219+
$newexception['line'] = $exception->getLine();
220+
}
221+
if (method_exists($exception, 'getTrace')) {
222+
$newexception['trace'] = $exception->getTraceAsString();
223+
}
224+
if (method_exists($exception, 'getPrevious')) {
225+
$newexception['previous'] = $exception->getPrevious();
226+
}
227+
if (method_exists($exception, 'getSeverity')) {
228+
$newexception['severity'] = $exception->getSeverity();
229+
}
230+
231+
$exception = $newexception;
232+
}
233+
}
234+
235+
return $exception;
236+
}
237+
196238
}

src/Models/LogToDbCreateObject.php

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,38 +80,6 @@ public function getExtraAttribute($value)
8080
*/
8181
public function setContextAttribute(array $value)
8282
{
83-
if (isset($value['exception'])) {
84-
if (!empty($value['exception'])) {
85-
$exception = $value['exception'];
86-
if (get_class($exception) === \Exception::class
87-
|| is_subclass_of($exception, \Exception::class)
88-
|| strpos(get_class($exception), "Exception") !== false) {
89-
$newexception = [];
90-
if (method_exists($exception, 'getMessage')) {
91-
$newexception['message'] = $exception->getMessage();
92-
}
93-
if (method_exists($exception, 'getCode')) {
94-
$newexception['code'] = $exception->getCode();
95-
}
96-
if (method_exists($exception, 'getFile')) {
97-
$newexception['file'] = $exception->getFile();
98-
}
99-
if (method_exists($exception, 'getLine')) {
100-
$newexception['line'] = $exception->getLine();
101-
}
102-
if (method_exists($exception, 'getTrace')) {
103-
$newexception['trace'] = $exception->getTrace();
104-
}
105-
if (method_exists($exception, 'getPrevious')) {
106-
$newexception['previous'] = $exception->getPrevious();
107-
}
108-
if (method_exists($exception, 'getSeverity')) {
109-
$newexception['severity'] = $exception->getSeverity();
110-
}
111-
$value['exception'] = $newexception;
112-
}
113-
}
114-
}
11583
$this->attributes['context'] = $this->jsonEncodeIfNotEmpty($value);
11684
}
11785

@@ -144,7 +112,7 @@ public function setExtraAttribute($value)
144112
private function jsonEncodeIfNotEmpty($value)
145113
{
146114
if (!empty($value)) {
147-
return json_encode($value);
115+
return @json_encode($value) ?? null;
148116
}
149117

150118
return $value;

tests/LogToDbTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,25 @@ public function testQueue()
260260
Queue::assertPushed(SaveNewLogEvent::class, 6);
261261

262262
config()->set('logtodb.queue_name', 'logHandler');
263+
264+
Log::info("I'm supposed to be added to the queue...");
265+
Log::warning("I'm supposed to be added to the queue...");
266+
Log::debug("I'm supposed to be added to the queue...");
267+
268+
config()->set('logtodb.queue_name', null);
263269
config()->set('logtodb.queue_connection', 'default');
264270

265271
Log::info("I'm supposed to be added to the queue...");
266272
Log::warning("I'm supposed to be added to the queue...");
267273
Log::debug("I'm supposed to be added to the queue...");
268274

269-
Queue::assertPushed(SaveNewLogEvent::class, 12);
275+
config()->set('logtodb.queue_name', 'logHandler');
276+
277+
Log::info("I'm supposed to be added to the queue...");
278+
Log::warning("I'm supposed to be added to the queue...");
279+
Log::debug("I'm supposed to be added to the queue...");
280+
281+
Queue::assertPushed(SaveNewLogEvent::class, 24);
270282

271283
config()->set('logtodb.queue', false);
272284

0 commit comments

Comments
 (0)