0

I want to create a logger that will work in the following way:

I want to limit the log file size to - X mb, and when the file is full, I want that it to continue putting new logs to end of file, but erasing the older logs from beginning of the file. Is this something possible to achieve?

I tried to use RotatingFileHandler but it doesn't seem to work, because the file exceeded the max size.

Here is how I configured the handlers section in LOGGING dictionary, in my settigns file:

'handlers': {
        'file': {
           'level': 'DEBUG',
            'class': 'logging.FileHandler',
             'filename': '/home/ubuntu/logs/django.log',
        },
        'request_file_info': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': '/home/ubuntu/logs/request_configuration.log',
            'formatter': 'verbose'
        },
    },
Keselme
  • 3,779
  • 7
  • 36
  • 68
  • Guess it much simpler to implement with RotatingFileHandler. Did you set `maxBytes` > 0 and `backupCount` > 0? – Sergei Zherevchuk Dec 13 '18 at 10:57
  • I set maxBytes to 100mb, and no backup copies. Must I have back up copies for this to work? – Keselme Dec 13 '18 at 11:11
  • From the [docs](https://docs.python.org/3/library/logging.handlers.html#logging.handlers.RotatingFileHandler): `but if either of maxBytes or backupCount is zero, rollover never occurs, so you generally want to set backupCount to at least 1, and have a non-zero maxBytes.` – Sergei Zherevchuk Dec 13 '18 at 11:16

0 Answers0