<@ULVA73B9P> When running a unit test (pytest) tha...
# ask-marvin
d
@Marvin When running a unit test (pytest) that has a logger, I receive the following warning "UserWarning: Logger 'cronch.collect.assets.process' attempted to send logs to the API without a flow run id. The API log handler can only send logs within flow run contexts unless the flow run id is manually provided. Set PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW=ignore to suppress this warning." How do I remove this warning?
m
To suppress the warning you're encountering, you can set the
PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW
environment variable to
ignore
. This will prevent the warning from being logged when your logger attempts to send logs to the API without a flow run ID. Here's how you can set this environment variable in your test environment:
Copy code
bash
export PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW=ignore
Alternatively, you can set this environment variable within your Python code before the logger is used:
Copy code
python
import os

os.environ["PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW"] = "ignore"
This configuration option controls the behavior when loggers attempt to send logs to the API handler from outside of a flow. The available options are
warn
,
error
, and
ignore
, with the default being
warn
. For more information, you can refer to the Prefect settings documentation. If you need to use a logger that doesn't send logs to the Prefect API, consider using
get_logger()
from the
prefect.logging
module.
d
@Marvin, I want to avoid the the first option of using bash to set the environment variable. The second option fails to provide the intended purpose in the test. The warning is still raised.
m
To suppress the warning directly within your Python code, you can use the
prefect.settings
module to configure the environment variable programmatically. This should ensure that the setting is applied correctly during the execution of your tests. Here's how you can do it:
Copy code
python
from prefect.settings import temporary_settings

with temporary_settings(updates={"PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW": "ignore"}):
    # Your test code here
The
temporary_settings
context manager allows you to temporarily override Prefect settings, ensuring that the environment variable is set appropriately during the execution of the block. This should suppress the warning without affecting other parts of your application. For more details on Prefect's logging and settings, you can explore the logging documentation and settings documentation.