Skip to content

Commit

Permalink
[melobot] Add support of yellow log color in warn level
Browse files Browse the repository at this point in the history
  • Loading branch information
aicorein committed Dec 10, 2024
1 parent 1343631 commit 0dae81d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/melobot/log/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,17 @@ def filter(self, record: logging.LogRecord) -> bool:

class _MeloLogFilter(logging.Filter):
def __init__(
self, name: str = "", red_error: bool = True, legacy: bool = False
self,
name: str = "",
yellow_warn: bool = True,
red_error: bool = True,
legacy: bool = False,
) -> None:
super().__init__(name)
self._obj: Any = VoidType.VOID
self._yellow_style = Style(color="yellow")
self._red_style = Style(color="red")
self._enable_yellow_warn = yellow_warn
self._enable_red_error = red_error
self._legacy = legacy

Expand Down Expand Up @@ -135,7 +141,9 @@ def filter(self, record: logging.LogRecord) -> Literal[True]:
return True

def _fill_msg_and_obj(self, msg: str, record: logging.LogRecord) -> None:
yellow_style = self._yellow_style
red_style = self._red_style
yellow_warn = self._enable_yellow_warn
red_error = self._enable_red_error

if self._legacy:
Expand All @@ -155,6 +163,8 @@ def _fill_msg_and_obj(self, msg: str, record: logging.LogRecord) -> None:

if red_error and record.levelno >= ERROR:
record.colored_msg_str, record.msg_str = _get_rich_repr(msg, red_style)
elif yellow_warn and record.levelno >= WARNING:
record.colored_msg_str, record.msg_str = _get_rich_repr(msg, yellow_style)
else:
record.colored_msg_str, record.msg_str = _get_rich_repr(msg)

Expand All @@ -163,6 +173,9 @@ def _fill_msg_and_obj(self, msg: str, record: logging.LogRecord) -> None:
elif red_error and record.levelno >= ERROR:
record.legacy_obj = record.obj = _get_rich_object(self._obj, no_color=True)[1]
record.colored_obj = ""
elif yellow_warn and record.levelno >= WARNING:
record.legacy_obj = record.obj = _get_rich_object(self._obj, no_color=True)[1]
record.colored_obj = ""
else:
record.colored_obj, record.obj = _get_rich_object(self._obj)

Expand Down Expand Up @@ -327,6 +340,7 @@ def __init__(
to_dir: str | None = None,
add_tag: bool = False,
legacy: bool = False,
yellow_warn: bool = True,
red_error: bool = True,
two_stream: bool = False,
) -> None:
Expand All @@ -339,6 +353,10 @@ def __init__(
:param to_dir: 保存日志文件的目录,为空则不保存文件
:param add_tag: 记录日志时是否标识日志器名称
:param legacy: 记录日志时是否使用传统样式(不对日志内容进行自动高亮,而是使用日志等级的五色)
:param yellow_warn:
记录 `LogLevel.WARN` 级别时,是否将日志内容着色为黄色。
`legacy` 选项为 `True` 时此参数无效
:param red_error:
记录 `LogLevel.ERROR` 及以上级别时,是否将日志内容着色为红色。
`legacy` 选项为 `True` 时此参数无效
Expand All @@ -354,7 +372,7 @@ def __init__(
super().__init__(name, LogLevel.DEBUG)
self._handler_arr: list[logging.Handler] = []
self._no_tag = not add_tag
self._filter = _MeloLogFilter(name, red_error, legacy)
self._filter = _MeloLogFilter(name, yellow_warn, red_error, legacy)

if to_console:
con_handler = self._add_console_handler()
Expand Down

0 comments on commit 0dae81d

Please sign in to comment.