5

I have this configuration in my project:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'WARNING',
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'debug.log'),
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'WARNING',
            'propagate': True,
        },
    },
}

Now its size grows uncontrollably. Is there a way to control a size of debug.log file? What is the best way to operate with log files in Django projects?
I have found similar question but I am not calling python logger directly.

Chiefir
  • 2,561
  • 1
  • 27
  • 46

1 Answers1

17

What you're looking for is Python's RotatingFileHandler.

Use this in your 'handlers'

'file': {
    'level': 'WARNING',
    'class': 'logging.handlers.RotatingFileHandler',
    'filename': os.path.join(BASE_DIR, 'debug.log'),
    'backupCount': 10, # keep at most 10 log files
    'maxBytes': 5242880, # 5*1024*1024 bytes (5MB)
},

When I tried I got PermissionError. This answer explains how to handle it. EDIT: This happens when you use django's development server to run it. Shouldn't be a problem in other cases.

phoenix
  • 7,988
  • 6
  • 39
  • 45
sP_
  • 1,738
  • 2
  • 15
  • 29