<@ULVA73B9P> How do I set logging level to 'debug'...
# ask-marvin
m
@Marvin How do I set logging level to 'debug' within a prefect flow?
m
To set the logging level to 'debug' within a Prefect flow, you can configure the logging settings using environment variables or a configuration file. Here are the steps you can follow: 1. Environment Variable: Set the environment variable
PREFECT_LOGGING_ROOT_LEVEL
to
DEBUG
. This will configure the root logger to output debug level logs. 2. Configuration File: You can create a
logging.yml
file in your
PREFECT_HOME
directory (default is
~/.prefect
) or specify a custom path using the
PREFECT_LOGGING_SETTINGS_PATH
setting. In this file, you can define the logging configuration, including setting the level to
DEBUG
. 3. Using Prefect's Logger: Within your flow, you can use Prefect's
get_run_logger()
to obtain a logger that is aware of the current flow or task run context. This allows for more detailed and contextual logging. Here is a basic example of using
get_run_logger()
in a flow:
Copy code
python
from prefect import flow
from prefect.logging import get_run_logger

@flow
def my_flow():
    logger = get_run_logger()
    logger.debug("This is a debug message")

my_flow()
For more detailed information about logging configuration, you can refer to the Prefect Logging Documentation.
m
@Marvin I know how to emit debug logs with
logger.debug()
. What I want to do is set the logging level to DEBUG using the python SDK, so that all debug messages will be sent to the logger in a particular flow.
m
To set the logging level to
DEBUG
for a specific flow using the Prefect Python SDK, you can use the
get_run_logger()
function to obtain the logger and then set its level. Here's how you can do it:
Copy code
python
from prefect import flow
from prefect.logging import get_run_logger
import logging

@flow
def my_flow():
    logger = get_run_logger()
    logger.setLevel(logging.DEBUG)  # Set the logging level to DEBUG
    logger.debug("This is a debug message")

my_flow()
This code snippet sets the logging level to
DEBUG
for the logger obtained in the flow, ensuring that all debug messages are captured during the flow execution.