@@ -402,37 +402,6 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:
402402 return True
403403
404404
405- # skip natural LogRecord attributes
406- # http://docs.python.org/library/logging.html#logrecord-attributes
407- _RESERVED_ATTRS = frozenset (
408- (
409- "asctime" ,
410- "args" ,
411- "created" ,
412- "exc_info" ,
413- "exc_text" ,
414- "filename" ,
415- "funcName" ,
416- "message" ,
417- "levelname" ,
418- "levelno" ,
419- "lineno" ,
420- "module" ,
421- "msecs" ,
422- "msg" ,
423- "name" ,
424- "pathname" ,
425- "process" ,
426- "processName" ,
427- "relativeCreated" ,
428- "stack_info" ,
429- "thread" ,
430- "threadName" ,
431- "taskName" ,
432- )
433- )
434-
435-
436405class LoggingHandler (logging .Handler ):
437406 """A handler class which writes logging records, in OTLP format, to
438407 a network destination or file. Supports signals from the `logging` module.
@@ -452,8 +421,17 @@ def __init__(
452421
453422 @staticmethod
454423 def _get_attributes (record : logging .LogRecord ) -> Attributes :
424+ private_record_attrs = (
425+ "args" ,
426+ "msg" ,
427+ "stack_info" ,
428+ "exc_info" ,
429+ "exc_text" ,
430+ )
455431 attributes = {
456- k : v for k , v in vars (record ).items () if k not in _RESERVED_ATTRS
432+ k : v
433+ for k , v in vars (record ).items ()
434+ if k not in private_record_attrs
457435 }
458436 if record .exc_info :
459437 exc_type = ""
@@ -478,48 +456,9 @@ def _translate(self, record: logging.LogRecord) -> LogRecord:
478456 timestamp = int (record .created * 1e9 )
479457 span_context = get_current_span ().get_span_context ()
480458 attributes = self ._get_attributes (record )
481- # This comment is taken from GanyedeNil's PR #3343, I have redacted it
482- # slightly for clarity:
483- # According to the definition of the Body field type in the
484- # OTel 1.22.0 Logs Data Model article, the Body field should be of
485- # type 'any' and should not use the str method to directly translate
486- # the msg. This is because str only converts non-text types into a
487- # human-readable form, rather than a standard format, which leads to
488- # the need for additional operations when collected through a log
489- # collector.
490- # Considering that he Body field should be of type 'any' and should not
491- # use the str method but record.msg is also a string type, then the
492- # difference is just the self.args formatting?
493- # The primary consideration depends on the ultimate purpose of the log.
494- # Converting the default log directly into a string is acceptable as it
495- # will be required to be presented in a more readable format. However,
496- # this approach might not be as "standard" when hoping to aggregate
497- # logs and perform subsequent data analysis. In the context of log
498- # extraction, it would be more appropriate for the msg to be
499- # converted into JSON format or remain unchanged, as it will eventually
500- # be transformed into JSON. If the final output JSON data contains a
501- # structure that appears similar to JSON but is not, it may confuse
502- # users. This is particularly true for operation and maintenance
503- # personnel who need to deal with log data in various languages.
504- # Where is the JSON converting occur? and what about when the msg
505- # represents something else but JSON, the expected behavior change?
506- # For the ConsoleLogExporter, it performs the to_json operation in
507- # opentelemetry.sdk._logs._internal.export.ConsoleLogExporter.__init__,
508- # so it can handle any type of input without problems. As for the
509- # OTLPLogExporter, it also handles any type of input encoding in
510- # _encode_log located in
511- # opentelemetry.exporter.otlp.proto.common._internal._log_encoder.
512- # Therefore, no extra operation is needed to support this change.
513- # The only thing to consider is the users who have already been using
514- # this SDK. If they upgrade the SDK after this change, they will need
515- # to readjust their logging collection rules to adapt to the latest
516- # output format. Therefore, this change is considered a breaking
517- # change and needs to be upgraded at an appropriate time.
518459 severity_number = std_to_otel (record .levelno )
519- if isinstance (record .msg , str ) and record .args :
520- body = record .msg % record .args
521- else :
522- body = record .msg
460+ body = self .format (record )
461+
523462 return LogRecord (
524463 timestamp = timestamp ,
525464 trace_id = span_context .trace_id ,
0 commit comments