I am new to logging module. i want to know that how to continuously update log file excluding response message like 2018-06-07 11:33:22,330|INFO|"POST /MyProject/ HTTP/1.1" 200 36
.
settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s|%(levelname)s|%(message)s'
}
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': "logging.handlers.RotatingFileHandler",
'formatter': 'standard',
'filename': "C:\\ProgramData\\PROGRAMX\\Logs\\PROGRAMX_logs_%s.txt" % (datetime.today().strftime("%Y_%m_%d"))
}
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
'format': '%(asctime)s|%(levelname)s|%(message)s'
}
}
}
i am updating log in django like,
import logging
log = logging.getLogger('django')
log.error("Internal Error: X happened.")
I want to complete two tasks:
1. Update log file immediately after request get processed.
2. In log file, i don't want to add message like "POST /MyProject/ HTTP/1.1" 200 36
.
PROGRAMX_logs_2018_06_07.txt
2018-06-07 11:33:14,317|ERROR|Internal Error: X happened.
2018-06-07 11:33:14,319|INFO|"POST /MyProject/ HTTP/1.1" 200 36
2018-06-07 11:33:22,327|ERROR|Internal Error: X happened.
2018-06-07 11:33:22,330|INFO|"POST /MyProject/ HTTP/1.1" 200 36
Write a custom logging filter script which will filter out the whatever you need and add it to your logging settings.
Follow the below link to add the custom filter to your django settings.py
https://docs.djangoproject.com/en/2.0/topics/logging/#examples
Below script only allows INFO level messages to be logged.
#bar.py
import logging
class InfoFilter(logging.Filter):
def filter(self, record):
return record.levelno == logging.INFO
You can also use record.name
to filter further based on your needs.
you need to add the filter to your settings:
settings.py
LOGGING={
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s|%(levelname)s|%(message)s'
}
},
'filters': {
'special': {
'()': 'Foo.bar',
}
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': "logging.handlers.RotatingFileHandler",
'formatter': 'standard',
'filters': ['special'],
'filename': "C:\\ProgramData\\PROGRAMX\\Logs\\PROGRAMX_logs_%s.txt"%(datetime.today().strftime("%Y_%m_%d"))
}
},
'loggers': {
'django': {
'handlers': [
'file'
],
'level': 'DEBUG',
'propagate': True,
'format': '%(asctime)s|%(levelname)s|%(message)s'
}
}
}