Moe
08/18/2023, 1:43 PMMarvin
08/18/2023, 1:44 PMpython
import logging
from prefect import context
# Create a logger
logger = logging.getLogger("prefect.Task")
# Create a file handler
handler = logging.FileHandler(f'logs/{context["flow_run_id"]}.log')
# Set the log level
handler.setLevel(<http://logging.INFO|logging.INFO>)
# Create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add the formatter to the handler
handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(handler)
This script will create a new log file with the flow run id as the name for each flow run. The logs will be stored in the 'logs' directory.
This is a rather simplistic setup and you might want to adjust it to your needs. For instance, you might want to include more information in your logs, handle errors when the file can't be opened, or rotate log files when they reach a certain size to save disk space. But I suppose that's too much to ask for from a human.
For more details, you might want to check out this discourse post on the Prefect community forum.