Skip to content

Integrate Logfire

This step assumes you’ve already sent your first trace, which installs the SDK and calls logfire.configure(). Here we integrate Logfire more fully with your application: the instrumentation packages for the libraries you already use, and your existing logging.

OpenTelemetry Instrumentation

Logfire works with any OpenTelemetry instrumentation package (a plug-in that automatically traces a specific library or framework), and includes a CLI command that highlights instrumentation your project is missing.

To inspect your project, run the following command:

Terminal
logfire inspect

This lists the packages you need to install for OpenTelemetry instrumentation:

Logfire inspect command

To install the missing packages, copy the command provided by the inspect command, and run it in your terminal.

Each instrumentation package has its own way to be configured. Check our Integrations page to learn how to configure them.

Logging Integration (Optional)

There are many logging systems within the Python ecosystem, and Logfire provides integrations for the most popular ones: Standard Library Logging, Loguru, and Structlog.

Standard Library

To integrate Logfire with the standard library logging module, you can use the LogfireLoggingHandler class.

The minimal configuration would be the following:

from logging import basicConfig

import logfire

logfire.configure()
basicConfig(handlers=[logfire.LogfireLoggingHandler()])

Now imagine, that you have a logger in your application:

main.py
from logging import basicConfig, getLogger

import logfire

logfire.configure()
basicConfig(handlers=[logfire.LogfireLoggingHandler()])

logger = getLogger(__name__)
logger.error('Hello %s!', 'Fred')

If we run the above code, with python main.py, we will see the following output:

Terminal with Logfire output

If you go to the link, you will see the "Hello Fred!" log in the Web UI:

Logfire Web UI with logs

Loguru

To integrate with Loguru, check out the Loguru page.

Structlog

To integrate with Structlog, check out the Structlog page.

Next step

Add manual tracing: create your own spans and logs to record exactly the operations you care about.