2022-01-18 23:46:10 +08:00
|
|
|
|
"""本模块定义了 NoneBot 的日志记录 Logger。
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2022-01-12 19:41:42 +08:00
|
|
|
|
NoneBot 使用 [`loguru`][loguru] 来记录日志信息。
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2023-06-01 14:18:16 +08:00
|
|
|
|
自定义 logger 请参考 [自定义日志](https://nonebot.dev/docs/appendices/log)
|
2022-01-18 16:12:12 +08:00
|
|
|
|
以及 [`loguru`][loguru] 文档。
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2022-01-12 19:41:42 +08:00
|
|
|
|
[loguru]: https://github.com/Delgan/loguru
|
2022-01-16 11:30:09 +08:00
|
|
|
|
|
|
|
|
|
FrontMatter:
|
2024-10-22 10:33:48 +08:00
|
|
|
|
mdx:
|
|
|
|
|
format: md
|
2022-01-16 11:30:09 +08:00
|
|
|
|
sidebar_position: 7
|
|
|
|
|
description: nonebot.log 模块
|
2020-08-20 15:07:05 +08:00
|
|
|
|
"""
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
|
|
|
|
import sys
|
2024-04-17 17:24:38 +08:00
|
|
|
|
import inspect
|
2020-08-27 13:27:42 +08:00
|
|
|
|
import logging
|
2022-09-09 11:52:57 +08:00
|
|
|
|
from typing import TYPE_CHECKING
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2021-05-21 15:15:04 +08:00
|
|
|
|
import loguru
|
2021-07-28 16:32:50 +08:00
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
# avoid sphinx autodoc resolve annotation failed
|
|
|
|
|
# because loguru module do not have `Logger` class actually
|
2022-09-09 11:52:57 +08:00
|
|
|
|
from loguru import Logger, Record
|
2022-01-14 12:32:03 +08:00
|
|
|
|
|
2020-08-26 22:54:58 +08:00
|
|
|
|
# logger = logging.getLogger("nonebot")
|
2021-07-28 16:32:50 +08:00
|
|
|
|
logger: "Logger" = loguru.logger
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""NoneBot 日志记录器对象。
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2022-01-12 19:41:42 +08:00
|
|
|
|
默认信息:
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2022-01-16 11:30:09 +08:00
|
|
|
|
- 格式: `[%(asctime)s %(name)s] %(levelname)s: %(message)s`
|
|
|
|
|
- 等级: `INFO` ,根据 `config.log_level` 配置改变
|
|
|
|
|
- 输出: 输出至 stdout
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2020-08-20 15:07:05 +08:00
|
|
|
|
from nonebot.log import logger
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2020-08-20 15:07:05 +08:00
|
|
|
|
"""
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
2020-08-26 22:54:58 +08:00
|
|
|
|
# default_handler = logging.StreamHandler(sys.stdout)
|
|
|
|
|
# default_handler.setFormatter(
|
|
|
|
|
# logging.Formatter("[%(asctime)s %(name)s] %(levelname)s: %(message)s"))
|
|
|
|
|
# logger.addHandler(default_handler)
|
|
|
|
|
|
2020-08-27 13:27:42 +08:00
|
|
|
|
|
2024-04-17 17:24:38 +08:00
|
|
|
|
# https://loguru.readthedocs.io/en/stable/overview.html#entirely-compatible-with-standard-logging
|
2021-12-16 17:28:57 +08:00
|
|
|
|
class LoguruHandler(logging.Handler): # pragma: no cover
|
2022-09-09 11:52:57 +08:00
|
|
|
|
"""logging 与 loguru 之间的桥梁,将 logging 的日志转发到 loguru。"""
|
|
|
|
|
|
|
|
|
|
def emit(self, record: logging.LogRecord):
|
2020-08-27 13:27:42 +08:00
|
|
|
|
try:
|
|
|
|
|
level = logger.level(record.levelname).name
|
|
|
|
|
except ValueError:
|
|
|
|
|
level = record.levelno
|
|
|
|
|
|
2024-04-17 17:24:38 +08:00
|
|
|
|
frame, depth = inspect.currentframe(), 0
|
|
|
|
|
while frame and (depth == 0 or frame.f_code.co_filename == logging.__file__):
|
2020-08-27 13:27:42 +08:00
|
|
|
|
frame = frame.f_back
|
|
|
|
|
depth += 1
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
logger.opt(depth=depth, exception=record.exc_info).log(
|
|
|
|
|
level, record.getMessage()
|
|
|
|
|
)
|
2020-08-27 13:27:42 +08:00
|
|
|
|
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def default_filter(record: "Record"):
|
|
|
|
|
"""默认的日志过滤器,根据 `config.log_level` 配置改变日志等级。"""
|
|
|
|
|
log_level = record["extra"].get("nonebot_log_level", "INFO")
|
|
|
|
|
levelno = logger.level(log_level).no if isinstance(log_level, str) else log_level
|
|
|
|
|
return record["level"].no >= levelno
|
|
|
|
|
|
|
|
|
|
|
2022-01-18 16:12:12 +08:00
|
|
|
|
default_format: str = (
|
2020-08-27 16:43:58 +08:00
|
|
|
|
"<g>{time:MM-DD HH:mm:ss}</g> "
|
|
|
|
|
"[<lvl>{level}</lvl>] "
|
|
|
|
|
"<c><u>{name}</u></c> | "
|
2020-08-27 13:27:42 +08:00
|
|
|
|
# "<c>{function}:{line}</c>| "
|
2021-11-22 23:21:26 +08:00
|
|
|
|
"{message}"
|
|
|
|
|
)
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""默认日志格式"""
|
2022-09-09 11:52:57 +08:00
|
|
|
|
|
|
|
|
|
logger.remove()
|
2021-11-22 23:21:26 +08:00
|
|
|
|
logger_id = logger.add(
|
|
|
|
|
sys.stdout,
|
2021-12-28 14:33:03 +08:00
|
|
|
|
level=0,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
diagnose=False,
|
|
|
|
|
filter=default_filter,
|
|
|
|
|
format=default_format,
|
|
|
|
|
)
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""默认日志处理器 id"""
|
2022-01-18 16:12:12 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
__autodoc__ = {"logger_id": False}
|