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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With