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

This commit is contained in:
Björn Fromme
2026-03-16 12:02:59 +01:00
parent 21a3e8fafc
commit afe5aef9f7
7 changed files with 517 additions and 0 deletions
+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;
}
}