feat: log to database
This commit is contained in:
@@ -296,6 +296,7 @@ class ApiClient
|
||||
|
||||
$this->logger->info('Sending request to BPN API', [
|
||||
'requestId' => $requestId,
|
||||
'type' => $data['satz']['@typ'],
|
||||
]);
|
||||
|
||||
if (true === $debug || true === $this->config['debug']) {
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Table(name: 'core_log_entry')]
|
||||
#[ORM\Entity]
|
||||
class LogEntry
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private ?string $channel;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private ?string $message;
|
||||
|
||||
#[ORM\Column(type: 'json', nullable: true)]
|
||||
private ?array $context = [];
|
||||
|
||||
#[ORM\Column(type: 'json', nullable: true)]
|
||||
private ?array $extra = [];
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private \DateTimeImmutable $createdAt;
|
||||
|
||||
public function __construct(string $channel, string $message)
|
||||
{
|
||||
$this->channel = $channel;
|
||||
$this->message = $message;
|
||||
$this->createdAt = new \DateTimeImmutable('now');
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getMessage(): ?string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function setMessage(string $message): self
|
||||
{
|
||||
$this->message = $message;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getChannel(): ?string
|
||||
{
|
||||
return $this->channel;
|
||||
}
|
||||
|
||||
public function setChannel(?string $channel): self
|
||||
{
|
||||
$this->channel = $channel;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getContext(): ?array
|
||||
{
|
||||
return $this->context;
|
||||
}
|
||||
|
||||
public function setContext(?array $context): self
|
||||
{
|
||||
$this->context = $context;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getExtra(): ?array
|
||||
{
|
||||
return $this->extra;
|
||||
}
|
||||
|
||||
public function setExtra(?array $extra): self
|
||||
{
|
||||
$this->extra = $extra;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(\DateTimeImmutable $createdAt): self
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Logger;
|
||||
|
||||
use App\Entity\LogEntry;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\LogRecord;
|
||||
|
||||
class DatabaseHandler extends AbstractProcessingHandler
|
||||
{
|
||||
public function __construct(private readonly EntityManagerInterface $entityManager)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function write(LogRecord $record): void
|
||||
{
|
||||
$message = $this->replacePlaceHolder($record);
|
||||
|
||||
$logEntry = new LogEntry($record->channel, $message);
|
||||
$logEntry
|
||||
->setContext($record->context)
|
||||
->setExtra($record->extra)
|
||||
;
|
||||
|
||||
$this->entityManager->persist($logEntry);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
private function replacePlaceHolder(LogRecord $record): string
|
||||
{
|
||||
$message = $record->message;
|
||||
|
||||
if (!str_contains($message, '{')) {
|
||||
return $message;
|
||||
}
|
||||
|
||||
$replacements = [];
|
||||
foreach ($record->context as $k => $v) {
|
||||
$replacements['{'.$k.'}'] = $v;
|
||||
}
|
||||
|
||||
return strtr($message, $replacements);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Logger;
|
||||
|
||||
use Monolog\Attribute\AsMonologProcessor;
|
||||
use Monolog\LogRecord;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
#[AsMonologProcessor]
|
||||
class RequestInfoProcessor
|
||||
{
|
||||
public function __construct(private readonly RequestStack $requestStack)
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(LogRecord $record): LogRecord
|
||||
{
|
||||
if (null == $request = $this->requestStack->getMainRequest()) {
|
||||
return $record;
|
||||
}
|
||||
|
||||
$record['extra']['uri'] = $request->getRequestUri();
|
||||
$record['extra']['method'] = $request->getMethod();
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Logger;
|
||||
|
||||
use App\Entity\User;
|
||||
use Monolog\Attribute\AsMonologProcessor;
|
||||
use Monolog\LogRecord;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
|
||||
|
||||
#[AsMonologProcessor]
|
||||
class UserDataProcessor
|
||||
{
|
||||
public function __construct(private readonly Security $security)
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(LogRecord $record): LogRecord
|
||||
{
|
||||
if (isset($record->extra['user'])) {
|
||||
return $record;
|
||||
}
|
||||
|
||||
if ('cli' === PHP_SAPI) {
|
||||
$record->extra['user'] = [
|
||||
'id' => null,
|
||||
'username' => 'SYSTEM',
|
||||
'role' => 'system',
|
||||
];
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (null === $user) {
|
||||
$record->extra['user'] = [
|
||||
'id' => null,
|
||||
'username' => 'ANONYMOUS',
|
||||
'role' => 'public',
|
||||
];
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
// Check for switched or aliased users
|
||||
$originalUser = null;
|
||||
$token = $this->security->getToken();
|
||||
if ($token instanceof SwitchUserToken) {
|
||||
// User is currently switched to
|
||||
$originalUser = $token->getOriginalToken()->getUser();
|
||||
}
|
||||
|
||||
if (null !== $originalUser) {
|
||||
$username = sprintf('%s (via %s)', $user->getUserIdentifier(), $originalUser->getUserIdentifier());
|
||||
} else {
|
||||
$username = $user->getUserIdentifier();
|
||||
}
|
||||
|
||||
$record->extra['user'] = [
|
||||
'id' => $user->getId(),
|
||||
'username' => $username,
|
||||
'roles' => $user->getRoles(),
|
||||
];
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user