Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a Log file max size in Django project

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.

like image 510
Chiefir Avatar asked Dec 10 '22 06:12

Chiefir


1 Answers

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.

like image 66
sP_ Avatar answered Dec 25 '22 04:12

sP_