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: channels:
- deprecation - deprecation
- bpn - bpn
- myep
when@dev: when@dev:
monolog: monolog:
@@ -10,11 +11,17 @@ when@dev:
type: stream type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log" path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug level: debug
channels: ["!event", "!bpn"] channels: ["!event", "!bpn", "!myep"]
bpn: bpn:
channels: ["bpn"] channels: ["bpn"]
type: rotating_file 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 max_files: 5
level: info level: info
console: console:
@@ -45,7 +52,7 @@ when@prod:
handler: nested handler: nested
excluded_http_codes: [404, 405] excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks buffer_size: 50 # How many messages should be saved? Prevent memory leaks
channels: ["!bpn"] channels: ["!bpn", "!myep"]
nested: nested:
type: stream type: stream
path: php://stderr path: php://stderr
@@ -54,13 +61,19 @@ when@prod:
bpn: bpn:
channels: ["bpn"] channels: ["bpn"]
type: rotating_file 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 max_files: 5
level: info level: info
console: console:
type: console type: console
process_psr_3_messages: false process_psr_3_messages: false
channels: ["!event", "!doctrine", "!bpn"] channels: ["!event", "!doctrine", "!bpn", "!myep"]
deprecation: deprecation:
type: stream type: stream
channels: [deprecation] channels: [deprecation]
+8
View File
@@ -11,6 +11,8 @@ services:
_defaults: _defaults:
autowire: true autowire: true
autoconfigure: true autoconfigure: true
bind:
$logger: '@monolog.logger.myep'
App\: App\:
resource: '../src/' resource: '../src/'
@@ -53,6 +55,12 @@ services:
$intlExtension: '@twig.extension.intl' $intlExtension: '@twig.extension.intl'
$environment: '%kernel.environment%' $environment: '%kernel.environment%'
App\Logging\InjectUserProcessor:
arguments:
- '@security.helper'
tags:
- { name: monolog.processor, channel: myep }
App\Menu\AdminMenuBuilder: App\Menu\AdminMenuBuilder:
arguments: arguments:
$factory: '@knp_menu.factory' $factory: '@knp_menu.factory'
+5 -1
View File
@@ -5,6 +5,7 @@ namespace App\Command;
use App\BusProNet\DataProvider\HotelDataProvider; use App\BusProNet\DataProvider\HotelDataProvider;
use App\BusProNet\DataProvider\PickupDataProvider; use App\BusProNet\DataProvider\PickupDataProvider;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@@ -22,6 +23,7 @@ class BpnImportCommand extends Command
private readonly Connection $connection, private readonly Connection $connection,
private readonly HotelDataProvider $hotelDataProvider, private readonly HotelDataProvider $hotelDataProvider,
private readonly PickupDataProvider $pickupDataProvider, private readonly PickupDataProvider $pickupDataProvider,
private readonly LoggerInterface $logger,
private readonly string $xmlFilesPath private readonly string $xmlFilesPath
) { ) {
parent::__construct(); 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; 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;
}
}