Eric
05/24/2024, 7:45 PMMarvin
05/24/2024, 7:46 PMget_run_logger
method.
Here is an example of how you can log information within your flow:
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:
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.