From 12203985306c646b197ffd2f0beb7e2e1166543a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Wed, 12 Feb 2025 16:53:02 +0100 Subject: [PATCH] feat: extended logging --- config/packages/monolog.yaml | 33 +++++++++++++------ config/services.yaml | 1 + src/BusProNet/ApiClient.php | 9 +++-- src/BusProNet/Security/Authenticator.php | 1 - src/Controller/Booking/DownloadController.php | 8 +++++ src/Controller/Booking/EditController.php | 25 +++++++++++--- src/Controller/PersonalDataController.php | 15 +++++++-- src/Controller/RegistrationController.php | 14 ++++++-- src/Controller/ResetPasswordController.php | 10 +++++- 9 files changed, 92 insertions(+), 24 deletions(-) diff --git a/config/packages/monolog.yaml b/config/packages/monolog.yaml index 6d4d1aa..dc77817 100644 --- a/config/packages/monolog.yaml +++ b/config/packages/monolog.yaml @@ -2,20 +2,26 @@ monolog: channels: - deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists - bpn + - core when@dev: monolog: handlers: - main: - type: stream - path: "%kernel.logs_dir%/%kernel.environment%.log" - level: debug - channels: ["!event"] bpn: type: stream path: "%kernel.logs_dir%/%kernel.environment%.bpn.log" level: debug channels: ["bpn"] + core: + type: stream + path: "%kernel.logs_dir%/%kernel.environment%.core.log" + level: debug + channels: ["core"] + main: + type: stream + path: "%kernel.logs_dir%/%kernel.environment%.log" + level: debug + channels: ["!event"] # uncomment to get logging in your browser # you may have to allow bigger header sizes in your Web server configuration #firephp: @@ -46,17 +52,24 @@ when@test: when@prod: monolog: handlers: + bpn: + type: rotating_file + max_files: 7 + path: "%kernel.logs_dir%/%kernel.environment%.bpn.log" + level: info + channels: ["bpn"] + core: + type: rotating_file + max_files: 7 + path: "%kernel.logs_dir%/%kernel.environment%.core.log" + level: info + channels: ["core"] main: type: fingers_crossed action_level: error handler: nested excluded_http_codes: [404, 405] buffer_size: 50 # How many messages should be saved? Prevent memory leaks - bpn: - type: stream - path: "%kernel.logs_dir%/%kernel.environment%.bpn.log" - level: debug - channels: ["bpn"] nested: type: stream path: php://stderr diff --git a/config/services.yaml b/config/services.yaml index 64b6ce1..936e1c1 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -12,6 +12,7 @@ services: autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. bind: $xmlPath: '%kernel.project_dir%/var/xmlexport' + $logger: '@monolog.logger.core' # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name diff --git a/src/BusProNet/ApiClient.php b/src/BusProNet/ApiClient.php index abda90e..ec23347 100644 --- a/src/BusProNet/ApiClient.php +++ b/src/BusProNet/ApiClient.php @@ -34,9 +34,9 @@ class ApiClient public function __construct( private readonly SerializerInterface $serializer, - private readonly ApiResponseParser $responseParser, - private readonly LoggerInterface $logger, - array $options + private readonly ApiResponseParser $responseParser, + private readonly LoggerInterface $logger, + array $options ) { $this->config = $this->resolveOptions($options); } @@ -296,6 +296,9 @@ class ApiClient ]) ; + $this->logger->info('Sending request to BPN API', [ + 'requestId' => $requestId, + ]); if (true === $this->config['debug']) { $this->dumpXmlToFile('request', $requestId, $body); diff --git a/src/BusProNet/Security/Authenticator.php b/src/BusProNet/Security/Authenticator.php index 2f94784..fdcd432 100644 --- a/src/BusProNet/Security/Authenticator.php +++ b/src/BusProNet/Security/Authenticator.php @@ -5,7 +5,6 @@ namespace App\BusProNet\Security; use App\BusProNet\ApiClient; use App\BusProNet\Exception\ApiClientException; use App\BusProNet\Model\CrmAttributes; -use App\BusProNet\Model\CrmAttributesResponse; use App\BusProNet\Model\PersonalData; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; diff --git a/src/Controller/Booking/DownloadController.php b/src/Controller/Booking/DownloadController.php index caa40b6..883a193 100644 --- a/src/Controller/Booking/DownloadController.php +++ b/src/Controller/Booking/DownloadController.php @@ -4,6 +4,7 @@ namespace App\Controller\Booking; use App\BusProNet\ApiClient; use App\BusProNet\Model\Notification; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpFoundation\Request; @@ -19,6 +20,7 @@ class DownloadController extends AbstractController public function __construct( private readonly ApiClient $apiClient, private readonly Security $security, + private readonly LoggerInterface $logger, ) { } @@ -48,6 +50,12 @@ class DownloadController extends AbstractController 'confirmation' => 'Vorgangdruck', }; + $this->logger->info('Initiated document download', [ + 'email' => $bpnUser->getEmail(), + 'document_type' => $fileType, + 'booking_id' => $id, + ]); + $file = $this ->apiClient ->getDocuments($bpnUser->getEmail(), $bpnUser->getPassword(), $id, $type) diff --git a/src/Controller/Booking/EditController.php b/src/Controller/Booking/EditController.php index 4566348..e5ccaf4 100644 --- a/src/Controller/Booking/EditController.php +++ b/src/Controller/Booking/EditController.php @@ -8,6 +8,7 @@ use App\BusProNet\XmlLoader\TravelLoader; use App\Form\BookingType; use App\Form\Model\BookingData; use Psr\Cache\InvalidArgumentException; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpFoundation\Request; @@ -20,11 +21,12 @@ use Symfony\Contracts\Cache\ItemInterface; class EditController extends AbstractController { public function __construct( - private readonly ApiClient $apiClient, - private readonly TravelLoader $travelDataLoader, - private readonly PickupLoader $pickupDataLoader, + private readonly ApiClient $apiClient, + private readonly TravelLoader $travelDataLoader, + private readonly PickupLoader $pickupDataLoader, private readonly CacheInterface $cache, - private readonly Security $security, + private readonly Security $security, + private readonly LoggerInterface $logger, ) { } @@ -87,9 +89,17 @@ class EditController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $response = $this->apiClient->updateBooking($formData); - + $this->logger->info('Initiated booking update', [ + 'email' => $bpnUser->getEmail(), + 'booking_id' => $id, + ]); if (false === $response->isSuccessful()) { $this->addFlash('error', $response->message); + $this->logger->error('Error initiating booking update', [ + 'email' => $bpnUser->getEmail(), + 'booking_id' => $id, + 'message' => $response->message, + ]); } else { try { $this->cache->delete($cacheKey); @@ -98,6 +108,11 @@ class EditController extends AbstractController $this->addFlash('success', 'Buchung erfolgreich aktualisiert'); + $this->logger->info('Booking update successful', [ + 'email' => $bpnUser->getEmail(), + 'booking_id' => $id, + ]); + return $this->redirectToRoute('app_booking_edit', ['id' => $id]); } } diff --git a/src/Controller/PersonalDataController.php b/src/Controller/PersonalDataController.php index e7f0506..dfbfdb4 100644 --- a/src/Controller/PersonalDataController.php +++ b/src/Controller/PersonalDataController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\BusProNet\ApiClient; use App\BusProNet\Exception\ApiClientException; use App\Form\PersonalDataType; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpFoundation\Request; @@ -14,8 +15,12 @@ use Symfony\Component\Security\Http\Attribute\IsGranted; class PersonalDataController extends AbstractController { - public function __construct(private readonly ApiClient $apiClient, private readonly Security $security) - {} + public function __construct( + private readonly ApiClient $apiClient, + private readonly Security $security, + private readonly LoggerInterface $logger, + ) { + } #[Route('/personal-data', name: 'app_personal_data')] #[IsGranted('ROLE_USER')] @@ -41,6 +46,9 @@ class PersonalDataController extends AbstractController try { $this->apiClient->updatePersonalData($bpnUser->getEmail(), $bpnUser->getPassword(), $personalData); $this->addFlash('success', 'Deine persönlichen Daten wurden aktualisiert'); + $this->logger->info('Updated personal data', [ + 'email' => $bpnUser->getEmail(), + ]); } catch (ApiClientException $e) { $this->addFlash('error', $e->getMessage()); } @@ -74,6 +82,9 @@ class PersonalDataController extends AbstractController try { $this->apiClient->updateNewsletterRegistration($bpnUser->getEmail(), $bpnUser->getPassword(), $personalData); $this->addFlash('success', 'Deine Anmeldung zum Newsletter wurde aktualisiert'); + $this->logger->info('Updated newsletter registration', [ + 'email' => $bpnUser->getEmail(), + ]); } catch (ApiClientException $e) { $this->addFlash('error', $e->getMessage()); } diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index 8110e87..8cd64a5 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -6,6 +6,7 @@ use App\BusProNet\ApiClient; use App\BusProNet\Exception\ApiClientException; use App\Form\Model\RegistrationData; use App\Form\RegistrationType; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -14,8 +15,10 @@ use Symfony\Component\Security\Http\Attribute\IsGranted; class RegistrationController extends AbstractController { - public function __construct(private readonly ApiClient $apiClient) - { + public function __construct( + private readonly ApiClient $apiClient, + private readonly LoggerInterface $logger, + ) { } #[Route('/registration', name: 'app_registration')] @@ -36,8 +39,15 @@ class RegistrationController extends AbstractController } else { $this->addFlash('error', 'Möglicherweise bist du bereits registriert. Bitte setze dein Passwort zurück.'); } + $this->logger->info('Initiated registration', [ + 'email' => $registrationData->email, + ]); } catch (ApiClientException $e) { $this->addFlash('error', $e->getMessage()); + $this->logger->error('Error initiating registration', [ + 'email' => $registrationData->email, + 'error' => $e->getMessage(), + ]); } return $this->redirectToRoute('app_login'); diff --git a/src/Controller/ResetPasswordController.php b/src/Controller/ResetPasswordController.php index 034cc22..2bcb447 100644 --- a/src/Controller/ResetPasswordController.php +++ b/src/Controller/ResetPasswordController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\BusProNet\ApiClient; use App\BusProNet\Exception\ApiClientException; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\HttpFoundation\Request; @@ -15,7 +16,7 @@ use Symfony\Component\Validator\Constraints\NotBlank; class ResetPasswordController extends AbstractController { - public function __construct(private readonly ApiClient $apiClient) + public function __construct(private readonly ApiClient $apiClient, private readonly LoggerInterface $logger) { } @@ -47,8 +48,15 @@ class ResetPasswordController extends AbstractController try { $this->apiClient->resetPassword($form->get('email')->getData()); $this->addFlash('success', 'Du erhältst in Kürze eine E-Mail mit einem Link zum (Zurück)setzen deines Passworts.'); + $this->logger->info('Initiated password reset', [ + 'email' => $form->get('email')->getData(), + ]); } catch (ApiClientException $e) { $this->addFlash('error', $e->getMessage()); + $this->logger->error('Error initiating password reset', [ + 'email' => $form->get('email')->getData(), + 'error' => $e->getMessage(), + ]); } return $this->redirectToRoute('app_login');