Skip to content

log

LoguruFormatter

format

format(record: dict[str, Any]) -> str

Format a Loguru record.

Example
>>> import sys
>>> from loguru import logger
>>> logger.remove()
>>> logger.add(sys.stdout, level="DEBUG", format=loguru_formater)
Source code in stdl/log.py
def format(self, record: dict[str, Any]) -> str:
    """
    Format a Loguru record.

    Example:
        ```python
        >>> import sys
        >>> from loguru import logger
        >>> logger.remove()
        >>> logger.add(sys.stdout, level="DEBUG", format=loguru_formater)
        ```
    """
    extras = ""
    if len(record["extra"]):
        for key in record["extra"]:
            if key in self.extra_key_skips:
                continue

            extras = (
                extras
                + colored(key, self.extra_key_name_color)
                + "="
                + "{extra["
                + key
                + "]}, "
            )
        extras = extras[:-2]

    if title := record["extra"].get("title"):
        return f"{self.time} | {self.level} | [ {title} ] {self.name}:{self.func}:{self.lineno} - {self.msg} {extras}\n"

    return f"{self.time} | {self.level} | {self.name}:{self.func}:{self.lineno} - {self.msg} {extras}\n"

br

br(c: str = '_', length: int | None = None, handler: Callable[[str], None] = print, *, newline: bool = False) -> None

Print a horizontal line break across the terminal.

Parameters:

Name Type Description Default
c str

Character to repeat. Defaults to "_".

'_'
length int | None

Line length. Defaults to terminal width.

None
handler Callable[[str], None]

Function to output the line. Defaults to print.

print
newline bool

Whether to append a newline. Defaults to False.

False
Source code in stdl/log.py
def br(
    c: str = "_",
    length: int | None = None,
    handler: Callable[[str], None] = print,
    *,
    newline: bool = False,
) -> None:
    """
    Print a horizontal line break across the terminal.

    Args:
        c (str): Character to repeat. Defaults to "_".
        length (int | None): Line length. Defaults to terminal width.
        handler (Callable[[str], None]): Function to output the line. Defaults to print.
        newline (bool): Whether to append a newline. Defaults to False.
    """
    if IGNORE_BREAKS:
        return

    length = length or get_terminal_size().columns
    line = c * length
    if newline:
        line += "\n"
    handler(line)