Files
myep-team/src/Logging/InjectUserProcessor.php
T

42 lines
908 B
PHP

<?php
namespace App\Logging;
use App\Entity\User;
use Monolog\LogRecord;
use Symfony\Bundle\SecurityBundle\Security;
class InjectUserProcessor
{
public function __construct(private readonly Security $security)
{}
public function __invoke(LogRecord $record): LogRecord
{
if (isset($record->extra['user'])) {
return $record;
}
/** @var User $user */
$user = $this->security->getUser();
if (null === $user || 'cli' === php_sapi_name()) {
$record->extra['user'] = [
'id' => null,
'username' => 'system',
'role' => 'system',
];
return $record;
}
$record->extra['user'] = [
'id' => $user->getId(),
'email' => $user->getEmail(),
'roles' => $user->getRoles(),
];
return $record;
}
}