Skip to content

Commit

Permalink
✨增加日志保存
Browse files Browse the repository at this point in the history
  • Loading branch information
JustUndertaker committed Oct 18, 2022
1 parent 62adb8e commit c1b80b6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ access_token = ""
# 日志显示等级
log_level = "DEBUG"

# 日志保存天数
log_days = 10

# 事件过滤列表,列表填tpye的数字
msg_filter = []

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ dmypy.json
.vscode/
*.code-workspace
file_cache/
logs/
3 changes: 2 additions & 1 deletion ntchat_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ntchat_client.cache import scheduler_job
from ntchat_client.client import Client
from ntchat_client.config import Config, Env
from ntchat_client.log import default_filter, logger
from ntchat_client.log import default_filter, log_init, logger


def tick():
Expand All @@ -21,6 +21,7 @@ def init():
env = Env()
config = Config(_common_config=env.dict())
default_filter.level = config.log_level
log_init(config.log_days)
logger.info(f"Current <y><b>Env: {env.environment}</b></y>")
logger.debug(f"Loaded <y><b>Config</b></y>: {str(config.dict())}")
logger.info("初始化ntchat_client...")
Expand Down
2 changes: 2 additions & 0 deletions ntchat_client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class Config(BaseConfig):
"""密钥"""
log_level: Union[int, str] = "INFO"
"""默认日志等级"""
log_days: int = 10
"""日志保存天数"""
msg_filter: Set[int] = {}
"""事件过滤列表"""
report_self: bool = False
Expand Down
60 changes: 60 additions & 0 deletions ntchat_client/log.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import sys
from pathlib import Path
from typing import Union

from loguru._logger import Core, Logger
Expand Down Expand Up @@ -69,3 +70,62 @@ def emit(self, record):
filter=default_filter,
format=default_format,
)


def log_init(log_days: int):
"""日志初始化"""
Path("./logs/info").mkdir(parents=True, exist_ok=True)
Path("./logs/debug").mkdir(parents=True, exist_ok=True)
Path("./logs/error").mkdir(parents=True, exist_ok=True)
# 日志文件记录格式
file_format = (
"<g>{time:MM-DD HH:mm:ss}</g> "
"[<lvl>{level}</lvl>] "
"<c><u>{name}</u></c> | "
"{message}"
)

# 错误日志文件记录格式
error_format = (
"<g>{time:MM-DD HH:mm:ss}</g> "
"[<lvl>{level}</lvl>] "
"[<c><u>{name}</u></c>] | "
"<c>{function}:{line}</c>| "
"{message}"
)

# info文件
info_path = "./logs/info/"
logger.add(
info_path + "{time:YYYY-MM-DD}.log",
rotation="00:00",
retention=f"{log_days} days",
level="INFO",
format=file_format,
filter=default_filter,
encoding="utf-8",
)

# debug文件
debug_path = "./logs/debug/"
logger.add(
debug_path + "{time:YYYY-MM-DD}.log",
rotation="00:00",
retention=f"{log_days} days",
level="DEBUG",
format=file_format,
filter=default_filter,
encoding="utf-8",
)

# error文件
error_path = "./logs/error/"
logger.add(
error_path + "{time:YYYY-MM-DD}.log",
rotation="00:00",
retention=f"{log_days} days",
level="ERROR",
format=error_format,
filter=default_filter,
encoding="utf-8",
)

0 comments on commit c1b80b6

Please sign in to comment.