mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-24 00:55:07 +08:00
✨ Feature: 优化调用栈识别 (#2644)
This commit is contained in:
parent
5e72461391
commit
30d3c1bbce
@ -13,6 +13,7 @@ FrontMatter:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
@ -45,6 +46,7 @@ logger: "Logger" = loguru.logger
|
|||||||
# logger.addHandler(default_handler)
|
# logger.addHandler(default_handler)
|
||||||
|
|
||||||
|
|
||||||
|
# https://loguru.readthedocs.io/en/stable/overview.html#entirely-compatible-with-standard-logging
|
||||||
class LoguruHandler(logging.Handler): # pragma: no cover
|
class LoguruHandler(logging.Handler): # pragma: no cover
|
||||||
"""logging 与 loguru 之间的桥梁,将 logging 的日志转发到 loguru。"""
|
"""logging 与 loguru 之间的桥梁,将 logging 的日志转发到 loguru。"""
|
||||||
|
|
||||||
@ -54,8 +56,8 @@ class LoguruHandler(logging.Handler): # pragma: no cover
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
level = record.levelno
|
level = record.levelno
|
||||||
|
|
||||||
frame, depth = sys._getframe(6), 6
|
frame, depth = inspect.currentframe(), 0
|
||||||
while frame and frame.f_code.co_filename == logging.__file__:
|
while frame and (depth == 0 or frame.f_code.co_filename == logging.__file__):
|
||||||
frame = frame.f_back
|
frame = frame.f_back
|
||||||
depth += 1
|
depth += 1
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ def get_matcher_module(depth: int = 1) -> Optional[ModuleType]: # pragma: no co
|
|||||||
return (source := get_matcher_source(depth + 1)) and source.module
|
return (source := get_matcher_source(depth + 1)) and source.module
|
||||||
|
|
||||||
|
|
||||||
def get_matcher_source(depth: int = 1) -> Optional[MatcherSource]:
|
def get_matcher_source(depth: int = 0) -> Optional[MatcherSource]:
|
||||||
"""获取事件响应器定义所在源码信息。
|
"""获取事件响应器定义所在源码信息。
|
||||||
|
|
||||||
参数:
|
参数:
|
||||||
@ -85,7 +85,14 @@ def get_matcher_source(depth: int = 1) -> Optional[MatcherSource]:
|
|||||||
current_frame = inspect.currentframe()
|
current_frame = inspect.currentframe()
|
||||||
if current_frame is None:
|
if current_frame is None:
|
||||||
return None
|
return None
|
||||||
frame = inspect.getouterframes(current_frame)[depth + 1].frame
|
|
||||||
|
frame = current_frame
|
||||||
|
d = depth + 1
|
||||||
|
while d > 0:
|
||||||
|
frame = frame.f_back
|
||||||
|
if frame is None:
|
||||||
|
raise ValueError("Depth out of range")
|
||||||
|
d -= 1
|
||||||
|
|
||||||
module_name = (module := inspect.getmodule(frame)) and module.__name__
|
module_name = (module := inspect.getmodule(frame)) and module.__name__
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user