Python observability platform with structured logging, distributed tracing, metrics collection, and automatic instrumentation for popular frameworks and AI services.
—
Pydantic Logfire is a Python observability platform that provides comprehensive monitoring, logging, and tracing capabilities. Built on OpenTelemetry standards, Logfire offers powerful observability tools with a focus on Python-centric insights, simple integration, and SQL-based querying of telemetry data.
pip install logfirelogfire auth (CLI command)import logfireFor type annotations:
from logfire import Logfire, LogfireSpan, ConsoleOptions, LevelName, AutoTraceModuleimport logfire
from datetime import date
# Configure logfire (call once at application startup)
logfire.configure()
# Basic logging
logfire.info('Hello, {name}!', name='world')
logfire.error('Something went wrong: {error}', error='connection failed')
# Structured logging with attributes
logfire.info('User login', user_id=123, ip_address='192.168.1.1', success=True)
# Span creation for tracking operations
with logfire.span('Processing user {user_id}', user_id=123):
logfire.debug('Starting validation')
# ... processing logic
logfire.info('Processing complete')
# Function instrumentation
@logfire.instrument
def calculate_total(items):
return sum(item.price for item in items)Logfire is built on the OpenTelemetry standard and provides three core observability signals:
The architecture enables:
Essential logging functionality providing structured event recording with multiple severity levels, exception tracking, and rich context through attributes and tags.
def trace(msg_template, /, *, _tags=None, _exc_info=False, **attributes): ...
def debug(msg_template, /, *, _tags=None, _exc_info=False, **attributes): ...
def info(msg_template, /, *, _tags=None, _exc_info=False, **attributes): ...
def notice(msg_template, /, *, _tags=None, _exc_info=False, **attributes): ...
def warning(msg_template, /, *, _tags=None, _exc_info=False, **attributes): ...
def error(msg_template, /, *, _tags=None, _exc_info=False, **attributes): ...
def fatal(msg_template, /, *, _tags=None, _exc_info=False, **attributes): ...
def exception(msg_template, /, *, _tags=None, _exc_info=True, **attributes): ...
def log(level: LevelName | int, msg_template: str, attributes: dict[str, Any] | None = None,
tags: Sequence[str] | None = None, exc_info: bool = False, console_log: bool | None = None): ...
def configure(*, send_to_logfire: bool | Literal['if-token-present'] | None = None,
token: str | None = None, service_name: str | None = None,
console: ConsoleOptions | False | None = None, **kwargs) -> Logfire: ...Comprehensive automatic instrumentation for popular Python frameworks, libraries, and services, enabling zero-configuration observability for web applications, databases, AI services, and more.
# Web Frameworks
def instrument_fastapi(app, *, capture_headers: bool = False, **kwargs): ...
def instrument_django(*, capture_headers: bool = False, **kwargs): ...
def instrument_flask(app, *, capture_headers: bool = False, **kwargs): ...
def instrument_starlette(app, *, capture_headers: bool = False, **kwargs): ...
# AI/LLM Services
def instrument_openai(openai_client=None, *, suppress_other_instrumentation: bool = True): ...
def instrument_anthropic(anthropic_client=None, *, suppress_other_instrumentation: bool = True): ...
# Databases
def instrument_sqlalchemy(engine=None, *, enable_commenter: bool = False, **kwargs): ...
def instrument_asyncpg(**kwargs): ...
def instrument_redis(*, capture_statement: bool = False, **kwargs): ...
# HTTP Clients
def instrument_httpx(client=None, *, capture_headers: bool = False, **kwargs): ...
def instrument_requests(*, excluded_urls=None, **kwargs): ...Manual span creation and management for tracking operations, creating distributed traces, and organizing observability data hierarchically with full OpenTelemetry compatibility.
def span(msg_template: str, /, *, _tags: Sequence[str] | None = None,
_span_name: str | None = None, _level: LevelName | int | None = None,
_links: Sequence = (), **attributes) -> LogfireSpan: ...
def instrument(msg_template: str | None = None, *, span_name: str | None = None,
extract_args: bool = True, record_return: bool = False,
allow_generator: bool = False): ...
def install_auto_tracing(modules: Sequence[str] | Callable[[AutoTraceModule], bool], *,
min_duration: float,
check_imported_modules: Literal['error', 'warn', 'ignore'] = 'error'): ...
class LogfireSpan:
def set_attribute(self, key: str, value: Any) -> None: ...
def set_attributes(self, attributes: dict[str, Any]) -> None: ...
def record_exception(self, exception: BaseException, *, attributes: dict[str, Any] | None = None,
timestamp: int | None = None, escaped: bool = False) -> None: ...Metrics creation and management for quantitative monitoring including counters, histograms, gauges, and callback-based metrics with OpenTelemetry compatibility.
def metric_counter(name: str, *, unit: str = '', description: str = '') -> Counter: ...
def metric_histogram(name: str, *, unit: str = '', description: str = '') -> Histogram: ...
def metric_gauge(name: str, *, unit: str = '', description: str = '') -> Gauge: ...
def metric_up_down_counter(name: str, *, unit: str = '', description: str = '') -> UpDownCounter: ...
def metric_counter_callback(name: str, *, callbacks: Sequence[Callable],
unit: str = '', description: str = ''): ...
def metric_gauge_callback(name: str, callbacks: Sequence[Callable],
*, unit: str = '', description: str = ''): ...Integration components for connecting Logfire with standard logging frameworks, structured logging libraries, and external systems.
class LogfireLoggingHandler:
"""Handler for Python standard library logging integration."""
def __init__(self, logfire: Logfire | None = None): ...
class StructlogProcessor:
"""Processor for structlog integration (alias: LogfireProcessor)."""
def __init__(self, logfire: Logfire | None = None): ...
def loguru_handler() -> dict[str, Any]:
"""Create a Logfire handler for Loguru integration."""
def suppress_instrumentation() -> AbstractContextManager[None]: ...
def get_baggage() -> dict[str, str]: ...
def set_baggage(baggage: dict[str, str]) -> Token: ...
# Version information
__version__: str # Package version string (e.g., "4.3.6")Install with Tessl CLI
npx tessl i tessl/pypi-logfire