@@ -161,12 +161,119 @@ def func(other_param: str, task_id: str):
161
161
logger .info ("processing" )
162
162
return other_param , task_id
163
163
164
- func ("something" , task_id = "abc123" )
164
+ func ("something" , "abc123" )
165
165
166
166
assert loguru_caplog .records [0 ].extra == {
167
167
LoggerContextKeys .task_id .value : "abc123"
168
168
}
169
169
170
+ def test_log_context_with_multiple_positional_captured_args (self , loguru_caplog ):
171
+ """Test that multiple captured args work with positional arguments"""
172
+
173
+ @log_context (
174
+ capture_args = {
175
+ "task_id" : LoggerContextKeys .task_id ,
176
+ "request_id" : LoggerContextKeys .privacy_request_id ,
177
+ }
178
+ )
179
+ def func (other_param : str , task_id : str , request_id : str ):
180
+ logger .info ("processing" )
181
+ return other_param , task_id , request_id
182
+
183
+ # Pass all arguments as positional arguments
184
+ func ("something" , "abc123" , "req456" )
185
+
186
+ assert loguru_caplog .records [0 ].extra == {
187
+ LoggerContextKeys .task_id .value : "abc123" ,
188
+ LoggerContextKeys .privacy_request_id .value : "req456" ,
189
+ }
190
+
191
+ def test_log_context_with_mixed_positional_and_keyword_only_args (
192
+ self , loguru_caplog
193
+ ):
194
+ """Test that captured args work with functions that have a mix of positional and keyword-only arguments"""
195
+
196
+ @log_context (
197
+ capture_args = {
198
+ "task_id" : LoggerContextKeys .task_id ,
199
+ "request_id" : LoggerContextKeys .privacy_request_id ,
200
+ }
201
+ )
202
+ def func (task_id : str , * , request_id : str ):
203
+ logger .info ("processing" )
204
+ return task_id , request_id
205
+
206
+ # Pass task_id as positional and request_id as keyword (required)
207
+ func ("abc123" , request_id = "req456" )
208
+
209
+ assert loguru_caplog .records [0 ].extra == {
210
+ LoggerContextKeys .task_id .value : "abc123" ,
211
+ LoggerContextKeys .privacy_request_id .value : "req456" ,
212
+ }
213
+
214
+ def test_log_context_with_keyword_only_args (self , loguru_caplog ):
215
+ """Test that captured args work with functions that have only keyword-only arguments"""
216
+
217
+ @log_context (
218
+ capture_args = {
219
+ "task_id" : LoggerContextKeys .task_id ,
220
+ "request_id" : LoggerContextKeys .privacy_request_id ,
221
+ }
222
+ )
223
+ def func (* , task_id : str , request_id : str ):
224
+ logger .info ("processing" )
225
+ return task_id , request_id
226
+
227
+ # All arguments must be passed as keywords
228
+ func (task_id = "abc123" , request_id = "req456" )
229
+
230
+ assert loguru_caplog .records [0 ].extra == {
231
+ LoggerContextKeys .task_id .value : "abc123" ,
232
+ LoggerContextKeys .privacy_request_id .value : "req456" ,
233
+ }
234
+
235
+ def test_log_context_with_default_parameters (self , loguru_caplog ):
236
+ """Test that captured args work with functions that have default parameters"""
237
+
238
+ @log_context (
239
+ capture_args = {
240
+ "task_id" : LoggerContextKeys .task_id ,
241
+ "request_id" : LoggerContextKeys .privacy_request_id ,
242
+ }
243
+ )
244
+ def func (task_id : str = "default_task" , request_id : str = "default_request" ):
245
+ logger .info ("processing" )
246
+ return task_id , request_id
247
+
248
+ # Call with no arguments - should use defaults
249
+ func ()
250
+
251
+ assert loguru_caplog .records [0 ].extra == {
252
+ LoggerContextKeys .task_id .value : "default_task" ,
253
+ LoggerContextKeys .privacy_request_id .value : "default_request" ,
254
+ }
255
+
256
+ def test_log_context_with_overridden_default_parameters (self , loguru_caplog ):
257
+ """Test that captured args work with functions where default parameters are overridden"""
258
+
259
+ @log_context (
260
+ capture_args = {
261
+ "task_id" : LoggerContextKeys .task_id ,
262
+ "request_id" : LoggerContextKeys .privacy_request_id ,
263
+ }
264
+ )
265
+ def func (task_id : str = "default_task" , request_id : str = "default_request" ):
266
+ logger .info ("processing" )
267
+ return task_id , request_id
268
+
269
+ # Override only one default parameter
270
+ func (task_id = "abc123" )
271
+
272
+ assert loguru_caplog .records [0 ].extra == {
273
+ LoggerContextKeys .task_id .value : "abc123" ,
274
+ LoggerContextKeys .privacy_request_id .value : "default_request" ,
275
+ }
276
+
170
277
171
278
class TestDetailFunctions :
172
279
@pytest .fixture
0 commit comments