[Django]-Django logging can't set maxBytes

10👍

maxBytes is an attribute of the RotatingFileHandler not the FileHandler. You will need to modify your logging config to:

'file_database': {
    'level': 'DEBUG',
    'class': 'logging.handlers.RotatingFileHandler',  # Use RotatingFileHandler
    'filename': os.path.join(BASE_DIR, 'logs/database.log'),
    'maxBytes': 1024 * 1024 * 10,  # 10 MB
},

Note that the package is logging.handlers.

0👍

From django doc, you can also set backupCount to keep multiple files.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'debugRotatingFileHandler': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'formatter': 'verbose',
            'maxBytes': 1024*1024*1000, # 1000 MB
            'backupCount': 0, # Keep 1 file
            'filename': os.path.join(BASE_DIR, 'django.log'),
        },
    },
    'loggers': {
        'django': {
            'handlers': ['debugRotatingFileHandler'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

Leave a comment