Instrumenting print()
¶
logfire.instrument_print()
can be used to capture calls to print()
and emit them
as Logfire logs. For example:
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.
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.