I have written a simple django LOGGING in my settings.py and I excepted that to log all error with a traceback in my file. But it doesn't and it just logs errors and everything in one line, but tracebacks are logged into the console. here is my LOGGING:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '{levelname} {asctime} {name} {module}.{funcName}:{lineno} {message}',
'style': '{',
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'logs/debug.log',
'formatter': 'simple'
},
},
'loggers': {
'': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': False,
},
},
}
can anybody help me to understand why and what to do? thanks.
The formatter you are using is "simple" (as defined on the config:
'formatters': {
'simple': {
'format': '{levelname} {asctime} {name} {module}.{funcName}:{lineno} {message}',
'style': '{',
},
},
So playing around with this is the trick: Have a look at this: https://docs.python.org/3/library/logging.html#logrecord-attributes
The one you probably want is: {stack_info}
To achieve this you can add a custom format and then write a custom filter for it to populate its value:
'formatters': {
'simple_trace': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s - %(trace)s'
},
Now, CustomFilter should be inherited from logging.Filter something like:
class CustomFilter(logging.Filter):
def filter(self, log_record):
def _get_trace():
trace = ""
if log_record.levelname in ['ERROR', 'CRITICAL']:
# Get the recent stack-trace
trace = traceback.format_exc().strip()
return json.dumps(trace)
log_record.trace = _get_trace()
Like this you can add other format too and just add it's value in log_record.
Finally, we need to inherit handler (in your case- logging.FileHandler ) and add this custom filter to it.
class CustomHandler(logging.FileHandler):
def __init__(self, *args, **kwargs):
logging.FileHandler.__init__(self, *args, **kwargs)
self.addFilter(CustomFilter())
while setting handlers we need to put our CustomHandler in class:
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging_custom.CustomHandler', # path to CustomHandler defination
'filename': 'logs/debug.log',
'formatter': 'simple_trace'
},
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.