From f0e3bec3d8e440125e955351c5dc945ed252a8eb Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Thu, 10 Jun 2021 23:53:29 +0800 Subject: [PATCH] :wheelchair: improve sentry disable logic --- src/plugins/nonebot_plugin_sentry/__init__.py | 31 +++++++++++-------- src/plugins/nonebot_plugin_sentry/config.py | 4 +-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/plugins/nonebot_plugin_sentry/__init__.py b/src/plugins/nonebot_plugin_sentry/__init__.py index 45b1115..ba67b87 100644 --- a/src/plugins/nonebot_plugin_sentry/__init__.py +++ b/src/plugins/nonebot_plugin_sentry/__init__.py @@ -4,7 +4,7 @@ @Author : yanyongyu @Date : 2020-11-23 18:44:25 @LastEditors : yanyongyu -@LastEditTime : 2021-03-09 16:39:07 +@LastEditTime : 2021-06-10 23:49:34 @Description : None @GitHub : https://github.com/yanyongyu """ @@ -21,16 +21,6 @@ driver = get_driver() global_config = driver.config config = Config(**global_config.dict()) -assert config.sentry_dsn, "Sentry DSN must provided!" - -sentry_sdk.init(**{ - key[7:]: value - for key, value in config.dict().items() - if key != "sentry_environment" -}, - environment=config.sentry_environment or driver.env, - default_integrations=False) - class Filter: @@ -42,5 +32,20 @@ class Filter: return record["level"].no >= levelno -logger.add(EventHandler("ERROR"), filter=Filter("ERROR")) -logger.add(BreadcrumbHandler("INFO"), filter=Filter("INFO")) +def init(config: Config): + sentry_config = { + key[7:]: value + for key, value in config.dict().items() + if key != "sentry_environment" + } + sentry_sdk.init(**sentry_config, + environment=config.sentry_environment or + driver.env.environment, + default_integrations=False) + + logger.add(EventHandler("ERROR"), filter=Filter("ERROR")) + logger.add(BreadcrumbHandler("INFO"), filter=Filter("INFO")) + + +if config.sentry_dsn: + init(config) diff --git a/src/plugins/nonebot_plugin_sentry/config.py b/src/plugins/nonebot_plugin_sentry/config.py index 1b7b07d..88999dc 100644 --- a/src/plugins/nonebot_plugin_sentry/config.py +++ b/src/plugins/nonebot_plugin_sentry/config.py @@ -4,7 +4,7 @@ @Author : yanyongyu @Date : 2020-11-23 18:44:18 @LastEditors : yanyongyu -@LastEditTime : 2021-06-03 23:26:46 +@LastEditTime : 2021-06-10 23:48:15 @Description : None @GitHub : https://github.com/yanyongyu """ @@ -18,7 +18,7 @@ from pydantic import Field, validator, BaseSettings class Config(BaseSettings): - sentry_dsn: str + sentry_dsn: Optional[str] sentry_debug: bool = False sentry_release: Optional[str] = None sentry_environment: Optional[str] = None