I've managed to silence the logger: I can't see th...
# ask-community
h
I've managed to silence the logger: I can't see the stacktrace when a task crashes. How do I make Prefect print the stacktrace locally?
Copy code
@task(
    nout=2,
    max_retries=10,
    retry_delay=timedelta(seconds=1),
)
def fetch_model_settings(
    dsn_params: DSNParams,
    app_id: UUID,
    default_model_group_id: UUID,
) -> ModelSettings:
    logger = prefect.context.get("logger")
    <http://logger.info|logger.info>(
        f"Fetching model settings for app_id={app_id}, default_model_group_id={default_model_group_id}"
    )
    raise ValueError("WTF where are the logs")

# before flow:
prefect.config.logging.level = "DEBUG"
In the sample above I never get to see the WTF in the output of running the flow in the console/locally (
PREFECT__LOGGING__LEVEL=DEBUG python flows/flow.py
)
This is actually a problem when the inputs / upstreams are "unmappable" but there's no error message about this unless you attach a failure state handler and log the result.
a
@haf 1. This line is not required to get exception logged:
prefect.config.logging.level = "DEBUG"
2. If you want to get full exception traceback, you can try the exception_to_string function from this Gist that includes the full exception traceback in a state handler: https://gist.github.com/anna-geller/2014180ee5eaec9ea54f4d3f5b98ca93 3. When running the flow locally, you should be able to see your exception. Here is my reproduction:
Copy code
import prefect
from prefect import task, Flow


@task()
def fetch_model_settings():
    logger = prefect.context.get("logger")
    <http://logger.info|logger.info>("Fetching model settings")
    raise ValueError("WTF where are the logs")


with Flow("exception-logging") as flow:
    fetch_model_settings()

if __name__ == '__main__':
    flow.run()
Console output:
Copy code
[2021-11-24 13:22:41+0100] INFO - prefect.FlowRunner | Beginning Flow run for 'exception-logging'
[2021-11-24 13:22:41+0100] INFO - prefect.TaskRunner | Task 'fetch_model_settings': Starting task run...
[2021-11-24 13:22:41+0100] INFO - prefect.fetch_model_settings | Fetching model settings
[2021-11-24 13:22:41+0100] ERROR - prefect.TaskRunner | Task 'fetch_model_settings': Exception encountered during task execution!
Traceback (most recent call last):
  File "/Users/anna/opt/anaconda3/envs/prefect-dbt-cloud/lib/python3.9/site-packages/prefect/engine/task_runner.py", line 876, in get_task_run_state
    value = prefect.utilities.executors.run_task_with_timeout(
  File "/Users/anna/opt/anaconda3/envs/prefect-dbt-cloud/lib/python3.9/site-packages/prefect/utilities/executors.py", line 454, in run_task_with_timeout
    return task.run(*args, **kwargs)  # type: ignore
  File "<input>", line 9, in fetch_model_settings
ValueError: WTF where are the logs
[2021-11-24 13:22:41+0100] INFO - prefect.TaskRunner | Task 'fetch_model_settings': Finished task run for task with final state: 'Failed'
[2021-11-24 13:22:41+0100] INFO - prefect.FlowRunner | Flow run FAILED: some reference tasks failed.
h
Yes, thank you for testing it 🙂 . I actually found that I had a problem when the inputs / upstreams are "unmappable" but there's no error message about this unless you attach a failure state handler and log the result.