From ac2951d1dd172b837bdd3b7aee94291ef594e80f Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 9 Apr 2015 11:43:14 +0200 Subject: [PATCH 3/3] Change default logging configuration (fixes #6922) We only configure the root logger and 'django.db'. If DEBUG is True we see the root logger to DEBUG. We do not use Django default logging configuration by bypassing it using LOGGING_CONFIG = None[1]. * django_select2 logger is set to warning as the INFO level emit uninteresting messages * django.db logger is set to INFO, to prevent getting SQL queries when DEBUG is True. To get the SQL queries set DEBUG_DB to True in your settings. * Set a default logging configuration for the multitenant packaging * Add a RequestContextFilter logging filterto provide, usename, ip and a request ID to the formatter. * Use the new filter in default, debian and debian-multitenant settings. * Add a new setting DEBUG_DB for activating logging of SQL queries. [1]: https://www.caktusgroup.com/blog/2015/01/27/Django-Logging-Configuration-logging_config-default-settings-logger/ --- debian-wheezy/debian_config.py | 31 ++++++++------------- debian-wheezy/multitenant/debian_config.py | 44 ++++++++++++++++++++++++++++++ src/authentic2/log_filters.py | 26 ++++++++++++++++++ src/authentic2/settings.py | 35 +++++++++++++++--------- 4 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 src/authentic2/log_filters.py diff --git a/debian-wheezy/debian_config.py b/debian-wheezy/debian_config.py index 6883eed..c8c9d53 100644 --- a/debian-wheezy/debian_config.py +++ b/debian-wheezy/debian_config.py @@ -19,56 +19,49 @@ if os.path.exists('/var/lib/authentic2/secret_key'): LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'filters': { 'cleaning': { '()': 'authentic2.utils.CleanLogMessage', }, + 'request_context': { + '()': 'authentic2.log_filters.RequestContextFilter', + }, }, 'formatters': { 'syslog': { - 'format': '%(levelname)s %(name)s.%(funcName)s: %(message)s', + 'format': '%(ip)s %(user)s %(request_id)s %(levelname)s %(name)s.%(funcName)s: %(message)s', }, }, 'handlers': { 'syslog': { 'level': 'DEBUG', 'address': '/dev/log', 'class': 'logging.handlers.SysLogHandler', - 'filters': ['cleaning'], + 'filters': ['cleaning', 'request_context'], 'formatter': 'syslog', }, }, 'loggers': { - 'py.warnings': { + # even when debugging seeing SQL queries is too much, activate it + # explicitly using DEBUG_DB + 'django.db': { 'handlers': ['syslog'], - 'level': 'WARNING', - 'propagate': False, - }, - 'django': { - 'handlers': ['syslog'], - 'level': 'WARNING', - 'propagate': False, + 'level': 'INFO', }, - 'lasso': { + # Django select2 output + 'django_select2': { 'handlers': ['syslog'], 'level': 'WARNING', - 'propagate': False, - }, - 'authentic2': { - 'handlers': ['syslog'], - 'level': 'INFO', - 'propagate': False, }, - 'authentic2_idp_openid': { + '': { 'handlers': ['syslog'], 'level': 'INFO', - 'propagate': False, }, }, } # Old settings method def extract_settings_from_environ(): import os import json diff --git a/debian-wheezy/multitenant/debian_config.py b/debian-wheezy/multitenant/debian_config.py index aa97532..0e51cb3 100644 --- a/debian-wheezy/multitenant/debian_config.py +++ b/debian-wheezy/multitenant/debian_config.py @@ -61,10 +61,54 @@ DATABASES = { 'NAME': PROJECT_NAME.replace('-', '_') } } DATABASE_ROUTERS = ( 'tenant_schemas.routers.TenantSyncRouter', ) +LOGGING = { + 'version': 1, + 'disable_existing_loggers': True, + 'filters': { + 'cleaning': { + '()': 'authentic2.utils.CleanLogMessage', + }, + 'request_context': { + '()': 'authentic2.log_filters.RequestContextFilter', + }, + }, + 'formatters': { + 'syslog': { + 'format': '%(ip)s %(user)s %(request_id)s %(levelname)s %(name)s.%(funcName)s: %(message)s', + }, + }, + 'handlers': { + 'syslog': { + 'level': 'DEBUG', + 'address': '/dev/log', + 'class': 'logging.handlers.SysLogHandler', + 'filters': ['cleaning', 'request_context'], + 'formatter': 'syslog', + }, + }, + 'loggers': { + # even when debugging seeing SQL queries is too much, activate it + # explicitly using DEBUG_DB + 'django.db': { + 'handlers': ['syslog'], + 'level': 'INFO', + }, + # django_select2 outputs debug message at level INFO + 'django_select2': { + 'handlers': ['syslog'], + 'level': 'WARNING', + }, + '': { + 'handlers': ['syslog'], + 'level': 'INFO', + }, + }, +} + if os.path.exists(os.path.join(ETC_DIR, 'config.py')): execfile(os.path.join(ETC_DIR, 'config.py')) diff --git a/src/authentic2/log_filters.py b/src/authentic2/log_filters.py new file mode 100644 index 0000000..6c7d713 --- /dev/null +++ b/src/authentic2/log_filters.py @@ -0,0 +1,26 @@ +import logging + +class RequestContextFilter(logging.Filter): + DEFAULT_USERNAME = '-' + DEFAULT_IP = '-' + DEFAULT_REQUEST_ID = '-' + + def filter(self, record): + '''Add username, ip and request ID to the log record. + + Inspired by django-log-request-id + ''' + from . import middleware + request = middleware.StoreRequestMiddleware.get_request() + user = self.DEFAULT_USERNAME + ip = self.DEFAULT_IP + request_id = self.DEFAULT_REQUEST_ID + if not request is None: + if hasattr(request, 'user') and request.user.is_authenticated(): + user = unicode(request.user).encode('utf-8') + ip = request.META['REMOTE_ADDR'] + request_id = request.request_id + record.user = user + record.ip = ip + record.request_id = request_id + return True diff --git a/src/authentic2/settings.py b/src/authentic2/settings.py index 096766d..c74a0b9 100644 --- a/src/authentic2/settings.py +++ b/src/authentic2/settings.py @@ -1,8 +1,9 @@ +import logging.config # Load default from Django from django.conf.global_settings import * import os import django from . import plugins @@ -11,16 +12,17 @@ BASE_DIR = os.path.dirname(__file__) ### Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'please-change-me-with-a-very-long-random-string' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True +DEBUG_DB = False TEMPLATE_DEBUG = True MEDIA = 'media' # See https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts ALLOWED_HOSTS = [] DATABASES = { 'default': { @@ -185,57 +187,57 @@ ADMIN_TOOLS_MENU = 'authentic2.menu.CustomMenu' SERIALIZATION_MODULES = { 'json': 'authentic2.serializers', } # Set Test runner to remove warning about test suite initialized with Django < 1.6 if django.VERSION >= (1,7): TEST_RUNNER = 'django.test.runner.DiscoverRunner' +LOGGING_CONFIG = None LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'filters': { 'cleaning': { '()': 'authentic2.utils.CleanLogMessage', }, + 'request_context': { + '()': 'authentic2.log_filters.RequestContextFilter', + }, }, 'formatters': { 'verbose': { - 'format': '[%(asctime)s] %(levelname)s %(name)s.%(funcName)s: %(message)s', + 'format': '[%(asctime)s] %(ip)s %(user)s %(request_id)s %(levelname)s %(name)s.%(funcName)s: %(message)s', 'datefmt': '%Y-%m-%d %a %H:%M:%S' }, }, 'handlers': { 'console': { 'level': 'DEBUG', 'class':'logging.StreamHandler', 'formatter': 'verbose', + 'filters': ['cleaning', 'request_context'], }, }, 'loggers': { - 'django': { + # even when debugging seeing SQL queries is too much, activate it + # explicitly using DEBUG_DB + 'django.db': { 'handlers': ['console'], - 'level': 'WARNING', - 'propagate': False, + 'level': 'INFO', }, - 'lasso': { + # django_select2 outputs debug message at level INFO + 'django_select2': { 'handlers': ['console'], 'level': 'WARNING', - 'propagate': False, - }, - 'authentic2': { - 'handlers': ['console'], - 'level': 'INFO', - 'propagate': False, }, - 'authentic2_idp_openid': { + '': { 'handlers': ['console'], 'level': 'INFO', - 'propagate': False, }, }, } # Temporary fix for User.username field length MIGRATION_MODULES = { 'auth': 'authentic2.auth_migrations', } @@ -247,8 +249,15 @@ MIGRATION_MODULES = { if 'AUTHENTIC2_SETTINGS_FILE' in os.environ: execfile(os.environ['AUTHENTIC2_SETTINGS_FILE']) # # Apply monkey patches # from . import fix_user_model + +# Post local config setting +if DEBUG: + LOGGING['loggers']['']['level'] = 'DEBUG' +if DEBUG_DB: + LOGGING['loggers']['django.db']['level'] = 'DEBUG' +logging.config.dictConfig(LOGGING) -- 1.9.1