🔀 Merge pull request #15

fix #14 logger escape
This commit is contained in:
Ju4tCode 2020-09-30 18:08:03 +08:00 committed by GitHub
commit d823b1733b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 3 deletions

View File

@ -109,6 +109,7 @@ def get_bots() -> Union[NoReturn, Dict[str, Bot]]:
from nonebot.sched import scheduler from nonebot.sched import scheduler
from nonebot.utils import escape_tag
from nonebot.config import Env, Config from nonebot.config import Env, Config
from nonebot.log import logger, default_filter from nonebot.log import logger, default_filter
from nonebot.adapters.cqhttp import Bot as CQBot 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}") _env_file=_env_file or f".env.{env.environment}")
default_filter.level = "DEBUG" if config.debug else "INFO" default_filter.level = "DEBUG" if config.debug else "INFO"
logger.opt( logger.opt(colors=True).debug(
colors=True).debug(f"Loaded <y><b>Config</b></y>: {config.dict()}") f"Loaded <y><b>Config</b></y>: {escape_tag(str(config.dict()))}")
DriverClass: Type[Driver] = getattr( DriverClass: Type[Driver] = getattr(
importlib.import_module(config.driver), "Driver") importlib.import_module(config.driver), "Driver")

View File

@ -6,6 +6,7 @@ from datetime import datetime
from nonebot.log import logger from nonebot.log import logger
from nonebot.rule import TrieRule from nonebot.rule import TrieRule
from nonebot.utils import escape_tag
from nonebot.matcher import matchers from nonebot.matcher import matchers
from nonebot.typing import Set, Type, Union, NoReturn from nonebot.typing import Set, Type, Union, NoReturn
from nonebot.typing import Bot, Event, Matcher, PreProcessor 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 += f"@[群:{event.group_id}]:"
log_msg += ' "' + "".join( log_msg += ' "' + "".join(
map(lambda x: str(x) if x.type == "text" else f"<le>{x!s}</le>", map(
lambda x: escape_tag(str(x))
if x.type == "text" else f"<le>{escape_tag(str(x))}</le>",
event.message)) + '"' # type: ignore event.message)) + '"' # type: ignore
elif event.type == "notice": elif event.type == "notice":
log_msg += f"Notice {event.raw_event}" log_msg += f"Notice {event.raw_event}"

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import re
import json import json
import asyncio import asyncio
import dataclasses import dataclasses
@ -9,6 +10,10 @@ from functools import wraps, partial
from nonebot.typing import Any, Callable, Awaitable, overrides from nonebot.typing import Any, Callable, Awaitable, overrides
def escape_tag(s: str) -> str:
return re.sub(r"</?((?:[fb]g\s)?[^<>\s]*)>", r"\\\g<0>", s)
def run_sync(func: Callable[..., Any]) -> Callable[..., Awaitable[Any]]: def run_sync(func: Callable[..., Any]) -> Callable[..., Awaitable[Any]]:
""" """
:说明: :说明: