Created
June 9, 2021 12:42
-
-
Save conformist-mw/6843982753ceb6fcee8128b8dd065447 to your computer and use it in GitHub Desktop.
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
import sys | |
import logging | |
from django.utils import termcolors | |
def exception_hook(exc_type, exc_value, exc_traceback): | |
# Log unhandled exceptions with time | |
if issubclass(exc_type, KeyboardInterrupt): | |
sys.__excepthook__(exc_type, exc_value, exc_traceback) | |
return | |
logging.getLogger('exception').critical( | |
'Uncaught Exception!', | |
exc_info=(exc_type, exc_value, exc_traceback), | |
) | |
sys.excepthook = exception_hook | |
class ColorFormatter(logging.Formatter): | |
# colors: ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') | |
FORMATTERS = { | |
logging.DEBUG: termcolors.make_style(opts='bold', fg='cyan'), | |
logging.INFO: termcolors.make_style(opts='bold', fg='green'), | |
logging.WARNING: termcolors.make_style(opts='bold', fg='yellow'), | |
logging.ERROR: termcolors.make_style(opts='bold', fg='red'), | |
logging.CRITICAL: termcolors.make_style(opts='bold', fg='magenta'), | |
} | |
def format(self, record): | |
log_string = super().format(record) | |
return self.FORMATTERS[record.levelno](log_string) | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'colored': { | |
'()': ColorFormatter, | |
'format': '{asctime} - {name} - {levelname} - {message} {filename}:{lineno}', | |
'style': '{', | |
}, | |
}, | |
'handlers': { | |
'console': { | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'formatter': 'colored' | |
}, | |
}, | |
'loggers': { | |
'': { | |
'level': 'DEBUG', | |
'handlers': ['console'], | |
'propagate': True, | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment