django preety log

  1. settings.py
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'logfile': {
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': 'var/log/log.log'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {

        'django': {
            'handlers': ['logfile'],
            'level': 'ERROR',
            'propagate': False,
        },
        'main': {
            'handlers': ['logfile', 'console'],
            'level': 'WARNING',
            'propagate': False
        },
    },
}

2. Logging

def exception_to_string(excp):
   stack = traceback.extract_stack()[:-999] + traceback.extract_tb(excp.__traceback__) 
   pretty = traceback.format_list(stack)
   return ''.join(pretty) + '\n  {} {}'.format(excp.__class__,excp)

log.error(exception_to_string(e)) log.error(e.message)</pre>