Joseph Wibowo
11/17/2025, 11:47 PMMarvin
11/17/2025, 11:47 PMMarvin
11/17/2025, 11:49 PMprefect.logging.get_run_logger() (inside a running flow/task) are automatically sent to the Prefect API and shown in the UI. Plain logging.getLogger(...) won’t reach the UI unless you configure Prefect to capture those loggers or attach Prefect’s API log handler yourself.
What to use:
- Inside flows/tasks, prefer `get_run_logger()`:
from prefect import flow, task
from prefect.logging import get_run_logger
@task
def do_work():
logger = get_run_logger()
<http://logger.info|logger.info>("This will appear in the Prefect UI")
@flow(log_prints=True) # optional: capture print() as logs
def my_flow():
do_work()
print("This will also appear in the UI because log_prints=True")
if __name__ == "__main__":
my_flow()
If you need to capture native/third‑party loggers:
- Enable extra loggers via settings (so they’re wired to the API handler):
export PREFECT_LOGGING_EXTRA_LOGGERS="myapp,urllib3"
- Or manually attach Prefect’s API log handler:
import logging
from prefect.logging.handlers import APILogHandler
logger = logging.getLogger("myapp")
logger.setLevel(<http://logging.INFO|logging.INFO>)
logger.addHandler(APILogHandler()) # will send logs to the Prefect API when a run context is active
Notes:
- print() is not logged unless you set log_prints=True on the `@flow`/`@task` decorator.
- prefect.logging.get_logger() returns a standard logger with Prefect formatting, but it won’t send to the API unless you attach APILogHandler or include it in PREFECT_LOGGING_EXTRA_LOGGERS.
Docs:
- Add logging to flows and tasks
- Advanced logging customization
- prefect.logging.loggers API
- prefect.logging.handlers API