我尝试使程序包记录器正常工作,但是由于我看不见的原因,即使添加了处理程序,日志级别也会被忽略。 这是我的代码
import logging
import sys
import colorlog
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
from in.er.hit import InerhitA, InerhitB
from .utils.exitcodes import ExitCodes
from .utils.constants import EnvVariables
log = logging.getLogger(__name__)
class PackageMainClass(InerhitA, InerhitB):
def __init__(self, *args, **kwargs):
self.debug = kwargs.get('debug')
self.__setup_logging()
super().__init__(*args, **kwargs)
def __setup_logging(self):
formatter = colorlog.ColoredFormatter(
"[{asctime}] [{levelname}] {name}: {message}",
datefmt="%d.%m.%Y %H:%M:%S",
style="{",
)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(formatter)
stdout_handler.setLevel(logging.DEBUG if self.debug else logging.INFO)
logging.getLogger(__package__).addHandler(stdout_handler)
if EnvVariables.get("SENTRY_DNS") and EnvVariables.get(
"SENTRY_PROJECT"
):
sentry_logging = LoggingIntegration(
level=logging.INFO, event_level=logging.ERROR
)
sentry_sdk.init(
dsn="https://{}@sentry.io/{}".format(
EnvVariables.get("SENTRY_DNS"),
EnvVariables.get("SENTRY_PROJECT"),
),
integrations=[sentry_logging],
ignore_errors=[KeyboardInterrupt]
)
async def start(self, *args, **kwargs):
# Todo
print(logging.getLogger().hasHandlers()) # => True
print(logging.getEffectiveLevel()) # => 30 aka warning
return await super().start(*args, **kwargs)
我要监督什么?同样,在修复记录器后,是否真的为 whole 软件包设置了记录器?
注:该代码由black和flask8格式化。另外,我认为自己是python新手,因此当我的代码不正确时,请给出建议>。<。