Skip to content

Commit 83d36ac

Browse files
authored
Merge pull request #29 from Manriel/patch-1
Wrong format of exception in log context fix
2 parents e4def4b + a70d0cc commit 83d36ac

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/LogToDbCustomLoggingHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function __construct(array $config,
5555
protected function write(array $record): void
5656
{
5757
if (!empty($record)) {
58-
if (!empty($record['context']['exception']) &&
58+
if (!empty($record['context']['exception']) && is_object($record['context']['exception']) &&
5959
get_class($record['context']['exception']) === DBLogException::class) {
6060
//Do nothing if empty log record or an error Exception from itself.
6161
} else {
@@ -72,4 +72,4 @@ protected function write(array $record): void
7272
}
7373
}
7474
}
75-
}
75+
}

tests/LogToDbTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,25 @@ public function testException()
245245
$this->expectException(DBLogException::class);
246246
throw new DBLogException('Dont log this');
247247
}
248+
249+
/**
250+
* Test exception when expected format is wrong.
251+
*
252+
* @group exception
253+
*/
254+
public function testExceptionWrongFormat()
255+
{
256+
$e = [
257+
'message' => 'Array instead exception',
258+
'code' => 0,
259+
'file' => __FILE__,
260+
'line' => __LINE__,
261+
'trace' => debug_backtrace(),
262+
];
263+
Log::warning("Error", ['exception' => $e, 'more' => 'infohere']);
264+
$log = LogToDB::model()->where('message', 'Error')->first();
265+
$this->assertNotEmpty($log->context);
266+
}
248267

249268
/**
250269
*
@@ -564,4 +583,4 @@ public function testFinalCleanup()
564583

565584
$this->artisan('migrate:rollback', ['--database' => 'mysql']);
566585
}
567-
}
586+
}

0 commit comments

Comments
 (0)