Skip to content

Commit

Permalink
fix: 优化logging模块 (#96)
Browse files Browse the repository at this point in the history
* Update README.md

* Update client.py

* Update logging.py

* Update client.py
  • Loading branch information
single-ptilopsis committed Jun 11, 2022
1 parent 2fb8159 commit 9162c24
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
24 changes: 13 additions & 11 deletions README.md
Expand Up @@ -138,10 +138,11 @@ SDK的日志设置集成在`bot.Client`的实例化阶段,也可通过[`loggin
```python
import botpy

# 示例,非默认值
botpy.Client(
log_level=20,
log_level=10,
log_format="new format",
bot_log=True,
bot_log=None,
ext_handlers=False,
log_config="log_config.json"
)
Expand Down Expand Up @@ -171,39 +172,40 @@ python3 demo_at_reply.py -d

### log_format

日志控制台输出格式
日志控制台输出格式,默认为 `"\033[1;33m[%(levelname)s]\t(%(filename)s:%(lineno)s)%(funcName)s\t\033[0m%(message)s"`

### bot_log

是否启用`botpy`日志
是否启用`botpy`日志,默认为`True`

`True` 启用
`None` 禁用 拓展
`False` 禁用 拓展+控制台输出

### ext_handlers

日志Handler拓展,为True使用默认拓展,False不添加拓展,可用list添加多个拓展
日志Handler拓展,`True`使用默认拓展,`False`不添加拓展,可用list添加多个拓展。默认为`True`

[默认拓展](botpy/logging.py)
[默认拓展](./botpy/logging.py)

```python
import os
import logging
from logging.handlers import TimedRotatingFileHandler

DEFAULT_FILE_HANDLER={
DEFAULT_FILE_HANDLER = {
# 要实例化的Handler
"handler": TimedRotatingFileHandler,
# 可选 Default to DEFAULT_FILE_FORMAT
"format": "%(asctime)s\t[%(levelname)s]\t(%(filename)s:%(lineno)s)%(funcName)s\t%(message)s",
# 可选 Default to DEBUG
"level": logging.DEBUG,
# 可选,其中如有 %(name)s 会在实例化阶段填入相应的日志name
"filename": os.path.join(os.getcwd(), "%(name)s.log"),
# 以下是Handler相关参数
"when": "D",
"backupCount": 7,
"encoding": "utf-8"
"encoding": "utf-8",
# *特殊* 对于filename参数,其中如有 %(name)s 会在实例化阶段填入相应的日志name
"filename": os.path.join(os.getcwd(), "%(name)s.log"),
}
```

Expand All @@ -224,7 +226,7 @@ botpy.Client(ext_handlers=DEFAULT_FILE_HANDLER)

### log_config

该参数将传入`logging.config.dictConfig`(内置logging而非botpy.logging),如果为.json/.yaml文件路径将从文件中读取配置
该参数将传入`logging.config.dictConfig`(内置logging而非botpy.logging),如果为.json/.yaml文件路径将从文件中读取配置,无默认值

# 参与开发

Expand Down
4 changes: 2 additions & 2 deletions botpy/client.py
Expand Up @@ -47,7 +47,7 @@ def __init__(
log_config: 日志配置,可以为dict或.json/.yaml文件路径,会从文件中读取(logging.config.dictConfig)。Default to None(不做更改)
log_format: 控制台输出格式(logging.basicConfig(format=))。Default to None(不做更改)
log_level: 控制台输出level。Default to None(不做更改),
bot_log: bot_log: 是否启用bot日志 None/禁用拓展 False/全部禁用。Default to True
bot_log: bot_log: bot_log: 是否启用bot日志 True/启用 None/禁用拓展 False/禁用拓展+控制台输出
ext_handlers: ext_handlers: 额外的handler,格式参考 logging.DEFAULT_FILE_HANDLER。Default to True(使用默认追加handler)
"""
self.intents: int = intents.value
Expand Down Expand Up @@ -283,4 +283,4 @@ async def _run_event(
try:
await self.on_error(event_name, *args, **kwargs)
except asyncio.CancelledError:
pass
pass
24 changes: 13 additions & 11 deletions botpy/logging.py
Expand Up @@ -17,6 +17,8 @@
"CRITICAL": "red",
}

DEFAULT_LOGGER_NAME = "botpy"

DEFAULT_PRINT_FORMAT = "\033[1;33m[%(levelname)s]\t(%(filename)s:%(lineno)s)%(funcName)s\t\033[0m%(message)s"
DEFAULT_FILE_FORMAT = "%(asctime)s\t[%(levelname)s]\t(%(filename)s:%(lineno)s)%(funcName)s\t%(message)s"
logging.basicConfig(format=DEFAULT_PRINT_FORMAT)
Expand All @@ -28,12 +30,12 @@
"format": "%(asctime)s\t[%(levelname)s]\t(%(filename)s:%(lineno)s)%(funcName)s\t%(message)s",
# 可选 Default to DEBUG
"level": logging.DEBUG,
# 可选,其中如有 %(name)s 会在实例化阶段填入相应的日志name
"filename": os.path.join(os.getcwd(), "%(name)s.log"),
# 以下是Handler相关参数
"when": "D",
"backupCount": 7,
"encoding": "utf-8"
"encoding": "utf-8",
# *特殊* 对于filename参数,其中如有 %(name)s 会在实例化阶段填入相应的日志name
"filename": os.path.join(os.getcwd(), "%(name)s.log"),
}

# 存放已经获取的Logger
Expand All @@ -46,7 +48,7 @@
os.system("")


def get_handler(handler, name=""):
def get_handler(handler, name=DEFAULT_LOGGER_NAME):
"""
将handler字典实例化
:param handler: handler配置
Expand Down Expand Up @@ -74,7 +76,7 @@ def get_logger(name=None):
global logs

if not name:
name = "botpy"
name = DEFAULT_LOGGER_NAME
if name in logs:
return logs[name]

Expand Down Expand Up @@ -108,7 +110,7 @@ def configure_logging(
:param config: logging.config.dictConfig
:param _format: logging.basicConfig(format=_format)
:param level: 控制台输出level
:param bot_log: 是否启用bot日志 None/禁用拓展 False/全部禁用
:param bot_log: 是否启用bot日志 True/启用 None/禁用拓展 False/禁用拓展+控制台输出
:param ext_handlers: 额外的handler,格式参考 DEFAULT_FILE_HANDLER。Default to True(使用默认handler)
:param force: 是否在已追加handler(_ext_handlers)不为空时继续追加(避免因多次实例化Client类导致重复添加)
"""
Expand All @@ -135,16 +137,16 @@ def configure_logging(
if _format is not None:
logging.basicConfig(format=_format)

for name, logger in logs.items():
if level is not None:
if level is not None:
for name, logger in logs.items():
logger.setLevel(level)

if not bot_log:
logger = logging.getLogger("botpy")
logger = logging.getLogger(DEFAULT_LOGGER_NAME)
if bot_log is False:
logger.propagate = False
if "botpy" in logs:
logs.pop("botpy")
if DEFAULT_LOGGER_NAME in logs:
logs.pop(DEFAULT_LOGGER_NAME)

logger.handlers = []

Expand Down

0 comments on commit 9162c24

Please sign in to comment.