Does anyone here have any experience using Prefect...
# ask-community
n
Does anyone here have any experience using Prefect with Logfire? I am trying to figure out how to get Prefect to capture my Logfire logs.
n
cc’ing @Alexander Azzam
hi @Nelson Griffiths have you tried setting extra loggers in prefect?
n
I have. Logfire isn't really a traditional logger, and from what I can tell doesn't have a logger name like most loggers do. I tried adding
logfire
to the extra loggers but that didn't work.
n
right but logfire does integrate with standard loggers, which is what prefect loggers are based on. so its not actually extra loggers in this case instead this should mostly be about writing a custom
logging.yaml
file and then setting that with
PREFECT_LOGGING_CONFIG_PATH
for example
Copy code
import logfire
from prefect import flow
from prefect.logging import get_run_logger

# Initialize Logfire first
logfire.configure()


@flow
def log_flow():
    logger = get_run_logger()
    logger.error("Damnit, Marvin!")


if __name__ == "__main__":
    log_flow()
Copy code
» PREFECT_LOGGING_CONFIG_PATH=logging.yaml python main.py
Logfire project URL: <https://logfire.pydantic.dev/zzstoatzz/marvin>
13:52:17.188 | INFO    | prefect.engine - Created flow run 'cheerful-doberman' for flow 'log-flow'
18:52:17.414 Damnit, Marvin!
13:52:17.410 | ERROR   | Flow run 'cheerful-doberman' - Damnit, Marvin!
18:52:17.593 Finished in state Completed()
13:52:17.591 | INFO    | Flow run 'cheerful-doberman' - Finished in state Completed()
where I added the following
Copy code
handlers:
...

  # Add Logfire handler
  logfire:
    level: 0
    class: logfire.LogfireLoggingHandler
    formatter: standard

loggers:
  prefect:
    level: "${PREFECT_LOGGING_LEVEL}"

...

  prefect.flow_runs:
    level: NOTSET
    handlers: [api, logfire] # Added logfire

  prefect.task_runs:
    level: NOTSET
    handlers: [api, logfire] # Added logfire
one last note, I just found out you can pass
console=False
to avoid dupe log records from logfire in your local console
Copy code
logfire.configure(console=False)
n
Okay thanks I will look at this!