|
13 | 13 | from loguru import logger |
14 | 14 |
|
15 | 15 | from app.tools.path_utils import get_app_root |
16 | | -from app.tools.config import configure_logging |
| 16 | +from app.tools.config import ( |
| 17 | + configure_logging, |
| 18 | + set_posthog_client, |
| 19 | + create_sentry_before_send_filter, |
| 20 | +) |
17 | 21 | from app.tools.settings_default import manage_settings_file |
18 | | -from app.tools.settings_access import readme_settings_async, get_or_create_user_id, update_settings |
| 22 | +from app.tools.settings_access import readme_settings_async, get_or_create_user_id |
19 | 23 | from app.core.app_init import calculate_total_draw_counts |
20 | 24 | from app.tools.variable import ( |
21 | 25 | APP_QUIT_ON_LAST_WINDOW_CLOSED, |
|
29 | 33 | PROCESS_EXIT_WAIT_SECONDS, |
30 | 34 | POSTHOG_API_KEY, |
31 | 35 | POSTHOG_HOST, |
32 | | - set_posthog_client, |
33 | 36 | ) |
34 | 37 | from app.core.single_instance import ( |
35 | 38 | check_single_instance, |
|
53 | 56 | # ================================================== |
54 | 57 |
|
55 | 58 |
|
56 | | -def create_sentry_before_send_filter(): |
57 | | - """创建 Sentry 事件过滤器 |
58 | | -
|
59 | | - 过滤掉不需要上报的错误,如第三方库错误和常见的无害错误 |
60 | | - """ |
61 | | - |
62 | | - def before_send(event, hint): |
63 | | - # 1. 检查是否有堆栈信息 |
64 | | - has_stacktrace = False |
65 | | - if "exception" in event: |
66 | | - values = event.get("exception", {}).get("values", []) |
67 | | - for val in values: |
68 | | - if val.get("stacktrace"): |
69 | | - has_stacktrace = True |
70 | | - break |
71 | | - |
72 | | - if not has_stacktrace and "threads" in event: |
73 | | - values = event.get("threads", {}).get("values", []) |
74 | | - for val in values: |
75 | | - if val.get("stacktrace"): |
76 | | - has_stacktrace = True |
77 | | - break |
78 | | - |
79 | | - # 检查 loguru 的 log_record |
80 | | - log_record = hint.get("log_record") |
81 | | - if log_record: |
82 | | - if getattr(log_record, "exception", None): |
83 | | - has_stacktrace = True |
84 | | - elif hasattr(log_record, "extra") and log_record.extra.get("exc_info"): |
85 | | - has_stacktrace = True |
86 | | - |
87 | | - # 如果没有堆栈信息,且是错误/严重级别,则丢弃 (logger.info 等低级别不会被丢弃,除非 event_level 设置) |
88 | | - if not has_stacktrace and event.get("level") in ("error", "fatal"): |
89 | | - return None |
90 | | - |
91 | | - # 2. 过滤特定的错误类型或模块 |
92 | | - if "exception" in event: |
93 | | - exceptions = event.get("exception", {}).get("values", []) |
94 | | - for exc in exceptions: |
95 | | - module = exc.get("module", "") |
96 | | - type_ = exc.get("type", "") |
97 | | - value = exc.get("value", "") |
98 | | - |
99 | | - # 过滤 Qt 常见无害错误 (通常是由于对象在 C++ 侧已销毁但 Python 侧仍在尝试访问) |
100 | | - if type_ == "RuntimeError" and ( |
101 | | - "Internal C++ object" in str(value) |
102 | | - or "has been deleted" in str(value) |
103 | | - ): |
104 | | - return None |
105 | | - |
106 | | - # 过滤 COM 相关 |
107 | | - if type_ == "COMError" and "没有注册类" in str(value): |
108 | | - return None |
109 | | - |
110 | | - return event |
111 | | - |
112 | | - return before_send |
113 | | - |
114 | | - |
115 | 59 | def initialize_sentry(): |
116 | 60 | """初始化 Sentry 错误监控系统""" |
117 | 61 | sentry_sdk.init( |
|
0 commit comments