Skip to content

Commit

Permalink
添加日志富文本支持
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinwei committed Mar 21, 2024
1 parent 18b64b2 commit 5933bb4
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/d2py/utils/log_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def config_logging(
},
file_formatter: str="%(levelname)s|%(asctime)s|%(name)s| -> %(message)s\n|==>%(module)s.%(funcName)s@: %(pathname)s:%(lineno)d",
stream_formatter: str="%(levelname)s|%(asctime)s|%(name)s| -> %(message)s",
rich_kwargs: dict|None=None,
**kwargs):
"""配置 logging
Expand All @@ -24,6 +25,12 @@ def config_logging(
default_filter_mod_names: 默认过滤日志 debug 模式对应的模块名称列表
file_formatter: 日志文件配置
stream_formatter: 控制台打印配置
rich_kwargs: 富文本模式,可为空
即开启如下模式
```
>>> from rich.logging import RichHandler
>>> ch = RichHandler(**rich_kwargs)
```
"""
logging.basicConfig(level=logging.DEBUG,
format=file_formatter,
Expand All @@ -37,8 +44,13 @@ def config_logging(
_logger.setLevel(logging.WARNING)

# 创建日志级别更高的控制台处理程序
ch = logging.StreamHandler()
if rich_kwargs:
from rich.logging import RichHandler
ch = RichHandler(**rich_kwargs)
else:
ch = logging.StreamHandler()
ch.setLevel(logging.INFO) # 或者 logging.ERROR
ch_formatter = logging.Formatter(stream_formatter)
ch.setFormatter(ch_formatter)
logging.getLogger("").addHandler(ch)

0 comments on commit 5933bb4

Please sign in to comment.