From bb0d8a8a1f9af2f97b966a2c16a2ef4216be5ecd Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 9 Feb 2023 11:54:33 +0100
Subject: [PATCH] LoggingProcessor: add the symfony route to the context

With many bundles active it might not be clear which bundle
is the cause for the logging message. Add the current active
Symfony route to make it a bit more clear.
---
 CHANGELOG.md                           |  4 ++++
 src/Logging/LoggingProcessor.php       |  5 +++++
 tests/Logging/LoggingProcessorTest.php | 12 ++++++++++++
 3 files changed, 21 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index dc04658..01e3743 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# v0.1.75
+
+* The logging context now includes the active symfony route name
+
 # v0.1.59
 
 * api-docs: compatibility fixes for relay-auth-bundle v0.1.12
diff --git a/src/Logging/LoggingProcessor.php b/src/Logging/LoggingProcessor.php
index 0b6c309..a2268da 100644
--- a/src/Logging/LoggingProcessor.php
+++ b/src/Logging/LoggingProcessor.php
@@ -55,6 +55,11 @@ final class LoggingProcessor
                 $request->attributes->set($requestAttributeKey, $requestId);
             }
             $record['context']['relay-request-id'] = $requestId;
+
+            $route = $request->attributes->get('_route');
+            if ($route !== null) {
+                $record['context']['relay-route'] = $route;
+            }
         }
 
         return $record;
diff --git a/tests/Logging/LoggingProcessorTest.php b/tests/Logging/LoggingProcessorTest.php
index 7db5c12..ec0a6ae 100644
--- a/tests/Logging/LoggingProcessorTest.php
+++ b/tests/Logging/LoggingProcessorTest.php
@@ -42,6 +42,18 @@ class LoggingProcessorTest extends WebTestCase
         $this->assertSame(['message' => 'foobar', 'context' => ['relay-session-id' => 'logging-id']], $record);
     }
 
+    public function testRoute()
+    {
+        $stack = new RequestStack();
+        $request = new Request();
+        $request->attributes->set('_route', 'some_route');
+        $stack->push($request);
+        $processor = new LoggingProcessor(new TestUserSession('log'), $stack);
+        $record = ['message' => 'foobar'];
+        $record = $processor->__invoke($record);
+        $this->assertSame('some_route', $record['context']['relay-route']);
+    }
+
     public function testMaskUserId()
     {
         $processor = new LoggingProcessor(new TestUserSession('some-random-user-id'), new RequestStack());
-- 
GitLab