0

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
bhargav patel
  • 919
  • 10
  • 16

2 Answers2

1

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'
    }
  }
}
Zeeshan
  • 277
  • 2
  • 6
  • 16
0

In settings, where is says 'level': 'DEBUG' change it to say 'level': 'ERROR'.

This page should explain more.

ddg
  • 1,090
  • 1
  • 6
  • 17
  • thanks ddg. but It is blocking all INFO messages. i want to log all info messages except "POST /MyProject/ HTTP/1.1" 200 36" – bhargav patel Jun 07 '18 at 07:12
  • I don't see an easy way to change that behavior, sorry. Maybe write a script to go through your logs afterwards and delete those messages? – ddg Jun 07 '18 at 07:16