跳转至

Logging

Integration with the standard library logging module.

LogfireLoggingHandler

LogfireLoggingHandler(
    level: int | str = NOTSET,
    fallback: Handler = StreamHandler(),
)

Bases: Handler

A logging handler that sends logs to Logfire.

Source code in logfire/integrations/logging.py
54
55
56
def __init__(self, level: int | str = NOTSET, fallback: LoggingHandler = StreamHandler()) -> None:
    super().__init__(level=level)
    self.fallback = fallback

emit

emit(record: LogRecord) -> None

Send the log to Logfire.

Parameters:

Name Type Description Default

record

LogRecord

The log record to send.

required
Source code in logfire/integrations/logging.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def emit(self, record: LogRecord) -> None:
    """Send the log to Logfire.

    Args:
        record: The log record to send.
    """
    if is_instrumentation_suppressed():
        self.fallback.handle(record)
        return

    attributes = self.fill_attributes(record)

    logfire.log(
        msg_template=attributes.pop(ATTRIBUTES_MESSAGE_TEMPLATE_KEY, record.msg),
        level=LOGGING_TO_OTEL_LEVEL_NUMBERS.get(record.levelno, record.levelno),
        attributes=attributes,
        custom_scope_suffix=self.custom_scope_suffix,
        exc_info=record.exc_info,
    )

fill_attributes

fill_attributes(record: LogRecord) -> dict[str, Any]

Fill the attributes to send to Logfire.

This method can be overridden to add more attributes.

Parameters:

Name Type Description Default

record

LogRecord

The log record.

required

Returns:

Type Description
dict[str, Any]

The attributes for the log record.

Source code in logfire/integrations/logging.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def fill_attributes(self, record: LogRecord) -> dict[str, Any]:
    """Fill the attributes to send to Logfire.

    This method can be overridden to add more attributes.

    Args:
        record: The log record.

    Returns:
        The attributes for the log record.
    """
    attributes = {k: v for k, v in record.__dict__.items() if k not in RESERVED_ATTRS}
    attributes['code.filepath'] = record.pathname
    attributes['code.lineno'] = record.lineno
    attributes['code.function'] = record.funcName

    attributes[ATTRIBUTES_MESSAGE_KEY], args = _format_message(record)
    attributes.update(args)

    return attributes

本文总阅读量