Hi! Yet another logging question.. I am trying to...
# ask-community
c
Hi! Yet another logging question.. I am trying to add a file logging handler that captures everything, while the “prefect” logger only shows INFO and up. I have tried searching around in the website, github and here, but with no luck. Right now I have the following MVE (see thread) and I am running prefect locally. Everything I do changes both the level of the prefect logger and the file-logger at the same time. How do I unentangle them? Also, it would be great if it could work with a local dask executor!
k
Hey @Christian Michelsen, I assume it’s all good except that the level is coupled together? I think the issue here is that the logger must have the lowest log level. For example, you can do DEBUG on the logger, and INFO on the FileHandler, but not the other way around. See this . Also, we’d appreciate it if you could move the code snippet to the thread so we don’t crowd the main channel 🙂
c
Moved code:
Copy code
@task()
def task_a():
    logger = prefect.context.get("logger")
    logger.debug("TASK A: Returning 3")
    <http://logger.info|logger.info>("TASK A: Returning 3")
    logger.warning("TASK A: Returning 3")
    logger.error("TASK A: Returning 3")
    return 3
@task()
def task_b(x):
    logger = prefect.context.get("logger")
    y = 3 * x + 1
    return y
prefect.utilities.logging.prefect_logger.setLevel("INFO")
f_handler = logging.FileHandler("file.log", mode="w")
f_handler.setLevel(logging.DEBUG)
f_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
f_handler.setFormatter(f_format)
with Flow("logging-example") as flow:
    result = task_b(task_a)
task_logger = get_logger()
task_logger.addHandler(f_handler)
task_logger.setLevel(logging.DEBUG)
flow.run()
Hi @Kevin Kho and thanks for the quick response! The issue is that I want to log everything to file, but only show some of the information on the screen. Setting DEBUG on the main logger and INFO on the filehandler is the opposite of what I want to archive 🙂
k
I know but I think this is a limitation of the Python logger where doing it with INFO on the main logger and DEBUG on the filehandler will not give you the DEBUG logs.
I’ll look into this a bit more