diff --git a/logfire-api/logfire_api/__init__.py b/logfire-api/logfire_api/__init__.py index 5e7919d18..87f710d66 100644 --- a/logfire-api/logfire_api/__init__.py +++ b/logfire-api/logfire_api/__init__.py @@ -1,10 +1,9 @@ from __future__ import annotations -from contextlib import contextmanager import importlib import sys -from typing import TYPE_CHECKING, ContextManager, Literal -from contextlib import nullcontext +from contextlib import contextmanager, nullcontext +from typing import Any, ContextManager, Literal, TYPE_CHECKING from unittest.mock import MagicMock try: @@ -166,6 +165,8 @@ def shutdown(self, *args, **kwargs) -> None: ... instrument_system_metrics = DEFAULT_LOGFIRE_INSTANCE.instrument_system_metrics shutdown = DEFAULT_LOGFIRE_INSTANCE.shutdown + def loguru_handler() -> dict[str, Any]: ... + def no_auto_trace(x): return x diff --git a/logfire-api/logfire_api/__init__.pyi b/logfire-api/logfire_api/__init__.pyi index d7ffe5c1f..32403b2a3 100644 --- a/logfire-api/logfire_api/__init__.pyi +++ b/logfire-api/logfire_api/__init__.pyi @@ -10,8 +10,9 @@ from .integrations.logging import LogfireLoggingHandler as LogfireLoggingHandler from .integrations.structlog import LogfireProcessor as StructlogProcessor from .version import VERSION as VERSION from logfire.sampling import SamplingOptions as SamplingOptions +from typing import Any -__all__ = ['Logfire', 'LogfireSpan', 'LevelName', 'AdvancedOptions', 'ConsoleOptions', 'CodeSource', 'PydanticPlugin', 'configure', 'span', 'instrument', 'log', 'trace', 'debug', 'notice', 'info', 'warn', 'error', 'exception', 'fatal', 'force_flush', 'log_slow_async_callbacks', 'install_auto_tracing', 'instrument_pydantic', 'instrument_fastapi', 'instrument_openai', 'instrument_anthropic', 'instrument_asyncpg', 'instrument_httpx', 'instrument_celery', 'instrument_requests', 'instrument_psycopg', 'instrument_django', 'instrument_flask', 'instrument_starlette', 'instrument_aiohttp_client', 'instrument_sqlalchemy', 'instrument_redis', 'instrument_pymongo', 'instrument_mysql', 'instrument_system_metrics', 'AutoTraceModule', 'with_tags', 'with_settings', 'shutdown', 'load_spans_from_file', 'no_auto_trace', 'ScrubMatch', 'ScrubbingOptions', 'VERSION', 'suppress_instrumentation', 'StructlogProcessor', 'LogfireLoggingHandler', 'SamplingOptions', 'MetricsOptions'] +__all__ = ['Logfire', 'LogfireSpan', 'LevelName', 'AdvancedOptions', 'ConsoleOptions', 'CodeSource', 'PydanticPlugin', 'configure', 'span', 'instrument', 'log', 'trace', 'debug', 'notice', 'info', 'warn', 'error', 'exception', 'fatal', 'force_flush', 'log_slow_async_callbacks', 'install_auto_tracing', 'instrument_pydantic', 'instrument_fastapi', 'instrument_openai', 'instrument_anthropic', 'instrument_asyncpg', 'instrument_httpx', 'instrument_celery', 'instrument_requests', 'instrument_psycopg', 'instrument_django', 'instrument_flask', 'instrument_starlette', 'instrument_aiohttp_client', 'instrument_sqlalchemy', 'instrument_redis', 'instrument_pymongo', 'instrument_mysql', 'instrument_system_metrics', 'AutoTraceModule', 'with_tags', 'with_settings', 'shutdown', 'load_spans_from_file', 'no_auto_trace', 'ScrubMatch', 'ScrubbingOptions', 'VERSION', 'suppress_instrumentation', 'StructlogProcessor', 'LogfireLoggingHandler', 'loguru_handler', 'SamplingOptions', 'MetricsOptions'] DEFAULT_LOGFIRE_INSTANCE = Logfire() span = DEFAULT_LOGFIRE_INSTANCE.span @@ -49,4 +50,11 @@ warn = DEFAULT_LOGFIRE_INSTANCE.warn error = DEFAULT_LOGFIRE_INSTANCE.error fatal = DEFAULT_LOGFIRE_INSTANCE.fatal exception = DEFAULT_LOGFIRE_INSTANCE.exception + +def loguru_handler() -> dict[str, Any]: + """Create a **Logfire** handler for Loguru. + + Returns: + A dictionary with the handler and format for Loguru. + """ __version__ = VERSION diff --git a/logfire-api/logfire_api/_internal/config.pyi b/logfire-api/logfire_api/_internal/config.pyi index 41b3cf693..da6253be0 100644 --- a/logfire-api/logfire_api/_internal/config.pyi +++ b/logfire-api/logfire_api/_internal/config.pyi @@ -77,7 +77,11 @@ class MetricsOptions: @dataclass class CodeSource: - """Settings for the source code of the project.""" + """Settings for the source code of the project. + + !!! Warning + This setting is experimental, and may change in the future! + """ repository: str revision: str root_path: str @@ -111,6 +115,8 @@ def configure(*, send_to_logfire: bool | Literal['if-token-present'] | None = No Defaults to `True` if and only if the Python version is at least 3.11. sampling: Sampling options. See the [sampling guide](https://logfire.pydantic.dev/docs/guides/advanced/sampling/). code_source: Settings for the source code of the project. + !!! Warning + This setting is experimental, and may change in the future! advanced: Advanced options primarily used for testing by Logfire developers. """ diff --git a/tests/test_logfire_api.py b/tests/test_logfire_api.py index d653bb676..94e4882b6 100644 --- a/tests/test_logfire_api.py +++ b/tests/test_logfire_api.py @@ -152,6 +152,10 @@ def func() -> None: ... logfire_api.LogfireLoggingHandler() logfire__all__.remove('LogfireLoggingHandler') + assert hasattr(logfire_api, 'loguru_handler') + logfire_api.loguru_handler() + logfire__all__.remove('loguru_handler') + assert hasattr(logfire_api, 'StructlogProcessor') logfire_api.StructlogProcessor() logfire__all__.remove('StructlogProcessor')