Skip to content

Commit

Permalink
Added formatException and formatStack implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
abrahammurciano authored and github-actions[bot] committed Jun 1, 2022
1 parent 7979bf3 commit 71522ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "rainbowlog"
version = "0.1.1"
version = "0.2.0"
description = "Format your python logs with colours based on the log levels."
authors = ["Abraham Murciano <[email protected]>"]
license = "MIT"
Expand Down
17 changes: 15 additions & 2 deletions rainbowlog/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
logging.CRITICAL: {"fg": "red", "style": "bold+italic+underline"},
}

default_exception_config = default_config[logging.CRITICAL]
default_stack_config = default_config[logging.ERROR]


class Formatter(logging.Formatter):
"""This log formatter wraps a given formatter and adds color to the output.
Expand All @@ -30,11 +33,15 @@ def __init__(
self,
inner_formatter: logging.Formatter,
color_configs: Mapping[int, Mapping[str, Any]] = default_config,
exception_config: Mapping[str, Any] = default_exception_config,
stack_config: Mapping[str, Any] = defaul_stack_config,
):
"""
Args:
inner_formatter: The formatter to use for the log messages.
color_configs: A mapping from log levels to keyword arguments for the ansicolors library's color function.
inner_formatter: The formatter to use for the log messages.
color_configs: A mapping from log levels to keyword arguments for the ansicolors library's color function.
exception_config: The keyword arguments to pass to color for formatting exceptions.
stack_config: The keyword arguments to pass to color for formatting stack traces.
"""
self.formatter = inner_formatter
self.configs = color_configs
Expand All @@ -43,3 +50,9 @@ def format(self, record: logging.LogRecord) -> str:
return color(
self.formatter.format(record), **self.configs.get(record.levelno, {})
)

def formatException(exc_info):
return color(self.formatter.formatException(exc_info), **self.exception_config)

def formatStack(stack_info):
return color(self.formatter.formatStack(stack_info), **self.stack_config)

0 comments on commit 71522ab

Please sign in to comment.