Can I define the logger globally?

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/logging/loggers.py", line 91, in get_run_logger
    raise RuntimeError("There is no active flow or task run context.")
RuntimeError: There is no active flow or task run context.

To avoid such issues, set your logger and add your logs exclusively within tasks, flows, and subflows using the get_run_logger function, as described in this topic:

Prefect 1.0

Prefect 1.0 allows logging exclusively within tasks. Adding logs outside of Prefect tasks is not supported.