How to add extra loggers to Prefect 1.0?

This example adds boto3 and botocore loggers so that debug logs from those libraries are displayed in the Prefect UI. Those extra log messages allow to debug issues such as bad requests made to some AWS APIs.

First, set those two environment variables on the machine from which you register your flow (e.g. your laptop):

export PREFECT__LOGGING__EXTRA_LOGGERS="['boto3', 'botocore']"

Now, when you use boto3 in your flow code and set the level to DEBUG explicitly, all of boto3 debug logs such as API response headers become available in the logs. Here is an example:

import boto3
import logging
import pandas as pd
import subprocess

import prefect
from prefect import task, Flow
from import Local
from prefect.run_configs import LocalRun

def get_s3_data():
    prefect_logger = prefect.context.get("logger")
    s3 = boto3.client("s3", region_name="us-east-1")
    obj = s3.get_object(Bucket="nyc-tlc", Key="trip data/yellow_tripdata_2021-07.csv",)
    df = pd.read_csv(obj["Body"], nrows=5, low_memory=False)"Read df: %s", df.head(5))

with Flow(
            "PREFECT__LOGGING__EXTRA_LOGGERS": "['boto3', 'botocore']",
) as flow:

if __name__ == "__main__":
    flow_id = flow.register("xyz")"prefect run --id {flow_id}", shell=True)
        "prefect agent local start --label logtest --no-hostname-label", shell=True

When you run the command above, this will register your flow, trigger a flow run and spin up a local agent. When you execute it, you should see the detailed logs from boto3.

In this example, we are using a public dataset, so you should be able to run it without changing anything

More resources on the subject