From e2fd28f903533e679c6ca3d96dc3879cd0077e32 Mon Sep 17 00:00:00 2001 From: Kartik Ohri Date: Tue, 16 Feb 2021 14:06:55 +0530 Subject: [PATCH 1/2] Use sentry_sdk instead of raven --- brainzutils/flask/loggers.py | 39 +++++++----------------------------- requirements.txt | 2 +- 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/brainzutils/flask/loggers.py b/brainzutils/flask/loggers.py index bb50940..7460c11 100644 --- a/brainzutils/flask/loggers.py +++ b/brainzutils/flask/loggers.py @@ -1,24 +1,12 @@ # pylint: disable=invalid-name import logging from logging.handlers import RotatingFileHandler, SMTPHandler -import raven -import raven.base -import raven.contrib.flask -import raven.transport.threaded_requests - -class MissingRavenClient(raven.Client): - """Raven client class that is used as a placeholder. - This is done to make sure that calls to functions in the client don't fail - even if the client is not initialized. Sentry server might be missing, but - we don't want to check if it actually exists in every place exception is - captured. - """ - captureException = lambda self, *args, **kwargs: None - captureMessage = lambda self, *args, **kwargs: None - - -_sentry_client = MissingRavenClient() # type: raven.Client +import sentry_sdk +from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration +from sentry_sdk.integrations.logging import LoggingIntegration +from sentry_sdk.integrations.redis import RedisIntegration +from sentry_sdk.integrations.flask import FlaskIntegration def add_file_handler(app, filename, max_bytes=512 * 1024, backup_count=100): @@ -62,20 +50,7 @@ def add_sentry(app, dsn, level=logging.WARNING, **options): Sentry is a realtime event logging and aggregation platform. Additional information about it is available at https://sentry.readthedocs.org/. - - We use Raven as a client for Sentry. More info about Raven is available at - https://raven.readthedocs.org/. """ - app.config["SENTRY_TRANSPORT"] = raven.transport.threaded_requests.ThreadedRequestsHTTPTransport app.config["SENTRY_CONFIG"] = options - global _sentry_client - _sentry_client = raven.contrib.flask.Sentry( - app=app, - dsn=dsn, - level=level, - logging=True, - ) - - -def get_sentry_client(): - return _sentry_client + sentry_sdk.init(dsn, integrations=[LoggingIntegration(level=level), FlaskIntegration(), RedisIntegration(), + SqlalchemyIntegration()]) diff --git a/requirements.txt b/requirements.txt index 565878a..a69cca3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ Jinja2>=2.11.2 werkzeug>=1.0.1 Flask-DebugToolbar>=0.11.0 Flask-UUID>=0.2 -raven[flask]>=6.10.0 +sentry-sdk[flask]>=0.20.2 certifi redis>=3.5,<4.0 msgpack-python==0.5.6 From 3ea6d1aa606bdfb17cbdf91a37168dc40a62a00f Mon Sep 17 00:00:00 2001 From: Kartik Ohri Date: Wed, 17 Feb 2021 19:47:17 +0530 Subject: [PATCH 2/2] Remove email logging handler --- brainzutils/flask/__init__.py | 17 ----------------- brainzutils/flask/loggers.py | 25 ------------------------- 2 files changed, 42 deletions(-) diff --git a/brainzutils/flask/__init__.py b/brainzutils/flask/__init__.py index a74f65d..5d31637 100644 --- a/brainzutils/flask/__init__.py +++ b/brainzutils/flask/__init__.py @@ -43,7 +43,6 @@ def init_debug_toolbar(self): def init_loggers(self, file_config=None, - email_config=None, sentry_config=None): """This method attaches loggers to the Flask app. @@ -63,20 +62,6 @@ def init_loggers(self, 'backup_count': 100, # optional } - email_config (dict): Dictionary with the following structure:: - - { - 'mail_server': 'localhost', - 'mail_port': 25, - 'mail_from_host': 'example.org', - 'log_email_recipients': [ - 'user1@example.org', - 'user2@example.org', - ], - 'log_email_topic': 'Error occurred', - 'level': 'ERROR', # optional - } - sentry_config (dict): Dictionary with the following structure:: { @@ -86,7 +71,5 @@ def init_loggers(self, """ if file_config: loggers.add_file_handler(self, **file_config) - if email_config: - loggers.add_email_handler(self, **email_config) if sentry_config: loggers.add_sentry(self, **sentry_config) diff --git a/brainzutils/flask/loggers.py b/brainzutils/flask/loggers.py index 7460c11..e82f6bc 100644 --- a/brainzutils/flask/loggers.py +++ b/brainzutils/flask/loggers.py @@ -20,31 +20,6 @@ def add_file_handler(app, filename, max_bytes=512 * 1024, backup_count=100): app.logger.addHandler(file_handler) -def add_email_handler(app, mail_server, mail_port, mail_from_host, - log_email_recipients, log_email_topic, - level=logging.ERROR): - """Adds email notifications about captured logs.""" - mail_handler = SMTPHandler( - (mail_server, mail_port), - "logs@" + mail_from_host, - log_email_recipients, - log_email_topic - ) - mail_handler.setLevel(level) - mail_handler.setFormatter(logging.Formatter(''' - Message type: %(levelname)s - Location: %(pathname)s:%(lineno)d - Module: %(module)s - Function: %(funcName)s - Time: %(asctime)s - - Message: - - %(message)s - ''')) - app.logger.addHandler(mail_handler) - - def add_sentry(app, dsn, level=logging.WARNING, **options): """Adds Sentry logging.