Skip to content

ColourizedFormatter

faststream.log.formatter.ColourizedFormatter #

ColourizedFormatter(
    fmt: Optional[str] = None,
    datefmt: Optional[str] = None,
    style: Literal["%", "{", "$"] = "%",
    use_colors: Optional[bool] = None,
)

Bases: Formatter

A class to format log messages with colorized level names.

METHOD DESCRIPTION
__init__

Initialize the formatter with specified format strings.

color_level_name

Colorize the level name based on the log level.

formatMessage

Format the log record message with colorized level name.

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Initialize the formatter with specified format strings.

Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.

Use a style parameter of '%', '{' or '$' to specify that you want to use one of %-formatting, :meth:str.format ({}) formatting or :class:string.Template formatting in your format string.

Source code in faststream/log/formatter.py
def __init__(
    self,
    fmt: Optional[str] = None,
    datefmt: Optional[str] = None,
    style: Literal["%", "{", "$"] = "%",
    use_colors: Optional[bool] = None,
):
    """
    Initialize the formatter with specified format strings.

    Initialize the formatter either with the specified format string, or a
    default as described above. Allow for specialized date formatting with
    the optional datefmt argument. If datefmt is omitted, you get an
    ISO8601-like (or RFC 3339-like) format.

    Use a style parameter of '%', '{' or '$' to specify that you want to
    use one of %-formatting, :meth:`str.format` (``{}``) formatting or
    :class:`string.Template` formatting in your format string.
    """
    if use_colors in (True, False):
        self.use_colors = use_colors
    else:
        self.use_colors = sys.stdout.isatty()
    super().__init__(fmt=fmt, datefmt=datefmt, style=style)

level_name_colors class-attribute instance-attribute #

level_name_colors: DefaultDict[
    str, Callable[[str], str]
] = defaultdict(
    lambda: str,
    **{
        str(logging.DEBUG): lambda: click.style(
            str(level_name), fg="cyan"
        ),
        str(logging.INFO): lambda: click.style(
            str(level_name), fg="green"
        ),
        str(logging.WARNING): lambda: click.style(
            str(level_name), fg="yellow"
        ),
        str(logging.ERROR): lambda: click.style(
            str(level_name), fg="red"
        ),
        str(logging.CRITICAL): lambda: click.style(
            str(level_name), fg="bright_red"
        ),
    }
)

use_colors instance-attribute #

use_colors = use_colors

color_level_name #

color_level_name(level_name: str, level_no: int) -> str

Returns the colored level name.

PARAMETER DESCRIPTION
level_name

The name of the level.

TYPE: str

level_no

The number of the level.

TYPE: int

RETURNS DESCRIPTION
str

The colored level name.

RAISES DESCRIPTION
KeyError

If the level number is not found in the level name colors dictionary.

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/log/formatter.py
def color_level_name(self, level_name: str, level_no: int) -> str:
    """Returns the colored level name.

    Args:
        level_name: The name of the level.
        level_no: The number of the level.

    Returns:
        The colored level name.

    Raises:
        KeyError: If the level number is not found in the level name colors dictionary.
    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    return self.level_name_colors[str(level_no)](level_name)

formatMessage #

formatMessage(record: logging.LogRecord) -> str

Formats the log message.

PARAMETER DESCRIPTION
record

The log record to format.

TYPE: LogRecord

RETURNS DESCRIPTION
str

The formatted log message.

TYPE: str

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/log/formatter.py
def formatMessage(self, record: logging.LogRecord) -> str:
    """Formats the log message.

    Args:
        record (logging.LogRecord): The log record to format.

    Returns:
        str: The formatted log message.
    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    levelname = expand_log_field(record.levelname, 8)
    if self.use_colors is True:  # pragma: no cover
        levelname = self.color_level_name(levelname, record.levelno)
    record.__dict__["levelname"] = levelname
    return super().formatMessage(record)

Last update: 2023-11-13