feat: error-codes in flash messages to reference log entries

This commit is contained in:
Björn Fromme committed 2026-01-23 11:06:24 +01:00
1 parent c41524c1fd
commit aa08307500
7 files changed
+517

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Logger;
use App\Service\ErrorCodeService;
use Monolog\Attribute\AsMonologProcessor;
use Monolog\Level;
use Monolog\LogRecord;
/**
* Adds error reference codes to log records with ERROR level or higher.
*
* When an error is logged, this processor generates a user-friendly error code
* and adds it to the log record's extra data. The code can then be displayed
* to users and used by support staff to locate the corresponding log entries.
*/
#[AsMonologProcessor]
class ErrorCodeProcessor
{
public function __construct(
private readonly ErrorCodeService $errorCodeService,
) {
}
public function __invoke(LogRecord $record): LogRecord
{
if ($record->level->value < Level::Error->value) {
return $record;
}
$errorCode = $this->errorCodeService->getErrorCode();
if (null === $errorCode) {
return $record;
}
$this->errorCodeService->markErrorOccurred();
$record->extra['error_code'] = $errorCode;
return $record;
}
}