<@ULVA73B9P> is there a way to set the background ...
# ask-marvin
t
@Marvin is there a way to set the background color of the prefect logs for a flow run?
m
Yes, you can customize the log appearance for flow runs in Prefect, including setting background colors for specific log messages. Prefect allows you to adjust the highlight styles in the console logs by configuring the logging settings. For both Prefect 2.x and 3.x, you can accomplish this by creating and modifying a
logging.yml
file. Here’s a brief guide on how to do it: 1. Create a custom logging handler and highlighter: - Create a Python file (e.g.,
my_custom_logging.py
) that defines a custom highlighter and handler. This file should be in the same directory as your flow script or part of a Python package.
Copy code
python
    import logging
    from typing import Dict, Union
    from rich.highlighter import Highlighter
    from prefect.logging.handlers import PrefectConsoleHandler
    from prefect.logging.highlighters import PrefectConsoleHighlighter

    class CustomConsoleHighlighter(PrefectConsoleHighlighter):
        base_style = "log."
        highlights = PrefectConsoleHighlighter.highlights + [
            # Custom regex for highlighting emails
            r"(?P<email>[\\w-]+@[\\w-]+\\.[\\w-]+)"
        ]

    class CustomConsoleHandler(PrefectConsoleHandler):
        def __init__(
            self,
            highlighter: Highlighter = CustomConsoleHighlighter,
            styles: Dict[str, str] = None,
            level: Union[int, str] = logging.NOTSET,
        ):
            super().__init__(highlighter=highlighter, styles=styles, level=level)
2. Update the logging configuration: - Modify the
logging.yml
configuration file to use your custom handler and define the styles, including background colors.
Copy code
yaml
    handlers:
      console_flow_runs:
        level: 0
        class: path.to.my_custom_logging.CustomConsoleHandler
        formatter: flow_runs
        styles:
          log.email: "magenta on white"
          # Define additional styles with background colors
          # log.info_level: "black on green"
          # log.error_level: "white on red"
3. Set environment variables: - Ensure Prefect uses your custom logging configuration by setting the
PREFECT_LOGGING_COLORS
environment variable to
True
and pointing to your
logging.yml
file. For detailed information and more customization options, refer to Prefect's logging documentation: - Prefect 2.x: https://docs-2.prefect.io/latest/guides/logs/ - Prefect 3.x: https://docs-3.prefect.io/3.0/develop/logging Feel free to ask if you have more questions!
t
perfect, thank you @Marvin
m
You're welcome! If you have any more questions or need further assistance, feel free to ask. Happy coding!