Logfire API Shim

4.31.0 · active · verified Wed Apr 08

The `logfire-api` library is a lightweight shim for the Logfire SDK, designed for packages that want to offer opt-in integration with Logfire without a hard dependency. It provides a clone of the `logfire` package's Python API that performs no-op operations if `logfire` is not installed, but makes real calls when `logfire` is present. This allows users of an integrated package to decide whether to install and configure Logfire for observability. The current version is 4.31.0, with frequent releases aligning with the main Logfire SDK.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `logfire_api` as a shim. When `logfire` (the full SDK) is installed and configured by the end-user, `logfire_api` calls will be delegated to the actual Logfire SDK. If `logfire` is not installed, `logfire_api` methods will gracefully act as no-ops. Library authors typically `import logfire_api as logfire` and use its API, leaving the `logfire.configure()` call to their users. To send data to the Logfire platform, the `LOGFIRE_TOKEN` environment variable must be set.

import os
import logfire_api as logfire

# Simulate Logfire being installed (or not) for demonstration
# In a real scenario, `logfire` would either be in your environment or not.
# For this example, we'll configure a mock for `logfire.configure` if `logfire` is not installed.
# In a production environment, users would `pip install logfire` and configure it.

try:
    import logfire as _actual_logfire
    logfire_installed = True
except ImportError:
    logfire_installed = False

if not logfire_installed:
    print("Logfire (the full SDK) is NOT installed. `logfire_api` will act as a no-op.")
else:
    print("Logfire (the full SDK) IS installed. `logfire_api` will delegate to it.")

# Configure Logfire (this would typically be done by the end-user of a library using logfire-api)
# For logfire-api, library authors usually *don't* call configure().
# The actual `logfire.configure()` would typically read LOGFIRE_TOKEN or other env vars.
if logfire_installed:
    # Only configure if the actual logfire SDK is present, otherwise it's a no-op by design
    os.environ['LOGFIRE_TOKEN'] = os.environ.get('LOGFIRE_TOKEN', 'your-logfire-write-token') # Replace with a real token in production
    logfire.configure(service_name='my-shimmed-app')
    print("Logfire (actual SDK) configured.")
else:
    print("Skipping logfire.configure() as the actual SDK is not installed.")

@logfire.instrument("my_function")
def my_function(name: str):
    logfire.info("Hello from {name}!", name=name)
    with logfire.span("inner_operation"):
        logfire.debug("Performing an inner operation.")
    return f"Processed {name}"

result = my_function("World")
print(f"Function returned: {result}")

# To see output in Logfire, ensure LOGFIRE_TOKEN is set and 'logfire' is installed.
# The above `logfire.configure()` will only run if `logfire` is installed.
# Otherwise, all logfire calls above will effectively do nothing.

view raw JSON →