Commit 00c9a6d5 authored by Antony Chazapis's avatar Antony Chazapis
Browse files

Change logging mechanism.

Refs #2016
parent fba1c49d
This diff is collapsed.
......@@ -34,17 +34,31 @@
from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed
from astakos.lib.dictconfig import dictConfig
import logging
class NullHandler(logging.Handler):
def emit(self, record):
pass
class LoggingConfigMiddleware:
def __init__(self):
'''Initialise the logging setup from settings, called on first request.'''
args = {}
args['level'] = logging.DEBUG if getattr(settings, 'DEBUG', False) else logging.INFO
if settings.LOGFILE:
args['filename'] = settings.LOGFILE
args['format'] = '%(asctime)s [%(levelname)s] %(name)s %(message)s'
args['datefmt'] = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(**args)
logging_setting = getattr(settings, 'LOGGING_SETUP', None)
if logging_setting:
# Disable handlers that are not used by any logger.
active_handlers = set()
loggers = logging_setting.get('loggers', {})
for logger in loggers.values():
active_handlers.update(logger.get('handlers', []))
handlers = logging_setting.get('handlers', {})
for handler in handlers:
if handler not in active_handlers:
handlers[handler] = {'class': 'logging.NullHandler'}
logging.NullHandler = NullHandler
dictConfig(logging_setting)
raise MiddlewareNotUsed('Logging setup only.')
......@@ -100,8 +100,34 @@ AUTHENTICATION_BACKENDS = ('astakos.im.auth_backends.EmailBackend',
CUSTOM_USER_MODEL = 'astakos.im.AstakosUser'
# Use to log to a file.
LOGFILE = None
# Setup logging (use this name for the setting to avoid conflicts with django > 1.2.x).
LOGGING_SETUP = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'simple': {
'format': '%(message)s'
},
'verbose': {
'format': '%(asctime)s [%(levelname)s] %(name)s %(message)s'
},
},
'handlers': {
'null': {
'class': 'logging.NullHandler',
},
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
},
'loggers': {
'astakos': {
'handlers': ['console'],
'level': 'DEBUG' if DEBUG else 'INFO'
},
}
}
# The server is behind a proxy (apache and gunicorn setup).
USE_X_FORWARDED_HOST = False
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment