Reading Logging Data in a Prefect Task #9495
-
Hello everyone, I am currently working on a prefect flow and I want to create a prefect task that reads all the available logging data of the flow that is being executed at the moment. My goal is to search for specific keywords such as "ERROR" in the logs and take appropriate action based on the results. Is it possible to read the logging data of the currently running flow? If so, how can I achieve this? Any suggestions or insights would be greatly appreciated. Thank you in advance for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You could read the logs from the API with the client import asyncio
from prefect import flow, task, get_client, runtime, get_run_logger
from prefect.server.schemas.filters import LogFilter
from prefect.logging.handlers import APILogHandler
@flow
def example():
check_logs.submit()
logger = get_run_logger()
logger.info("test")
@task
async def check_logs():
async with get_client() as client:
for _ in range(5):
# Ensure any pending logs are sent
await APILogHandler.aflush()
logs = await client.read_logs(
LogFilter(flow_run_id={"any_": [runtime.flow_run.id]})
)
for log in logs:
print(log)
await asyncio.sleep(1)
example() |
Beta Was this translation helpful? Give feedback.
You could read the logs from the API with the client