<@ULVA73B9P> is there a way to disable the logging...
# ask-marvin
p
@Marvin is there a way to disable the logging of a task finishing when running task.map? I'd like to map a task to a large list and I don't want my logs to be spammed with
Finished in state Completed()
.
m
Disabling task finish logs can indeed declutter your outputs in Prefect. Here's how you can manage your logging preferences: 1. Adjust Log Levels: Prefect uses standard Python logging, and you can configure the log levels. By default, Prefect logs at the
INFO
level. You can adjust this setting in your Prefect configuration to a higher level like
WARNING
or
ERROR
to suppress
INFO
level logs, which include task completion messages. This configuration can be set globally or specifically for certain tasks and flows. This means you won't see the "Finished in state Completed()" logs. Here's more about this: Prefect 2.x Logging and Prefect 3.x Logging. 2. Using
log_prints
Option
: Prefect provides the
log_prints=False
option at both the task and flow levels to disable automatic logging of print statements. While this is slightly different from your current request, it's good to know about such features if you ever want to disable print statement logs specifically. You can explore the links provided to get a deeper understanding of how to customize your logging settings. This should help minimize the log output to your liking. Don't we all wish we could just tell our programs to keep things to themselves sometimes? If you need further assistance configuring these, feel free to ask!
p
@Marvin I'm using prefect 3. Give me an example on how to locally raise log-level around task.map, so that INFO level logs are filtered out.
m
To adjust the log level locally around your
task.map
in Prefect 3, you will use Python's standard logging configuration. Here's a simple example to help guide you:
Copy code
python
import logging
from prefect import flow, task, get_run_logger

# Configure the root logger. This is a global change.
logging.basicConfig(level=logging.WARNING)

@task
def my_task(x):
    logger = get_run_logger()
    <http://logger.info|logger.info>(f"Processing {x}")
    return x * 2

@flow
def my_flow():
    # Temporarily change the log level
    logger = logging.getLogger("prefect")
    previous_level = logger.level
    logger.setLevel(logging.WARNING)  # Set a higher level to suppress INFO logs
    
    results = my_task.map(range(10))  # Logs from these tasks will be at WARNING level
    
    logger.setLevel(previous_level)  # Reset back to the previous level

    return results

if __name__ == "__main__":
    my_flow()
In this example, I've changed the logging level to
WARNING
before the
task.map
call to suppress
INFO
-level logs like "Finished in state Completed()". After the
map
, it resets to the previous logging level. For more about configuring Prefect's logging, you can refer to the Prefect 3.x Logging Documentation. Who knew teaching a task to "shhh!" could be so rewarding?