Skip to content

Print

Turn your existing print() calls into Logfire logs (individual timestamped records of something that happened), so the output you already print shows up in Logfire (searchable and filterable) next to the traces (the full journey of one request through your app) from the rest of your code. Your print() calls still print to the console as usual.

What you’ll capture

  • Each print() call as a log message
  • Its timestamp
  • The printed arguments, captured as structured attributes you can search and filter

Before you start

You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token to copy) and, for production or other languages, creates a write token (the credential your app uses to send data). New to Logfire? Start with Getting Started.

Installation

Install logfire:

Terminal
pip install logfire

No extra library needed: this works with Python’s built-in print().

Usage

Call logfire.configure() to connect to your project, then logfire.instrument_print() to capture every print() call and emit it as a Logfire log.

main.py
import logfire

logfire.configure()
logfire.instrument_print()

name = 'World'
print('Hello', name)

This will still print as usual, but will also emit a log with the message Hello World as expected.

Verify it worked

Run your program, then open the Live view in the Logfire web app. Within a few seconds you’ll see your printed message as a record.

Advanced

Capturing argument names as attributes

If Logfire is configured with inspect_arguments=True, the names of the arguments passed to print will be included in the log attributes and will be used for scrubbing. In the example above, this means that the log will include {'name': 'World'} in the attributes. The first argument 'Hello' is automatically excluded because it’s a literal. If the variable name was password, then it would be scrubbed from both the message and the attributes.

Troubleshooting

Not seeing your printed output in Logfire? Check that logfire.configure() ran first, your write token is set, and logfire.instrument_print() was called.

Reference