Feat: Implement logging database handler

This commit is contained in:
Björn Fromme
2023-10-21 15:16:28 +02:00
parent cc93a0be0c
commit bf2e5600d0
9 changed files with 87 additions and 21 deletions
+1
View File
@@ -1,6 +1,7 @@
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
schema_filter: ~^(?!log)~
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
+8
View File
@@ -24,6 +24,10 @@ when@dev:
path: '%kernel.logs_dir%/myep.%kernel.environment%.log'
max_files: 5
level: info
database:
channels: ["myep"]
type: service
id: App\Logging\DatabaseHandler
console:
type: console
process_psr_3_messages: false
@@ -70,6 +74,10 @@ when@prod:
path: '%kernel.logs_dir%/myep.%kernel.environment%.log'
max_files: 5
level: info
database:
channels: ["myep"]
type: service
id: App\Logging\DatabaseHandler
console:
type: console
process_psr_3_messages: false
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231021125423 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
$this->connection->executeQuery('CREATE TABLE log (id INT AUTO_INCREMENT NOT NULL, message VARCHAR(255) DEFAULT NULL, context JSON NOT NULL COMMENT \'(DC2Type:json)\', extra JSON NOT NULL COMMENT \'(DC2Type:json)\', level VARCHAR(32) NOT NULL, channel VARCHAR(255) NOT NULL, timestamp DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}
public function down(Schema $schema): void
{
$this->addSql('DROP TABLE log');
}
}
+11 -11
View File
@@ -69,8 +69,8 @@ class UserDataHandler
$teamer->setCrmSelections($crmSelections);
$user->setTeamer($teamer);
$this->logger->info('Create teamer', [
'id' => $teamer->getId(),
'name' => $teamer->getFullName(),
'teamer_id' => $teamer->getId(),
'teamer_name' => (string) $teamer,
]);
}
@@ -78,8 +78,8 @@ class UserDataHandler
$this->entityManager->flush();
$this->logger->info('Create user', [
'id' => $user->getId(),
'email' => $user->getEmail(),
'user_id' => $user->getId(),
'user_email' => $user->getEmail(),
]);
return $user;
@@ -97,18 +97,18 @@ class UserDataHandler
if (true === $isTeamer) {
$address = Address::fromApiResponse($profileResponse);
$communication = Communication::fromApiResponse($profileResponse);
$user
->getTeamer()
$teamer = $user->getTeamer();
$teamer
->setAddress($address)
->setCommunication($communication)
->setCrmSelections($crmSelections)
;
}
$this->logger->info('Update user', [
'id' => $user->getId(),
'email' => $user->getEmail(),
]);
$this->logger->info('Update teamer', [
'teamer_id' => $teamer->getId(),
'teamer_name' => (string) $teamer,
]);
}
$this->entityManager->flush();
}
@@ -28,7 +28,7 @@ class DeleteController extends AbstractController
$this->addFlash('success', 'Die Bewerbung wurde gelöscht');
$this->logger->info('Delete application', [
'application_id' => $application->getId(),
'destination' => (string) $application->getAssignment()->getDestination(),
'teamer' => (string) $application->getTeamer(),
]);
@@ -28,12 +28,10 @@ class DisposeController extends AbstractController
$this->entityManager->persist($disposition);
$this->entityManager->remove($application);
$this->entityManager->flush();
$this->addFlash('success', 'Der Teamer wurde eingeteilt');
$this->logger->info('Create disposition', [
'application_id' => $application->getId(),
'disposition_id' => $disposition->getId(),
'teamer' => (string) $application->getTeamer(),
]);
@@ -47,15 +47,15 @@ class DeleteController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->eventDispatcher->dispatch(new DispositionDeletedEvent($disposition), DispositionDeletedEvent::NAME);
$this->entityManager->remove($disposition);
$this->entityManager->flush();
$this->addFlash('success', 'Die Einteilung wurde gelöscht');
$this->logger->info('Delete disposition', [
'disposition_id' => $disposition->getId(),
'teamer' => (string) $disposition->getTeamer(),
]);
$this->entityManager->remove($disposition);
$this->entityManager->flush();
$response->setCloseAndRedirect($this->generateUrl('app_admin_assignment_detail', [
'uuid' => $disposition->getAssignment()->getUuid(),
]));
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Logging;
use Doctrine\DBAL\Connection;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\LogRecord;
class DatabaseHandler extends AbstractProcessingHandler
{
public function __construct(private readonly Connection $connection)
{
parent::__construct();
}
protected function write(LogRecord $record): void
{
$this->connection->insert('log', [
'level' => $record->level->getName(),
'channel' => $record->channel,
'message' => $record->message,
'context' => json_encode($record->context),
'extra' => json_encode($record->extra),
'timestamp' => (new \DateTimeImmutable())->format('Y-m-d H:i:s'),
]);
}
}
+7 -4
View File
@@ -2,6 +2,7 @@
namespace App\Logging;
use App\Entity\User;
use Monolog\LogRecord;
use Symfony\Bundle\SecurityBundle\Security;
@@ -16,18 +17,20 @@ class InjectUserProcessor
return $record;
}
if ('cli' === php_sapi_name()) {
/** @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;
} elseif (null === $user = $this->security->getUser()) {
return $record;
}
$record['extra']['user'] = [
$record->extra['user'] = [
'id' => $user->getId(),
'email' => $user->getEmail(),
'roles' => $user->getRoles(),