<@ULVA73B9P> what is the best way to use logging w...
# ask-marvin
o
@Marvin what is the best way to use logging with prefect?
m
thought for 274.0 seconds
Great question! Assuming Prefect 3.x, here’s the most effective way to do logging. Quick start (best practice) - In flows/tasks, always use
get_run_logger()
so your logs are enriched with run context and show up in the UI/API. - Configure behavior via Prefect settings (profiles or env vars), not hardcoded in your flow code. - If you need structured logs, switch the console handler to JSON via a logging.yml. - To capture third‑party libraries, set
PREFECT_LOGGING_EXTRA_LOGGERS
. Minimal example
Copy code
from prefect import flow, task
from prefect.logging import get_run_logger

@task(log_prints=True)  # captures `print()` as INFO logs
def my_task():
    logger = get_run_logger()
    <http://logger.info|logger.info>("Task is running")
    logger.debug("Extra debug info")

@flow(log_prints=True)
def my_flow():
    logger = get_run_logger()
    <http://logger.info|logger.info>("Flow started")
    my_task()

if __name__ == "__main__":
    my_flow()
Common configuration (via profile or env) - View current settings:
Copy code
prefect config view
- Set common logging options:
Copy code
prefect config set PREFECT_LOGGING_LEVEL=DEBUG
prefect config set PREFECT_LOGGING_COLORS=false
prefect config set PREFECT_LOGGING_MARKUP=true
prefect config set PREFECT_LOGGING_EXTRA_LOGGERS="requests,urllib3"
Notes: -
get_run_logger()
sends logs to the Prefect API automatically; standard
logging.getLogger()
does not unless you add it to
PREFECT_LOGGING_EXTRA_LOGGERS
. - Use
log_prints=True
on flows/tasks to convert
print()
to logs. Structured/JSON logs Use a custom logging.yml to switch the console handler to JSON. Save this to
~/.prefect/logging.yml
(or point to a file with
PREFECT_LOGGING_SETTINGS_PATH
):
Copy code
version: 1
disable_existing_loggers: false

formatters:
  json:
    class: prefect.logging.formatters.JsonFormatter
    format: default  # or "pretty"

handlers:
  console:
    class: prefect.logging.handlers.PrefectConsoleHandler
    level: 0
    formatter: json
  api:
    class: prefect.logging.handlers.APILogHandler
    level: 0

loggers:
  prefect.flow_runs:
    level: NOTSET
    handlers: [console, api]
    propagate: false
  prefect.task_runs:
    level: NOTSET
    handlers: [console, api]
    propagate: false

root:
  level: WARNING
  handlers: [console]
Capturing third‑party library logs - Add their logger names to
PREFECT_LOGGING_EXTRA_LOGGERS
to route them through Prefect’s handlers (console/API):
Copy code
prefect config set PREFECT_LOGGING_EXTRA_LOGGERS="dask,botocore,sqlalchemy"
Where logs go - get_run_logger() output is formatted and sent to: - Stdout (via
PrefectConsoleHandler
, colored by default) - Prefect API for viewing in the UI (via
APILogHandler
) - You can further ship stdout to your log aggregator (e.g., via Docker/K8s). Helpful links - How to customize logging: Logging customization - Loggers API (includes
get_run_logger
): prefect.logging.loggers API - Handlers/formatters API: handlersformatters - Settings & profiles: Settings and profiles - Default logging.yml (reference): default logging.yml Want me to tailor a snippet for your setup? Let me know: - Prefect Cloud or self-hosted? - Local, Docker, or Kubernetes? - Do you need JSON logs for a specific aggregator (e.g., CloudWatch, GCP, ELK)?