Lars Nilse
08/15/2024, 2:26 PMprefect.log
file. The logging docu is not covering this. If possible, I would like to set this config in the Python code and not in ~/.prefect/logging.yml
. See here. Thanks.Lars Nilse
08/15/2024, 2:54 PMimport logging
from logging.handlers import RotatingFileHandler
from time import sleep
from prefect import flow, task
from prefect.logging import get_run_logger
# Set up a custom logger
logger = logging.getLogger("prefect")
# Create a handler for writing to a file
handler = RotatingFileHandler(
"all_logs.log",
maxBytes=10 * 1024 * 1024,
backupCount=5,
)
handler.setLevel(logging.DEBUG)
# Add the handler to the logger
logger.addHandler(handler)
# Define the task
@task
def say_hello(name: str) -> None:
"""Say Hello."""
logger = get_run_logger()
logger.debug("This is a debug message in the say_hello task.")
<http://logger.info|logger.info>("We are in the say_hello task.")
logger.error("This is an error message in the say_hello task.")
print(f"Hello, {name}!")
sleep(1)
Lars Nilse
08/15/2024, 2:55 PMRotatingFileHandler
does the trick.
But setting the log level has no effect.Bianca Hoch
08/19/2024, 7:05 PMBianca Hoch
08/19/2024, 7:06 PMBianca Hoch
08/19/2024, 7:06 PMget_logger
, you could:
instantiate it at the top of your script, like so:
from prefect import flow, task
from prefect.logging.loggers import get_logger
logger = get_logger("hey-there")
@task
def work(x):
logger.warning(x)
@flow
def collection_of_work():
<http://logger.info|logger.info>("Starting work...")
work.map(range(5))
collection_of_work()
Bianca Hoch
08/19/2024, 7:10 PMBianca Hoch
08/19/2024, 7:11 PMLars Nilse
08/22/2024, 11:16 AM