From 234e0bfa89eddfc03a0641734c7dd29afa5a802f Mon Sep 17 00:00:00 2001 From: Martijn Harte Date: Thu, 12 Sep 2024 08:59:30 +0200 Subject: [PATCH] Log every time a session is created Prior to this change there we weren't able to keep track of sessions that got lost. This change allows us to see every time a session is created and distinguish them by their correlation id. --- src/Service/SessionCorrelationIdService.php | 44 +++++++++++++ src/Session/LoggingSessionFactory.php | 70 +++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/Service/SessionCorrelationIdService.php create mode 100644 src/Session/LoggingSessionFactory.php diff --git a/src/Service/SessionCorrelationIdService.php b/src/Service/SessionCorrelationIdService.php new file mode 100644 index 00000000..00f77c5c --- /dev/null +++ b/src/Service/SessionCorrelationIdService.php @@ -0,0 +1,44 @@ +requestStack->getMainRequest()?->cookies->get('PHPSESSID'); + + if ($sessionCookie === null) { + return null; + } + + return hash('sha256', self::SALT . substr($sessionCookie, 10)); + } +} diff --git a/src/Session/LoggingSessionFactory.php b/src/Session/LoggingSessionFactory.php new file mode 100644 index 00000000..8491197f --- /dev/null +++ b/src/Session/LoggingSessionFactory.php @@ -0,0 +1,70 @@ +logger = WithContextLogger::from( + $monologLogger, + ['correlationId' => $sessionCorrelationIdService->generateCorrelationId() ?? ''], + ); + + parent::__construct($requestStack, $storageFactory, $usageReporter); + } + + public function createSession(): SessionInterface + { + $this->logger->info('Created new session.'); + + return parent::createSession(); + } +}