Created
January 18, 2024 07:50
-
-
Save grambas/f4a2d8dd8fbbf30f41a658404edd9e1a to your computer and use it in GitHub Desktop.
Log stack trace as string too for exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Monolog; | |
use Throwable; | |
/** | |
* log stack trace instead of exception class | |
*/ | |
class StackTraceProcessor | |
{ | |
public function __invoke(array $record): array | |
{ | |
foreach($record['context'] as $key => $data) { | |
if ($data instanceof Throwable && !in_array('backtrace', $record['context'], true)) { | |
$record['extra']['backtrace'] = $data->getTraceAsString(); | |
unset($record['context'][$key]); | |
} | |
} | |
return $record; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment