From cc0f010566db47eb7a5bca98dd350c4b6dd46c9b Mon Sep 17 00:00:00 2001 From: Francis Reyes Date: Tue, 5 Aug 2014 13:29:07 +1000 Subject: [PATCH] Add log filter --- cid/log.py | 10 ++++++++++ conftest.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 cid/log.py diff --git a/cid/log.py b/cid/log.py new file mode 100644 index 0000000..f41e583 --- /dev/null +++ b/cid/log.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +import logging +from cid.locals import get_cid + + +class CidContextFilter(logging.Filter): + + def filter(self, record): + record.cid = get_cid() + return True diff --git a/conftest.py b/conftest.py index 56e7451..12ee4d4 100644 --- a/conftest.py +++ b/conftest.py @@ -65,5 +65,36 @@ def pytest_configure(): ), # Other settings go here + LOGGING={ + 'version': 1, + 'formatters': { + 'verbose': { + 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(cid)s %(message)s' # noqa + }, + 'simple': { + 'format': '%(levelname)s %(message)s' + } + }, + 'filters': { + 'cid': { + '()': 'cid.log.CidContextFilter' + } + }, + 'handlers': { + 'cid': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'verbose', + 'filters': ['cid'] + } + }, + 'loggers': { + 'cid': { + 'handlers': ['cid'], + 'propagate': True, + 'level': 'DEBUG' + } + } + } ) settings.configure(**BASE_SETTINGS)