Merge pull request #726 from radzinzki/allow_exclude_list_on_creation

Allow logging exclusions during object creation and `init_app`
This commit is contained in:
Armin Ronacher 2016-01-27 15:06:50 -08:00
commit 3e14b36050
4 changed files with 40 additions and 6 deletions

View File

@ -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

View File

@ -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", ...))

View File

@ -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)

View File

@ -270,6 +270,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):