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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm tring this solution in my poroject

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.