From 615fa4c6099288fbf873f04a0cc9fb86a96616b9 Mon Sep 17 00:00:00 2001 From: Arnav Kumar Date: Wed, 20 Jan 2016 00:41:08 +0800 Subject: [PATCH 1/3] Allow logging exclusions during object creation and `init_app` --- raven/contrib/flask.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/raven/contrib/flask.py b/raven/contrib/flask.py index d1781779..8086d7f9 100644 --- a/raven/contrib/flask.py +++ b/raven/contrib/flask.py @@ -103,10 +103,11 @@ class Sentry(object): # TODO(dcramer): the client isn't using local context and therefore # gets shared by every app that does init on it def __init__(self, app=None, client=None, client_cls=Client, dsn=None, - logging=False, level=logging.NOTSET, wrap_wsgi=None, - register_signal=True): + logging=False, logging_exclusions=None, level=logging.NOTSET, + wrap_wsgi=None, register_signal=True): self.dsn = dsn self.logging = logging + self.logging_exclusions = logging_exclusions self.client_cls = client_cls self.client = client self.level = level @@ -238,7 +239,8 @@ class Sentry(object): self.client.context.clear() return response - def init_app(self, app, dsn=None, logging=None, level=None, wrap_wsgi=None, + def init_app(self, app, dsn=None, logging=None, level=None, + logging_exclusions=None, wrap_wsgi=None, register_signal=None): if dsn is not None: self.dsn = dsn @@ -262,11 +264,18 @@ class Sentry(object): if logging is not None: self.logging = logging + if logging_exclusions is not None: + self.logging_exclusions = logging_exclusions + if not self.client: self.client = make_client(self.client_cls, app, self.dsn) if self.logging: - setup_logging(SentryHandler(self.client, level=self.level)) + kwargs = {} + if self.logging_exclusions is not None: + kwargs['exclude'] = self.logging_exclusions + + setup_logging(SentryHandler(self.client, level=self.level), **kwargs) if self.wrap_wsgi: app.wsgi_app = SentryMiddleware(app.wsgi_app, self.client) From 0de40e8e191469127668158fb483f8020ad93ccd Mon Sep 17 00:00:00 2001 From: Arnav Kumar Date: Mon, 25 Jan 2016 12:09:45 +0800 Subject: [PATCH 2/3] Add test for excluding loggers during setup with Flask --- tests/contrib/flask/tests.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/contrib/flask/tests.py b/tests/contrib/flask/tests.py index 072cec80..dd0df263 100644 --- a/tests/contrib/flask/tests.py +++ b/tests/contrib/flask/tests.py @@ -263,6 +263,19 @@ class FlaskTest(BaseTest): assert self.middleware.last_event_id == event_id assert g.sentry_event_id == event_id + def test_logging_setup_with_exclusion_list(self): + app = Flask(__name__) + raven = TempStoreClient() + + Sentry(app, client=raven, logging=True, + logging_exclusions=("excluded_logger",)) + + excluded_logger = logging.getLogger("excluded_logger") + self.assertFalse(excluded_logger.propagate) + + some_other_logger = logging.getLogger("some_other_logger") + self.assertTrue(some_other_logger.propagate) + class FlaskLoginTest(BaseTest): From 76042258f1e082539851c148b1c4375e7cc4c911 Mon Sep 17 00:00:00 2001 From: Arnav Kumar Date: Mon, 25 Jan 2016 13:18:35 +0800 Subject: [PATCH 3/3] Add docs to demonstrate logging exclusions --- docs/integrations/flask.rst | 6 ++++-- docs/integrations/logging.rst | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/integrations/flask.rst b/docs/integrations/flask.rst index 438a4795..8fc4e1ab 100644 --- a/docs/integrations/flask.rst +++ b/docs/integrations/flask.rst @@ -30,7 +30,8 @@ You can optionally configure logging too:: import logging from raven.contrib.flask import Sentry - sentry = Sentry(app, logging=True, level=logging.ERROR) + sentry = Sentry(app, logging=True, level=logging.ERROR, \ + logging_exclusions=("logger1", "logger2", ...)) Building applications on the fly? You can use Raven's ``init_app`` hook:: @@ -48,7 +49,8 @@ You can pass parameters in the ``init_app`` hook:: def create_app(): app = Flask(__name__) sentry.init_app(app, dsn='___DSN___', logging=True, - level=logging.ERROR) + level=logging.ERROR, + logging_exclusions=("logger1", "logger2", ...)) return app Settings diff --git a/docs/integrations/logging.rst b/docs/integrations/logging.rst index da703aa6..514d54a7 100644 --- a/docs/integrations/logging.rst +++ b/docs/integrations/logging.rst @@ -132,3 +132,13 @@ message within Sentry:: logger.error('There was some %s error', 'crazy') logger.error('There was some %s error', 'fun') logger.error('There was some %s error', 1) + +Exclusions +~~~~~~~~~~ + +You can also configure some logging exclusions during setup. These loggers +will not propagate their logs to the Sentry handler. + + from raven.conf import setup_logging + + setup_logging(handler, exclude=("logger1", "logger2", ...))