Skip to content

Commit

Permalink
Parse exceptions before sending it to the queue worker to support log…
Browse files Browse the repository at this point in the history
…ging of error/exceptions trough queues.

Use getTraceAsString() instead of getTrace() on exceptions to avoid serialization issues with Closures.
  • Loading branch information
danielme85 committed May 4, 2020
1 parent 98d378d commit af17616
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
60 changes: 51 additions & 9 deletions src/LogToDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,11 @@ public function getModel()
public function newFromMonolog(array $record)
{
if (!empty($this->connection)) {
if (!empty($record['context']) && !empty($record['context']['exception'])) {
$record['context']['exception'] = $this->parseIfException($record['context']['exception']);
}

if ($this->config['queue']) {
if (!empty($record['context']['exception'])) {
//Check for exception, they can't be queued.
$exception = $record['context']['exception'];
if (get_class($exception) === \Exception::class
|| is_subclass_of($exception, \Exception::class)) {
dispatch_now(new SaveNewLogEvent($this, $record));
}
}
if (empty($this->config['queue_name']) && empty($this->config['queue_connection'])) {
dispatch(new SaveNewLogEvent($this, $record));
} else if (!empty($this->config['queue_name']) && !empty($this->config['queue_connection'])) {
Expand Down Expand Up @@ -190,7 +186,53 @@ public function newFromMonolog(array $record)
* @param string $config
* @return mixed|null
*/
public function getConfig(string $config) {
public function getConfig(string $config)
{
return $this->config[$config] ?? null;
}

/**
* Parse the exception class
*
* @param mixed $exception
* @return mixed
*/
private function parseIfException($exception)
{
if (is_object($exception)) {
if (get_class($exception) === \Exception::class
|| is_subclass_of($exception, \Exception::class)
|| strpos(get_class($exception), "Exception") !== false) {

$newexception = [];

if (method_exists($exception, 'getMessage')) {
$newexception['message'] = $exception->getMessage();
}
if (method_exists($exception, 'getCode')) {
$newexception['code'] = $exception->getCode();
}
if (method_exists($exception, 'getFile')) {
$newexception['file'] = $exception->getFile();
}
if (method_exists($exception, 'getLine')) {
$newexception['line'] = $exception->getLine();
}
if (method_exists($exception, 'getTrace')) {
$newexception['trace'] = $exception->getTraceAsString();
}
if (method_exists($exception, 'getPrevious')) {
$newexception['previous'] = $exception->getPrevious();
}
if (method_exists($exception, 'getSeverity')) {
$newexception['severity'] = $exception->getSeverity();
}

$exception = $newexception;
}
}

return $exception;
}

}
34 changes: 1 addition & 33 deletions src/Models/LogToDbCreateObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,6 @@ public function getExtraAttribute($value)
*/
public function setContextAttribute(array $value)
{
if (isset($value['exception'])) {
if (!empty($value['exception'])) {
$exception = $value['exception'];
if (get_class($exception) === \Exception::class
|| is_subclass_of($exception, \Exception::class)
|| strpos(get_class($exception), "Exception") !== false) {
$newexception = [];
if (method_exists($exception, 'getMessage')) {
$newexception['message'] = $exception->getMessage();
}
if (method_exists($exception, 'getCode')) {
$newexception['code'] = $exception->getCode();
}
if (method_exists($exception, 'getFile')) {
$newexception['file'] = $exception->getFile();
}
if (method_exists($exception, 'getLine')) {
$newexception['line'] = $exception->getLine();
}
if (method_exists($exception, 'getTrace')) {
$newexception['trace'] = $exception->getTrace();
}
if (method_exists($exception, 'getPrevious')) {
$newexception['previous'] = $exception->getPrevious();
}
if (method_exists($exception, 'getSeverity')) {
$newexception['severity'] = $exception->getSeverity();
}
$value['exception'] = $newexception;
}
}
}
$this->attributes['context'] = $this->jsonEncodeIfNotEmpty($value);
}

Expand Down Expand Up @@ -144,7 +112,7 @@ public function setExtraAttribute($value)
private function jsonEncodeIfNotEmpty($value)
{
if (!empty($value)) {
return json_encode($value);
return @json_encode($value) ?? null;
}

return $value;
Expand Down
14 changes: 13 additions & 1 deletion tests/LogToDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,25 @@ public function testQueue()
Queue::assertPushed(SaveNewLogEvent::class, 6);

config()->set('logtodb.queue_name', 'logHandler');

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

config()->set('logtodb.queue_name', null);
config()->set('logtodb.queue_connection', 'default');

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

Queue::assertPushed(SaveNewLogEvent::class, 12);
config()->set('logtodb.queue_name', 'logHandler');

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

Queue::assertPushed(SaveNewLogEvent::class, 24);

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

Expand Down

0 comments on commit af17616

Please sign in to comment.