Last active
January 15, 2017 21:44
-
-
Save vaibhav-jain/e32341218dc7bacc5f44 to your computer and use it in GitHub Desktop.
Log Django exceptions to file. Contains snippet for both WatchedFileHandler and RotatingFileHandler handlers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add this configuration to your Django settings.py file | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'verbose': { | |
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s', | |
'datefmt': "%d/%b/%Y %H:%M:%S", | |
}, | |
}, | |
'handlers': { | |
# Log to a text file | |
'logfile': { | |
'class': 'logging.handlers.WatchedFileHandler', | |
'filename': 'path/to/log/file', | |
'formatter': 'verbose', | |
}, | |
}, | |
'loggers': { | |
'django': { | |
'handlers': ['logfile'], | |
'level': 'DEBUG', | |
'propagate': False, | |
}, | |
}, | |
} | |
# This is logging configuration 'RotatingFileHandler' | |
# The setting mentioned above and below are totally seprate. | |
# Use python manage.py runserver --no-reload. | |
# If you get file in use error | |
# Logger configuration | |
# Create a seprate folder within your project. | |
# For storing logs. | |
# Change `myproject` with your project name. | |
LOGFILE_NAME = os.path.join(BASE_DIR, 'logs/myproject.log') | |
# Max size allowed for one file | |
# This setting will be used by 'RotatingFileHandler' | |
# I have kept maxBytes to low value. | |
# Just for demonstration purpose. | |
LOGFILE_SIZE = 1 * 1024 | |
# Log file count | |
LOGFILE_COUNT = 2 | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'verbose': { | |
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s', | |
'datefmt': "%d/%b/%Y %H:%M:%S", | |
}, | |
}, | |
'handlers': { | |
# Log to a text file that can be rotated by logrotate | |
'logfile': { | |
'level': 'DEBUG', | |
'formatter': 'verbose', | |
'class': 'logging.handlers.RotatingFileHandler', | |
'filename': LOGFILE_NAME, | |
'maxBytes': LOGFILE_SIZE, | |
'backupCount': LOGFILE_COUNT, | |
}, | |
}, | |
'loggers': { | |
'django': { | |
'handlers': ['logfile'], | |
'level': 'DEBUG', | |
'propagate': True, | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment