Skip to content

Instantly share code, notes, and snippets.

@st4lk
Last active December 2, 2022 07:14
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save st4lk/6725777 to your computer and use it in GitHub Desktop.
Save st4lk/6725777 to your computer and use it in GitHub Desktop.
Django logging settings
# Logging settings for django projects, works with django 1.5+
# 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': '%(levelname)s:%(name)s: %(message)s '
'(%(asctime)s; %(filename)s:%(lineno)d)',
'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',
},
'production_file': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/main.log',
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 7,
'formatter': 'main_formatter',
'filters': ['require_debug_false'],
},
'debug_file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/main_debug.log',
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 7,
'formatter': 'main_formatter',
'filters': ['require_debug_true'],
},
'null': {
"class": 'django.utils.log.NullHandler',
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins', 'console'],
'level': 'ERROR',
'propagate': True,
},
'django': {
'handlers': ['null', ],
},
'py.warnings': {
'handlers': ['null', ],
},
'': {
'handlers': ['console', 'production_file', 'debug_file'],
'level': "DEBUG",
},
}
}
@gonzaloamadio
Copy link

For django 1.10+, null handler should be: "class": 'logging.NullHandler',

And another improvement is changing filename to:
'filename': os.path.join(DJANGO_ROOT, 'logs/myfile.log'),

So it is always placed on same folder.

@whg517
Copy link

whg517 commented Jan 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment