William Jamir
02/19/2024, 9:48 PMMarvin
02/19/2024, 9:48 PMWilliam Jamir
02/19/2024, 9:51 PMMarvin
02/19/2024, 9:51 PMprint
statements or the prefect.logging
module as shown in the following examples. By setting log_prints=True
in your flow decorator, any print
statements will be logged as INFO logs:
python
from prefect import flow
@flow(log_prints=True)
def my_workflow():
# This will be logged as an INFO message
print("This is a log message.")
if __name__ == "__main__":
my_workflow() # call it like a normal python function
In the case of exceptions, you can log them as follows:
python
import prefect
from prefect import task, flow
@task
def task_that_may_fail(some_param):
if some_param:
raise ValueError("This is an error message that will be logged.")
@flow(log_prints=True)
def my_workflow_with_exception():
try:
task_that_may_fail(False)
except ValueError as e:
# This will log the exception message as an ERROR
prefect.logger.error(f"An error occurred: {e}")
if __name__ == "__main__":
my_workflow_with_exception()
Regarding retrieving specific logs, Prefect does not have code examples available in their repository for fetching logs by a specific criteria (such as error messages only) via the REST API. However, you can do so by querying the REST API for logs associated with a particular flow run ID and filtering them to find error-level logs or logs containing specific error messages.
You can use an endpoint of Prefect's REST API to fetch logs, like this hypothetical example:
python
import requests
flow_run_id = "your-flow-run-id"
prefect_api_url = "<http://your-prefect-api-url>"
response = requests.get(
f"{prefect_api_url}/flow_run/{flow_run_id}/logs"
)
logs = response.json()
error_logs = [log for log in logs if "ERROR" in log["level"]]
for log in error_logs:
print(log["message"])
For the actual API endpoint and parameters, you would need to refer to the Prefect REST API documentation, which is version-specific and hence necessitates checking the appropriate endpoint in the version of Prefect you work with.
Keep in mind that these are simple examples and you might need to handle authentication, pagination, and error handling when dealing with real API requests.Nate
02/19/2024, 9:56 PMcreate_markdown_artifact(key=..., markdown=...)