Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加日志输出到文件方便检查,统一日志格式及配置 #213

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
import itertools
from src import BiliUser

log_file = os.path.join(os.path.dirname(__file__), "log/fansMedalHelper_{time:YYYY-MM-DD}.log")
log_format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"

logger.remove()
logger.add(
sys.stdout,
format=log_format,
backtrace=True,
diagnose=True,
level="INFO"
)
log = logger.bind(user="B站粉丝牌助手")
__VERSION__ = "0.3.8"

Expand All @@ -25,6 +36,16 @@

with open("users.yaml", "r", encoding="utf-8") as f:
users = yaml.load(f, Loader=yaml.FullLoader)
if users.get("WRITE_LOG_FILE"):
logger.add(
log_file if users["WRITE_LOG_FILE"] == True else users["WRITE_LOG_FILE"],
format=log_format,
backtrace=True,
diagnose=True,
rotation="00:00",
retention="30 days",
level="DEBUG"
)
assert users["ASYNC"] in [0, 1], "ASYNC参数错误"
assert users["LIKE_CD"] >= 0, "LIKE_CD参数错误"
# assert users['SHARE_CD'] >= 0, "SHARE_CD参数错误"
Expand Down
1 change: 1 addition & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __init__(self, u: BiliUser, s: ClientSession):
self.session = s

def __check_response(self, resp: dict) -> dict:
logger.trace(resp)
if resp["code"] != 0 or ("mode_info" in resp["data"] and resp["message"] != ""):
raise BiliApiError(resp["code"], resp["message"])
return resp["data"]
Expand Down
10 changes: 0 additions & 10 deletions src/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

logger.remove()
logger.add(
sys.stdout,
colorize=True,
format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> <blue> {extra[user]} </blue> <level>{message}</level>",
backtrace=True,
diagnose=True,
)


class BiliUser:
def __init__(self, access_token: str, whiteUIDs: str = '', bannedUIDs: str = '', config: dict = {}):
from .api import BiliApi
Expand Down
6 changes: 6 additions & 0 deletions users.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ USERS:
# 井号后为注释 井号前后必须有空格!井号前后必须有空格!井号前后必须有空格!
# 冒号后面也要有空格!冒号前面也要有空格!冒号前面也要有空格!
# 英文冒号,英文逗号!英文逗号!英文逗号!
WRITE_LOG_FILE: False
# 是否写入日志文件
# 填写True时使用默认目录(运行目录log文件夹下)
# 填写绝对路径时写入指定log文件位置
# 不填或填写False则不写入日志文件

CRON: # 0 0 * * *
# 这里是 cron 表达式, 第一个参数是分钟, 第二个参数是小时
# 例如每天凌晨0点0分执行一次为 0 0 * * *
Expand Down