@@ -54,11 +54,15 @@ def test_process_exception_logs_with_authenticated_user(
5454
5555 middleware .process_exception (request , exception )
5656
57- expected_context = (
58- "URL: /test/, Method: GET, User: testuser, Exception: Something went wrong"
59- )
6057 mock_logger .exception .assert_called_once_with (
61- expected_context , correlation_id = "corr-id-123"
58+ "url=/test/, method=GET, user=testuser, correlation_id=corr-id-123, exception_type=Exception Something went wrong" ,
59+ extra = {
60+ "url" : "/test/" ,
61+ "method" : "GET" ,
62+ "user" : "testuser" ,
63+ "correlation_id" : "corr-id-123" ,
64+ "exception_type" : "Exception" ,
65+ },
6266 )
6367
6468 def test_process_exception_logs_with_anonymous_user (self , monkeypatch , middleware ):
@@ -74,11 +78,15 @@ def test_process_exception_logs_with_anonymous_user(self, monkeypatch, middlewar
7478
7579 middleware .process_exception (request , exception )
7680
77- expected_context = (
78- "URL: /test/, Method: GET, User: Anonymous, Exception: Details of exception"
79- )
8081 mock_logger .exception .assert_called_once_with (
81- expected_context , correlation_id = None
82+ "url=/test/, method=GET, user=Anonymous, correlation_id=None, exception_type=Exception Details of exception" ,
83+ extra = {
84+ "url" : "/test/" ,
85+ "method" : "GET" ,
86+ "user" : "Anonymous" ,
87+ "correlation_id" : None ,
88+ "exception_type" : "Exception" ,
89+ },
8290 )
8391
8492 def test_process_exception_logs_with_no_user (self , monkeypatch , middleware ):
@@ -93,9 +101,15 @@ def test_process_exception_logs_with_no_user(self, monkeypatch, middleware):
93101
94102 middleware .process_exception (request , exception )
95103
96- expected_context = "URL: /test/, Method: GET, User: Anonymous, Exception: Fail"
97104 mock_logger .exception .assert_called_once_with (
98- expected_context , correlation_id = "abc"
105+ "url=/test/, method=GET, user=Anonymous, correlation_id=abc, exception_type=Exception Fail" ,
106+ extra = {
107+ "url" : "/test/" ,
108+ "method" : "GET" ,
109+ "user" : "Anonymous" ,
110+ "correlation_id" : "abc" ,
111+ "exception_type" : "Exception" ,
112+ },
99113 )
100114
101115 def test_process_exception_returns_none (self , middleware ):
@@ -142,11 +156,15 @@ def test_process_exception_logs_correct_path_and_method(
142156
143157 middleware .process_exception (request , exception )
144158
145- expected_context = (
146- f"URL: { path } , Method: { method } , User: testuser, Exception: err"
147- )
148159 mock_logger .exception .assert_called_once_with (
149- expected_context , correlation_id = "x"
160+ f"url={ path } , method={ method } , user=testuser, correlation_id=x, exception_type=Exception err" ,
161+ extra = {
162+ "url" : path ,
163+ "method" : method ,
164+ "user" : "testuser" ,
165+ "correlation_id" : "x" ,
166+ "exception_type" : "Exception" ,
167+ },
150168 )
151169
152170
@@ -298,15 +316,6 @@ def test_correlation_id_filter_uses_correlation_id_from_context(self, filter):
298316 assert filter .filter (record )
299317 assert record .correlation_id == test_correlation_id
300318
301- def test_correlation_id_filter_preserves_existing_correlation_id (self , filter ):
302- correlation_id_ctx .set ("context-correlation-id-123" )
303-
304- record = self ._make_record ()
305- record .correlation_id = "record-correlation-id-123"
306-
307- assert filter .filter (record )
308- assert record .correlation_id == "record-correlation-id-123"
309-
310319 def test_correlation_id_filter_uses_default_when_not_set (self , filter ):
311320 correlation_id_ctx .set (None )
312321
0 commit comments