Skip to content

Commit

Permalink
ref: LogRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 committed Dec 20, 2023
1 parent b0301d0 commit bcfe9e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
17 changes: 8 additions & 9 deletions EMS/common-bundle/src/Common/Log/DoctrineHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use EMS\CommonBundle\Repository\LogRepository;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\LogRecord;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
Expand All @@ -20,22 +21,20 @@ public function __construct(private readonly LogRepository $logRepository, priva
parent::__construct();
}

/**
* @param array{message: string, level: int, level_name: string, context: array<mixed>, channel: string, formatted: string, datetime: \DateTimeImmutable, extra: array<mixed>} $record
*/
protected function write(array $record): void
protected function write(LogRecord $record): void
{
if ($record['level'] < $this->minLevel) {
$logArray = $record->toArray();
if ($logArray['level'] < $this->minLevel) {
return;
}

$token = $this->tokenStorage->getToken();
$record['username'] = $token instanceof TokenInterface ? $token->getUserIdentifier() : null;
$record['impersonator'] = $token instanceof SwitchUserToken ? $token->getOriginalToken()->getUserIdentifier() : null;
$logArray['username'] = $token instanceof TokenInterface ? $token->getUserIdentifier() : null;
$logArray['impersonator'] = $token instanceof SwitchUserToken ? $token->getOriginalToken()->getUserIdentifier() : null;

$record['context'] = DoctrineHandler::secretContext($record['context']);
$logArray['context'] = DoctrineHandler::secretContext($logArray['context']);

$this->logRepository->insertRecord($record);
$this->logRepository->insertRecord($logArray);
}

/**
Expand Down
17 changes: 8 additions & 9 deletions EMS/core-bundle/src/Core/UI/FlashMessageLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use EMS\CoreBundle\EMSCoreBundle;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\LogRecord;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -18,36 +19,34 @@ public function __construct(private readonly RequestStack $requestStack, private
parent::__construct(Logger::NOTICE);
}

/**
* @param array{level: int, level_name: string, message: string, context: array<mixed>} $record
*/
protected function write(array $record): void
protected function write(LogRecord $record): void
{
$logArray = $record->toArray();
if (null === $currentRequest = $this->requestStack->getCurrentRequest()) {
return;
}

$headers = $currentRequest->headers;
$logLevel = $headers->has('x-log-level') ? (int) $headers->get('x-log-level') : Logger::NOTICE;

if ($record['level'] < $logLevel) {
if ($logArray['level'] < $logLevel) {
return;
}

if (true === ($record['context']['noFlash'] ?? false)) {
if (true === ($logArray['context']['noFlash'] ?? false)) {
return;
}

// TODO: remove the translator when all logger have been migrated to the localized logger
$parameters = [];
foreach ($record['context'] as $key => &$value) {
foreach ($logArray['context'] as $key => &$value) {
$parameters['%'.$key.'%'] = $value;
}

$message = $this->translator->trans($record['message'], $parameters, EMSCoreBundle::TRANS_DOMAIN);
$message = $this->translator->trans($logArray['message'], $parameters, EMSCoreBundle::TRANS_DOMAIN);

/** @var Session $session */
$session = $currentRequest->getSession();
$session->getFlashBag()->add(\strtolower($record['level_name']), $message);
$session->getFlashBag()->add(\strtolower($logArray['level_name']), $message);
}
}

0 comments on commit bcfe9e6

Please sign in to comment.