Feat: Add logging

This commit is contained in:
Björn Fromme
2023-10-20 17:37:14 +02:00
parent d7bbb4bb29
commit b29960075d
4 changed files with 69 additions and 6 deletions
+18 -5
View File
@@ -2,6 +2,7 @@ monolog:
channels:
- deprecation
- bpn
- myep
when@dev:
monolog:
@@ -10,11 +11,17 @@ when@dev:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event", "!bpn"]
channels: ["!event", "!bpn", "!myep"]
bpn:
channels: ["bpn"]
type: rotating_file
path: '%kernel.logs_dir%/%kernel.environment%.bpn.log'
path: '%kernel.logs_dir%/bpn.%kernel.environment%.log'
max_files: 5
level: info
myep:
channels: [ "myep" ]
type: rotating_file
path: '%kernel.logs_dir%/myep.%kernel.environment%.log'
max_files: 5
level: info
console:
@@ -45,7 +52,7 @@ when@prod:
handler: nested
excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
channels: ["!bpn"]
channels: ["!bpn", "!myep"]
nested:
type: stream
path: php://stderr
@@ -54,13 +61,19 @@ when@prod:
bpn:
channels: ["bpn"]
type: rotating_file
path: '%kernel.logs_dir%/%kernel.environment%.bpn.log'
path: '%kernel.logs_dir%/bpn.%kernel.environment%.log'
max_files: 5
level: info
myep:
channels: [ "myep" ]
type: rotating_file
path: '%kernel.logs_dir%/myep.%kernel.environment%.log'
max_files: 5
level: info
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!bpn"]
channels: ["!event", "!doctrine", "!bpn", "!myep"]
deprecation:
type: stream
channels: [deprecation]
+8
View File
@@ -11,6 +11,8 @@ services:
_defaults:
autowire: true
autoconfigure: true
bind:
$logger: '@monolog.logger.myep'
App\:
resource: '../src/'
@@ -53,6 +55,12 @@ services:
$intlExtension: '@twig.extension.intl'
$environment: '%kernel.environment%'
App\Logging\InjectUserProcessor:
arguments:
- '@security.helper'
tags:
- { name: monolog.processor, channel: myep }
App\Menu\AdminMenuBuilder:
arguments:
$factory: '@knp_menu.factory'
+5 -1
View File
@@ -5,6 +5,7 @@ namespace App\Command;
use App\BusProNet\DataProvider\HotelDataProvider;
use App\BusProNet\DataProvider\PickupDataProvider;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
@@ -22,6 +23,7 @@ class BpnImportCommand extends Command
private readonly Connection $connection,
private readonly HotelDataProvider $hotelDataProvider,
private readonly PickupDataProvider $pickupDataProvider,
private readonly LoggerInterface $logger,
private readonly string $xmlFilesPath
) {
parent::__construct();
@@ -124,7 +126,9 @@ class BpnImportCommand extends Command
}
}
$io->info('Added '.$addedCount.' and updated '.$updatedCount.' destinations');
$logMessage = 'Added '.$addedCount.' and updated '.$updatedCount.' destinations';
$io->info($logMessage);
$this->logger->info($logMessage);
return Command::SUCCESS;
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Logging;
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;
}
if ('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'] = [
'id' => $user->getId(),
'email' => $user->getEmail(),
'roles' => $user->getRoles(),
];
return $record;
}
}