From 921acff09e3e8cf6fed6da7d96b9ccff302f8dd6 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Wed, 30 Sep 2020 18:01:31 +0800 Subject: [PATCH] :bug: fix logger escape tag #14 --- nonebot/__init__.py | 5 +++-- nonebot/message.py | 5 ++++- nonebot/utils.py | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/nonebot/__init__.py b/nonebot/__init__.py index 6f93bdae..213b63d2 100644 --- a/nonebot/__init__.py +++ b/nonebot/__init__.py @@ -109,6 +109,7 @@ def get_bots() -> Union[NoReturn, Dict[str, Bot]]: from nonebot.sched import scheduler +from nonebot.utils import escape_tag from nonebot.config import Env, Config from nonebot.log import logger, default_filter from nonebot.adapters.cqhttp import Bot as CQBot @@ -155,8 +156,8 @@ def init(*, _env_file: Optional[str] = None, **kwargs): _env_file=_env_file or f".env.{env.environment}") default_filter.level = "DEBUG" if config.debug else "INFO" - logger.opt( - colors=True).debug(f"Loaded Config: {config.dict()}") + logger.opt(colors=True).debug( + f"Loaded Config: {escape_tag(str(config.dict()))}") DriverClass: Type[Driver] = getattr( importlib.import_module(config.driver), "Driver") diff --git a/nonebot/message.py b/nonebot/message.py index b6c3d888..f3a69e3a 100644 --- a/nonebot/message.py +++ b/nonebot/message.py @@ -6,6 +6,7 @@ from datetime import datetime from nonebot.log import logger from nonebot.rule import TrieRule +from nonebot.utils import escape_tag from nonebot.matcher import matchers from nonebot.typing import Set, Type, Union, NoReturn from nonebot.typing import Bot, Event, Matcher, PreProcessor @@ -64,7 +65,9 @@ async def handle_event(bot: Bot, event: Event): log_msg += f"@[群:{event.group_id}]:" log_msg += ' "' + "".join( - map(lambda x: str(x) if x.type == "text" else f"{x!s}", + map( + lambda x: escape_tag(str(x)) + if x.type == "text" else f"{escape_tag(str(x))}", event.message)) + '"' # type: ignore elif event.type == "notice": log_msg += f"Notice {event.raw_event}" diff --git a/nonebot/utils.py b/nonebot/utils.py index 06bdaaa4..b442f6bd 100644 --- a/nonebot/utils.py +++ b/nonebot/utils.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import re import json import asyncio import dataclasses @@ -9,6 +10,10 @@ from functools import wraps, partial from nonebot.typing import Any, Callable, Awaitable, overrides +def escape_tag(s: str) -> str: + return re.sub(r"\s]*)>", r"\\\g<0>", s) + + def run_sync(func: Callable[..., Any]) -> Callable[..., Awaitable[Any]]: """ :说明: