Skip to content

Let the logfire-api shim's StructlogProcessor, LogfireLoggingHandler and loguru_handler be used without logfire - #2786

Merged
alexmojaki merged 1 commit into
pydantic:mainfrom
Ishkirat-Singh:fix/logfire-api-logging-shims
Nov 12, 2026
Merged

alexmojaki merged 1 commit into
pydantic:mainfrom
Ishkirat-Singh:fix/logfire-api-logging-shims

Conversation

@Ishkirat-Singh

@Ishkirat-Singh Ishkirat-Singh commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

A library that depends only on logfire-api and wires up logging the way the integration docs show breaks when logfire isn't installed. The three logging integrations in the shim are bare classes/functions that don't satisfy the interface the logging library expects:

import sys; sys.modules['logfire'] = None  # simulate logfire not installed
import logging, structlog
from loguru import logger
import logfire_api as logfire

structlog.configure(processors=[logfire.StructlogProcessor(), structlog.dev.ConsoleRenderer()])
structlog.get_logger().info('hi')
# TypeError: 'StructlogProcessor' object is not callable

logging.basicConfig(handlers=[logfire.LogfireLoggingHandler()], force=True)
# AttributeError: 'LogfireLoggingHandler' object has no attribute 'formatter'

logger.configure(handlers=[logfire.loguru_handler()])
# TypeError: Logger.add() missing 1 required positional argument: 'sink'

With logfire installed all three work, so the shim is the only place this fails, which is exactly the case it exists for.

Changes in logfire-api/logfire_api/__init__.py:

  • StructlogProcessor.__call__(logger, name, event_dict) returns event_dict unchanged, like the real processor does after logging.
  • LogfireLoggingHandler subclasses logging.NullHandler and accepts the real handler's level argument (other arguments are still ignored), so it can be added to loggers and passed to basicConfig/dictConfig and simply drops records.
  • loguru_handler() returns a sink that discards messages plus the same 'format': '{message}' the real one returns, so logger.add(**loguru_handler()) and logger.configure(handlers=[...]) work.

test_runtime in tests/test_logfire_api.py now actually uses each of these (adds the handler to a stdlib logger and logs, adds the Loguru handler and logs, calls the structlog processor) instead of only constructing them, for both the shim and the real module.

Testing:

  • uv run pytest tests/test_logfire_api.py: on main the new assertions fail for without_logfire (isinstance(LogfireLoggingHandler(), logging.Handler) is False); with this change: 3 passed, 1 skipped.
  • ruff format --check, ruff check and pyright on both touched files: clean.

View guided diff

…and loguru_handler be used without logfire

Without logfire installed, the shim's StructlogProcessor wasn't callable,
LogfireLoggingHandler wasn't a logging.Handler, and loguru_handler()
returned an empty dict with no sink, so the documented setups for
structlog, stdlib logging and Loguru raised as soon as they were
configured or used.
@veria-ai

veria-ai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

View guided diff | Re-trigger cubic

@alexmojaki
alexmojaki enabled auto-merge November 12, 2026 10:15
@alexmojaki
alexmojaki added this pull request to the merge queue Nov 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-api/logfire_api
  __init__.py
Project Total  

This report was generated by python-coverage-comment-action

Merged via the queue into pydantic:main with commit eb1aebc Nov 12, 2026
53 of 55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants