@@ -97,23 +97,28 @@ class LoggingAssertionMixin:
97
97
def assertLogRecord (
98
98
self ,
99
99
logger_cm ,
100
- level ,
101
100
msg ,
101
+ levelno ,
102
102
status_code ,
103
+ request = None ,
103
104
exc_class = None ,
104
105
):
105
106
self .assertEqual (
106
107
records_len := len (logger_cm .records ),
107
108
1 ,
108
- f"Wrong number of calls for { logger_cm = } in { level = } (expected 1, got "
109
+ f"Wrong number of calls for { logger_cm = } in { levelno = } (expected 1, got "
109
110
f"{ records_len } )." ,
110
111
)
111
112
record = logger_cm .records [0 ]
112
113
self .assertEqual (record .getMessage (), msg )
114
+ self .assertEqual (record .levelno , levelno )
113
115
self .assertEqual (record .status_code , status_code )
116
+ if request is not None :
117
+ self .assertEqual (record .request , request )
114
118
if exc_class :
115
119
self .assertIsNotNone (record .exc_info )
116
120
self .assertEqual (record .exc_info [0 ], exc_class )
121
+ return record
117
122
118
123
def assertLogsRequest (
119
124
self , url , level , msg , status_code , logger = "django.request" , exc_class = None
@@ -123,7 +128,9 @@ def assertLogsRequest(
123
128
self .client .get (url )
124
129
except views .UncaughtException :
125
130
pass
126
- self .assertLogRecord (cm , level , msg , status_code , exc_class )
131
+ self .assertLogRecord (
132
+ cm , msg , getattr (logging , level ), status_code , exc_class = exc_class
133
+ )
127
134
128
135
129
136
@override_settings (DEBUG = True , ROOT_URLCONF = "logging_tests.urls" )
@@ -155,21 +162,17 @@ def test_control_chars_escaped(self):
155
162
)
156
163
157
164
async def test_async_page_not_found_warning (self ):
158
- logger = "django.request"
159
- level = "WARNING"
160
- with self .assertLogs (logger , level ) as cm :
165
+ with self .assertLogs ("django.request" , "WARNING" ) as cm :
161
166
await self .async_client .get ("/does_not_exist/" )
162
167
163
- self .assertLogRecord (cm , level , "Not Found: /does_not_exist/" , 404 )
168
+ self .assertLogRecord (cm , "Not Found: /does_not_exist/" , logging . WARNING , 404 )
164
169
165
170
async def test_async_control_chars_escaped (self ):
166
- logger = "django.request"
167
- level = "WARNING"
168
- with self .assertLogs (logger , level ) as cm :
171
+ with self .assertLogs ("django.request" , "WARNING" ) as cm :
169
172
await self .async_client .get (r"/%1B[1;31mNOW IN RED!!!1B[0m/" )
170
173
171
174
self .assertLogRecord (
172
- cm , level , r"Not Found: /\x1b[1;31mNOW IN RED!!!1B[0m/" , 404
175
+ cm , r"Not Found: /\x1b[1;31mNOW IN RED!!!1B[0m/" , logging . WARNING , 404
173
176
)
174
177
175
178
def test_page_not_found_raised (self ):
@@ -688,23 +691,9 @@ def patch_django_server_logger():
688
691
)
689
692
690
693
691
- class LogResponseRealLoggerTests (TestCase ):
694
+ class LogResponseRealLoggerTests (LoggingAssertionMixin , TestCase ):
692
695
request = RequestFactory ().get ("/test-path/" )
693
696
694
- def assertResponseLogged (self , logger_cm , msg , levelno , status_code , request ):
695
- self .assertEqual (
696
- records_len := len (logger_cm .records ),
697
- 1 ,
698
- f"Unexpected number of records for { logger_cm = } in { levelno = } (expected 1, "
699
- f"got { records_len } )." ,
700
- )
701
- record = logger_cm .records [0 ]
702
- self .assertEqual (record .getMessage (), msg )
703
- self .assertEqual (record .levelno , levelno )
704
- self .assertEqual (record .status_code , status_code )
705
- self .assertEqual (record .request , request )
706
- return record
707
-
708
697
def test_missing_response_raises_attribute_error (self ):
709
698
with self .assertRaises (AttributeError ):
710
699
log_response ("No response provided" , response = None , request = self .request )
@@ -713,29 +702,29 @@ def test_missing_request_logs_with_none(self):
713
702
response = HttpResponse (status = 403 )
714
703
with self .assertLogs ("django.request" , level = "INFO" ) as cm :
715
704
log_response (msg := "Missing request" , response = response , request = None )
716
- self .assertResponseLogged (cm , msg , logging .WARNING , 403 , request = None )
705
+ self .assertLogRecord (cm , msg , logging .WARNING , 403 , request = None )
717
706
718
707
def test_logs_5xx_as_error (self ):
719
708
response = HttpResponse (status = 508 )
720
709
with self .assertLogs ("django.request" , level = "ERROR" ) as cm :
721
710
log_response (
722
711
msg := "Server error occurred" , response = response , request = self .request
723
712
)
724
- self .assertResponseLogged (cm , msg , logging .ERROR , 508 , self .request )
713
+ self .assertLogRecord (cm , msg , logging .ERROR , 508 , self .request )
725
714
726
715
def test_logs_4xx_as_warning (self ):
727
716
response = HttpResponse (status = 418 )
728
717
with self .assertLogs ("django.request" , level = "WARNING" ) as cm :
729
718
log_response (
730
719
msg := "This is a teapot!" , response = response , request = self .request
731
720
)
732
- self .assertResponseLogged (cm , msg , logging .WARNING , 418 , self .request )
721
+ self .assertLogRecord (cm , msg , logging .WARNING , 418 , self .request )
733
722
734
723
def test_logs_2xx_as_info (self ):
735
724
response = HttpResponse (status = 201 )
736
725
with self .assertLogs ("django.request" , level = "INFO" ) as cm :
737
726
log_response (msg := "OK response" , response = response , request = self .request )
738
- self .assertResponseLogged (cm , msg , logging .INFO , 201 , self .request )
727
+ self .assertLogRecord (cm , msg , logging .INFO , 201 , self .request )
739
728
740
729
def test_custom_log_level (self ):
741
730
response = HttpResponse (status = 403 )
@@ -746,14 +735,14 @@ def test_custom_log_level(self):
746
735
request = self .request ,
747
736
level = "debug" ,
748
737
)
749
- self .assertResponseLogged (cm , msg , logging .DEBUG , 403 , self .request )
738
+ self .assertLogRecord (cm , msg , logging .DEBUG , 403 , self .request )
750
739
751
740
def test_logs_only_once_per_response (self ):
752
741
response = HttpResponse (status = 500 )
753
742
with self .assertLogs ("django.request" , level = "ERROR" ) as cm :
754
743
log_response ("First log" , response = response , request = self .request )
755
744
log_response ("Second log" , response = response , request = self .request )
756
- self .assertResponseLogged (cm , "First log" , logging .ERROR , 500 , self .request )
745
+ self .assertLogRecord (cm , "First log" , logging .ERROR , 500 , self .request )
757
746
758
747
def test_exc_info_output (self ):
759
748
response = HttpResponse (status = 500 )
@@ -767,9 +756,7 @@ def test_exc_info_output(self):
767
756
request = self .request ,
768
757
exception = exc ,
769
758
)
770
- self .assertResponseLogged (
771
- cm , "With exception" , logging .ERROR , 500 , self .request
772
- )
759
+ self .assertLogRecord (cm , "With exception" , logging .ERROR , 500 , self .request )
773
760
self .assertIn ("ValueError" , "\n " .join (cm .output )) # Stack trace included
774
761
775
762
def test_format_args_are_applied (self ):
@@ -783,7 +770,7 @@ def test_format_args_are_applied(self):
783
770
request = self .request ,
784
771
)
785
772
msg = "Something went wrong: DB error (42)"
786
- self .assertResponseLogged (cm , msg , logging .ERROR , 500 , self .request )
773
+ self .assertLogRecord (cm , msg , logging .ERROR , 500 , self .request )
787
774
788
775
def test_logs_with_custom_logger (self ):
789
776
handler = logging .StreamHandler (log_stream := StringIO ())
@@ -855,7 +842,7 @@ def test_unicode_escape_escaping(self):
855
842
response = HttpResponse (status = 318 )
856
843
log_response (msg , case , response = response , level = "error" )
857
844
858
- record = self .assertResponseLogged (
845
+ record = self .assertLogRecord (
859
846
cm ,
860
847
msg % expected ,
861
848
levelno = logging .ERROR ,
0 commit comments