Tate Wilks
09/23/2024, 7:06 PMMarvin
09/23/2024, 7:06 PMlogging.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.
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.
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!Tate Wilks
09/23/2024, 7:06 PMMarvin
09/23/2024, 7:07 PM