@@ -356,32 +356,43 @@ def _capture_log_from_record(self, client, record):
356356 record .levelno , SEVERITY_TO_OTEL_SEVERITY
357357 )
358358 project_root = client .options ["project_root" ]
359+
359360 attrs = self ._extra_from_record (record ) # type: Any
360361 attrs ["sentry.origin" ] = "auto.logger.log"
361- if isinstance ( record . msg , str ):
362- attrs [ "sentry.message.template" ] = record . msg
362+
363+ parameters_set = False
363364 if record .args is not None :
364365 if isinstance (record .args , tuple ):
366+ parameters_set = bool (record .args )
365367 for i , arg in enumerate (record .args ):
366368 attrs [f"sentry.message.parameter.{ i } " ] = (
367369 arg
368370 if isinstance (arg , (str , float , int , bool ))
369371 else safe_repr (arg )
370372 )
371373 elif isinstance (record .args , dict ):
374+ parameters_set = bool (record .args )
372375 for key , value in record .args .items ():
373376 attrs [f"sentry.message.parameter.{ key } " ] = (
374377 value
375378 if isinstance (value , (str , float , int , bool ))
376379 else safe_repr (value )
377380 )
381+
382+ if parameters_set and isinstance (record .msg , str ):
383+ # only include template if there is at least one
384+ # sentry.message.parameter.X set
385+ attrs ["sentry.message.template" ] = record .msg
386+
378387 if record .lineno :
379388 attrs ["code.line.number" ] = record .lineno
389+
380390 if record .pathname :
381391 if project_root is not None and record .pathname .startswith (project_root ):
382392 attrs ["code.file.path" ] = record .pathname [len (project_root ) + 1 :]
383393 else :
384394 attrs ["code.file.path" ] = record .pathname
395+
385396 if record .funcName :
386397 attrs ["code.function.name" ] = record .funcName
387398
0 commit comments