Is your feature request related to a problem?
Many 3rd party libraries (unfortunately) use a logging.Formatter to mutate the log record, for example:
As a result the LoggingHandler fails to capture valuable attributes as often as it is used in tandem with a Formatter that mutates log records as described above.
Describe the solution you'd like
I think opentelemetry.sdk._logs._internal.LoggingHandler._translate should extract the attributes from the log record (using self._get_attributes(record)) after the formatted body has been calculated using self.format(record).
def _translate(self, record: logging.LogRecord) -> LogRecord:
timestamp = int(record.created * 1e9)
observered_timestamp = time_ns()
span_context = get_current_span().get_span_context()
# Instead of extracting attributes from 'record' here
# attributes = self._get_attributes(record)
severity_number = std_to_otel(record.levelno)
if self.formatter:
body = self.format(record)
else:
# ...
# defer until the formatter has had a chance to mutate
attributes = self._get_attributes(record)
Describe alternatives you've considered
I haven't thought of any alternatives, per se, but it's worth acknowledging that I don't think log formatters should be in the business of mutating the log record; python provides the "log factory" construct for this. Admittedly I'm asking the opentelemetry-python library to meet the Python ecosystem where it is.
Additional Context
No response
Would you like to implement a fix?
Yes
Is your feature request related to a problem?
Many 3rd party libraries (unfortunately) use a
logging.Formatterto mutate the logrecord, for example:task_id&task_nameto therecord.__dict__in theTaskFormatter.format()methodclient_addr,request_lineandstatus_codeto the__dict__of therecord(via a shallow copy)As a result the
LoggingHandlerfails to capture valuable attributes as often as it is used in tandem with aFormatterthat mutates log records as described above.Describe the solution you'd like
I think
opentelemetry.sdk._logs._internal.LoggingHandler._translateshould extract the attributes from the logrecord(usingself._get_attributes(record)) after the formattedbodyhas been calculated usingself.format(record).Describe alternatives you've considered
I haven't thought of any alternatives, per se, but it's worth acknowledging that I don't think log formatters should be in the business of mutating the log record; python provides the "log factory" construct for this. Admittedly I'm asking the opentelemetry-python library to meet the Python ecosystem where it is.
Additional Context
No response
Would you like to implement a fix?
Yes