Skip to content

Commit

Permalink
修改: src/d2py/utils/log_config.py
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinwei committed Apr 16, 2024
1 parent 24c1b7c commit 2e191bc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dynamic = ["version", "description"]
license = {file = "LICENSE"}
name = "d2py"
readme = "README.md"
requires-python = ">=3.10"
requires-python = ">=3.9"

dependencies = []

Expand Down
2 changes: 1 addition & 1 deletion src/d2py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Dive into Python."""
__version__ = '0.5.10'
__version__ = '0.5.11'
23 changes: 8 additions & 15 deletions src/d2py/utils/log_config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
from logging.handlers import RotatingFileHandler

def config_logging(
filename: str,
logger_name: str="logger",
filemode: str='a',
filter_mod_names: set=set(),
default_filter_mod_names: set = {
"matplotlib", "PIL", "asyncio",
Expand All @@ -12,7 +12,6 @@ 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 @@ -31,23 +30,17 @@ def config_logging(
>>> ch = RichHandler(**rich_kwargs)
```
"""
logging.basicConfig(level=logging.DEBUG,
format=file_formatter,
datefmt='%m-%d %H:%M',
filename=filename,
filemode=filemode, **kwargs)

logger = logging.getLogger(logger_name)
# 禁用一些 debug 信息
for mod_name in filter_mod_names|default_filter_mod_names:
_logger = logging.getLogger(mod_name)
_logger.setLevel(logging.WARNING)

# 创建日志级别更高的控制台处理程序
if rich_kwargs:
from rich.logging import RichHandler
ch = RichHandler(**rich_kwargs)
else:
ch = logging.StreamHandler()
logger.setLevel(logging.DEBUG)
fh = RotatingFileHandler(filename, **kwargs)
fh.setFormatter(logging.Formatter(file_formatter))
# fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO) # 或者 logging.ERROR
ch_formatter = logging.Formatter(stream_formatter)
ch.setFormatter(ch_formatter)
Expand Down

0 comments on commit 2e191bc

Please sign in to comment.