Skip to content

Instantly share code, notes, and snippets.

@thulasi-ram
Created June 2, 2017 11:48
Show Gist options
  • Save thulasi-ram/a03557108a3cd878f0efa4b7e6e9df57 to your computer and use it in GitHub Desktop.
Save thulasi-ram/a03557108a3cd878f0efa4b7e6e9df57 to your computer and use it in GitHub Desktop.
Split config for a Django Project.
import logging
from config.utils.json_encoders import ObjectEncoder
def get(log_root=None, formatter=None, handler=None):
if not formatter:
formatter = 'logstash_fmtr'
if not handler:
handler = 'logging.handlers.WatchedFileHandler'
if not log_root.endswith('/'):
log_root += '/'
return {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'request_filter': {
'()': 'log_request_id.filters.RequestIDFilter'
},
'critical_filter': {
'()': 'config.utils.filters.LogLevelFilter',
'level': logging.CRITICAL
},
'error_filter': {
'()': 'config.utils.filters.LogLevelFilter',
'level': logging.ERROR
},
'warn_filter': {
'()': 'config.utils.filters.LogLevelFilter',
'level': logging.WARN
},
'debug_filter': {
'()': 'config.utils.filters.LogLevelFilter',
'level': logging.DEBUG
},
'info_filter': {
'()': 'config.utils.filters.LogLevelFilter',
'level': logging.INFO
}
},
'formatters': {
'verbose': {
'format': "[%(request_id)s] [%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt': "%d/%b/%Y %H:%M:%S"},
'simple': {
'format': '%(levelname)s %(message)s'
},
'logstash_fmtr': {
'()': 'logstash_formatter.LogstashFormatterV1'
# TODO: Commented out to find any possibilities of logstash formatter erring in keys.
# Once there are any type errors we can uncomment the below code
# 'json_cls': ObjectEncoder
},
},
'handlers': {
'null': {
'level': 'INFO',
'class': 'django.utils.log.NullHandler',
},
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
'filters': ['request_filter']
},
'django': {
'level': 'DEBUG',
'class': handler,
'filters': ['request_filter'],
'formatter': formatter,
'filename': log_root + 'django.log',
},
'default': {
'level': 'DEBUG',
'class': handler,
'filters': ['request_filter'],
'formatter': formatter,
'filename': log_root + 'default.log',
},
'error_handler': {
'level': 'ERROR',
'class': handler,
'filters': ['error_filter', 'request_filter'],
'formatter': formatter,
'filename': log_root + 'app_error.log',
},
'critical_handler': {
'level': 'ERROR',
'class': handler,
'filters': ['critical_filter', 'request_filter'],
'formatter': formatter,
'filename': log_root + 'app_critical.log',
},
'warn_handler': {
'level': 'WARN',
'class': handler,
'filters': ['warn_filter', 'request_filter'],
'formatter': formatter,
'filename': log_root + 'app_warn.log',
},
'info_handler': {
'level': 'INFO',
'class': handler,
'filters': ['info_filter', 'request_filter'],
'formatter': formatter,
'filename': log_root + 'app_info.log',
},
'debug_handler': {
'level': 'DEBUG',
'class': handler,
'filters': ['debug_filter', 'request_filter'],
'formatter': formatter,
'filename': log_root + 'app_debug.log',
},
},
'loggers': {
'amqp': {
'handlers': ['default',],
'level': 'DEBUG',
'propagate': False,
},
'django': {
'handlers': ['django'],
'level': 'DEBUG',
'propagate': False,
},
'requests': {
'handlers': ['default', ],
'level': 'DEBUG',
'propagate': False,
},
},
'root': {
'handlers': ['console', 'error_handler', 'warn_handler', 'info_handler', 'debug_handler', 'critical_handler'],
'level': 'DEBUG',
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment