62
62
"exc_info" ,
63
63
"exc_text" ,
64
64
}
65
+ _SEMANTIC_CONVENTION_ATTRS = {
66
+ "pathname" ,
67
+ "funcName" ,
68
+ "lineno" ,
69
+ "thread" ,
70
+ "threadName" ,
71
+ }
65
72
66
73
67
74
class LogLimits :
@@ -416,26 +423,21 @@ class LoggingHandler(logging.Handler):
416
423
https://docs.python.org/3/library/logging.html
417
424
"""
418
425
419
- def __init__ (
420
- self ,
421
- level = logging .NOTSET ,
422
- logger_provider = None ,
423
- exclude_attributes = None ,
424
- ) -> None :
426
+ def __init__ (self , level = logging .NOTSET , logger_provider = None ) -> None :
425
427
super ().__init__ (level = level )
426
428
self ._logger_provider = logger_provider or get_logger_provider ()
427
429
self ._logger = get_logger (
428
430
__name__ , logger_provider = self ._logger_provider
429
431
)
430
- if exclude_attributes is None :
431
- exclude_attributes = set ()
432
- self . exclude_attributes = _PRIVATE_RECORD_ATTRS | exclude_attributes
432
+ self . _exclude_attributes = (
433
+ _PRIVATE_RECORD_ATTRS | _SEMANTIC_CONVENTION_ATTRS
434
+ )
433
435
434
436
def _get_attributes (self , record : logging .LogRecord ) -> Attributes :
435
437
attributes = {
436
438
k : v
437
439
for k , v in vars (record ).items ()
438
- if k not in self .exclude_attributes
440
+ if k not in self ._exclude_attributes
439
441
}
440
442
if record .exc_info :
441
443
exc_type = ""
@@ -454,6 +456,13 @@ def _get_attributes(self, record: logging.LogRecord) -> Attributes:
454
456
attributes [SpanAttributes .EXCEPTION_TYPE ] = exc_type
455
457
attributes [SpanAttributes .EXCEPTION_MESSAGE ] = message
456
458
attributes [SpanAttributes .EXCEPTION_STACKTRACE ] = stack_trace
459
+
460
+ # adding these attributes with their semantic convention names
461
+ attributes [SpanAttributes .CODE_FILEPATH ] = record .pathname
462
+ attributes [SpanAttributes .CODE_FUNCTION ] = record .funcName
463
+ attributes [SpanAttributes .CODE_LINENO ] = record .lineno
464
+ attributes [SpanAttributes .THREAD_ID ] = record .thread
465
+ attributes [SpanAttributes .THREAD_NAME ] = record .threadName
457
466
return attributes
458
467
459
468
def _translate (self , record : logging .LogRecord ) -> LogRecord :
0 commit comments