Yes, you can! But this is considered an extra logger, so you would need to add it to your prefect configuration e.g., as an environment variable. Check this usage example:
The solution from Michael!
from prefect import flow, task
from logging import getLogger
logger = getLogger("my-logger")
logger.setLevel("INFO")
@task
def my_task():
logger.info("Hello from the task")
@flow
def my_flow():
logger.info("Hello from the flow")
my_task()
my_flow()
Then run the flow with these environment variables:
PREFECT_LOGGING_LEVEL=WARNING PREFECT_LOGGING_EXTRA_LOGGERS=my-logger python flow.py
Results in only the custom loggers
12:40:19.285 | INFO | my-…
Yes, exactly as shown in the example. This is true only for the extra logger , though. Defining Prefect logger globally wouldn’t work - see this topic:
Prefect 2.0
No, setting the logger globally (i.e. at a module scope) is not supported. You should define the logger and log messages within your tasks and flows instead. The following will raise a RuntimeError:
from prefect import get_run_logger
logger = get_run_logger()
logger.info("Starting the script...")
The output
Traceback (most recent call last):
File "/Users/xxx/repos/orion/example.py", line 3, in <module>
logger = get_run_logger()
File "/Users/xxx/repos/orion/src/prefect/lo…
2 Likes