Skip to content

formatters

Formatters for a logging dictConfig.

Formatters define the message and timestamp format for log messages.

FormatterConfig dataclass

Bases: BaseLoggingConfig

Define a logging formatter.

Parameters:

Name Type Description Default
name str

The name of the formatter.

None
fmt str

The string formatting to use for log messages.

MESSAGE_FMT_STANDARD
datefmt str

The string formatting to use for log message timestamps.

DATE_FMT_STANDARD
style str

The string substitution style to use for log formats. Default is %, which means formats need to be written like %(asctime)s %(levelname)s %(message)s. If you change this style, make sure the fmt you pass uses the correct formatting style.

'%'
validate bool

When True, the configuration dict this formatter returns will be validated by the logging module.

True
Source code in src/red_logging/config_classes/formatters/_formatters.py
@dataclass
class FormatterConfig(BaseLoggingConfig):
    """Define a logging formatter.

    Params:
        name (str): The name of the formatter.
        fmt (str): The string formatting to use for log messages.
        datefmt (str): The string formatting to use for log message timestamps.
        style (str): The string substitution style to use for log formats. Default is `%`, which
            means formats need to be written like `%(asctime)s %(levelname)s %(message)s`. If
            you change this style, make sure the `fmt` you pass uses the correct formatting style.
        validate (bool): When `True`, the configuration dict this formatter returns will be validated by the logging module.

    """

    name: str = None
    fmt: str = MESSAGE_FMT_STANDARD
    datefmt: str = DATE_FMT_STANDARD
    style: str = "%"
    validate: bool = True

    def get_configdict(self) -> dict[str, dict[str, str]]:
        """Return a dict representation of the formatter described by this class."""
        formatter_dict: dict[str, dict[str, str]] = {self.name: {"format": self.fmt}}
        if self.datefmt:
            formatter_dict[self.name]["datefmt"] = self.datefmt
        if self.style:
            formatter_dict[self.name]["style"] = self.style
        formatter_dict[self.name]["validate"] = self.validate

        return formatter_dict

get_configdict()

Return a dict representation of the formatter described by this class.

Source code in src/red_logging/config_classes/formatters/_formatters.py
def get_configdict(self) -> dict[str, dict[str, str]]:
    """Return a dict representation of the formatter described by this class."""
    formatter_dict: dict[str, dict[str, str]] = {self.name: {"format": self.fmt}}
    if self.datefmt:
        formatter_dict[self.name]["datefmt"] = self.datefmt
    if self.style:
        formatter_dict[self.name]["style"] = self.style
    formatter_dict[self.name]["validate"] = self.validate

    return formatter_dict