Add separate logging FileHandlers within Python code, specific to a flow

Contributed by Tuoyi Zhao:

import prefect
from prefect import flow, task
import logging
from datetime import datetime

def string_prep(ct):
    return ct.strftime("%H:%M:%S")

def dynamic_parameter(
    #Add File Handler to both flow and task logger
    flow_logger = logging.getLogger('prefect.flow_runs')
    task_logger = logging.getLogger('prefect.task_runs')

    handler = logging.FileHandler(filename=<replace with your own log file path>)
    handler.setFormatter(logging.Formatter("%(asctime)s.%(msecs)03d | %(levelname)-7s | %(name)s - %(message)s"))


    #Actual Flow Part
    general_logger = prefect.logging.get_run_logger()
    strings = string_prep(ct)'see see ' + strings)

For global settings, it’s recommended that you use the logging.yml file as described here:

1 Like