Created
December 14, 2022 12:31
-
-
Save anna-geller/0b9e6ecbde45c355af425cd5b97e303d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from prefect import get_run_logger | |
def enable_loguru_support() -> None: | |
"""Redirect loguru logging messages to the prefect run logger. | |
This function should be called from within a Prefect task or flow before calling any module that uses loguru. | |
This function can be safely called multiple times. | |
Example Usage: | |
from prefect import flow | |
from loguru import logger | |
from prefect_utils import enable_loguru_support # import this function in your flow from your module | |
@flow() | |
def myflow(): | |
logger.info("This is hidden from the Prefect UI") | |
enable_loguru_support() | |
logger.info("This shows up in the Prefect UI") | |
""" | |
# import here for distributed execution because loguru cannot be pickled. | |
from loguru import logger # pylint: disable=import-outside-toplevel | |
run_logger = get_run_logger() | |
logger.remove() | |
log_format = "{name}:{function}:{line} - {message}" | |
logger.add( | |
run_logger.debug, | |
filter=lambda record: record["level"].name == "DEBUG", | |
level="TRACE", | |
format=log_format, | |
) | |
logger.add( | |
run_logger.warning, | |
filter=lambda record: record["level"].name == "WARNING", | |
level="TRACE", | |
format=log_format, | |
) | |
logger.add( | |
run_logger.error, | |
filter=lambda record: record["level"].name == "ERROR", | |
level="TRACE", | |
format=log_format, | |
) | |
logger.add( | |
run_logger.critical, | |
filter=lambda record: record["level"].name == "CRITICAL", | |
level="TRACE", | |
format=log_format, | |
) | |
logger.add( | |
run_logger.info, | |
filter=lambda record: record["level"].name | |
not in ["DEBUG", "WARNING", "ERROR", "CRITICAL"], | |
level="TRACE", | |
format=log_format, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment