Skip to content

Instantly share code, notes, and snippets.

@gonzaloamadio
Created November 10, 2017 18:45
Show Gist options
  • Save gonzaloamadio/764bb66febcab4a2c0f48f1791a62b13 to your computer and use it in GitHub Desktop.
Save gonzaloamadio/764bb66febcab4a2c0f48f1791a62b13 to your computer and use it in GitHub Desktop.
########## LOGGIN
# Put this inside settings.py (if you have separated settings, put it on base setting)
# If DEBUG=True, all logs (including django logs) will be
# written to console and to debug_file.
# If DEBUG=False, logs with level INFO or higher will be
# saved to production_file.
# Logging usage:
# import logging
# logger = logging.getLogger(__name__)
# logger.info("Log this message")
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
},
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue'
}
},
'formatters': {
'main_formatter': {
'format': '[%(asctime)s][%(levelname)s][%(filename)s - %(name)s:%(lineno)d] %(message)s',
'datefmt': "%Y-%m-%d %H:%M:%S",
},
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'console': {
'level': 'DEBUG',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
'formatter': 'main_formatter',
},
'reqfile': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(DJANGO_ROOT, 'logs/nafta-requests.log'),
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 10,
'formatter': 'main_formatter',
},
'production_file': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(DJANGO_ROOT, 'logs/nafta-prod.log'),
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 10,
'formatter': 'main_formatter',
# Filtro que da true cuando DEBUG=False, entonces si debug=True,
# no pasa el filtro, y por lo tanto no es manejado por este handler.
'filters': ['require_debug_false'],
},
'debug_file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(DJANGO_ROOT, 'logs/nafta-test.log'),
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 10,
'formatter': 'main_formatter',
'filters': ['require_debug_true'],
},
'null': {
"class": 'logging.NullHandler',
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins', 'console','reqfile'],
'level': 'ERROR',
'propagate': True,
},
'django': {
'handlers': ['null', ],
},
'py.warnings': {
'handlers': ['null', ],
},
# Definimos este root logger, que agarra logs de todos los modulos.
# Por eso ponemos los otros en null, para no duplicar mensajes.
'': {
'handlers': ['console', 'production_file', 'debug_file'],
'level': "DEBUG",
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment