<@ULVA73B9P> Using the python sdk, how would I get...
# ask-marvin
e
@Marvin Using the python sdk, how would I get logs for a Prefect flow run? I primarily want to get the reason why a flow run might have failed
m
To get logs for a Prefect flow run using the Python SDK, you can use the following approach: 1. Log within your flow: Ensure that your flow is logging relevant information using the
get_run_logger
method. Here is an example of how you can log information within your flow:
Copy code
python
from prefect import flow, get_run_logger

@flow
def my_flow():
    logger = get_run_logger()
    <http://logger.info|logger.info>("This is an info log message.")
    logger.error("This is an error log message.")
    try:
        # Simulating a failure
        1 / 0
    except Exception as e:
        logger.exception("An exception occurred: %s", e)

if __name__ == "__main__":
    my_flow()
2. Retrieve logs from the Prefect backend: After running the flow, you can interact with the Prefect Client to fetch logs for the flow run. Here's an example of how you can fetch and print logs for a specific flow run:
Copy code
python
from prefect.client import OrionClient
import asyncio

async def fetch_flow_run_logs(flow_run_id):
    async with OrionClient() as client:
        logs = await client.read_flow_run_logs(flow_run_id)
        for log in logs.logs:
            display(log)

flow_run_id = 'your-flow-run-id-here'
asyncio.run(fetch_flow_run_logs(flow_run_id))
3. Check for failure reasons: By examining the logs retrieved, you can determine the reason for flow run failures. For more details and examples, you can refer to the Prefect logging guide. If you want to see another code example of a basic flow with logging, visit this link. This approach should help you investigate failures in your Prefect flows by examining logs through the Prefect backend.