Zorgan
Zorgan

Reputation: 9153

Is it suitable to delete my log.django file?

My log.django file is over 100,000 lines now. If I delete it, will it be re-generated again? It exists because I have LOGGING in my settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'log.django',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console', 'file'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
        },
    },
}

Upvotes: 3

Views: 1085

Answers (1)

Borut
Borut

Reputation: 3364

Log file cannot be re-generated. It consists of events that occurred in the past. But if you deleted it, a new one will be generated.

I suggest you set RotatingFileHandler logging handler, the maximum size for the log file and a number of backups to be kept. This way once the file reaches maximum size, current log file will be renamed and a new log file will be created.

Django uses dictConfig scheme for logging configuration. For rotating file handler with maximum size of about 2MB and 2 backup log files, you'd define file key in handlers as:

'file': {
    'level': 'DEBUG',
    'class': 'logging.handlers.RotatingFileHandler',
    'filename': 'log.django',
    'maxBytes': 2000000,
    'backupCount': 2
 },

This way current log.django will be renamed to log.django.1 once it reaches size of 2.000.000 bytes and 2 backup files will be kept (log.django.1 and log.django.2) at all times.

If you add maxBytes and backupCount to your current configuration, your current log file will be renamed to log.django.1 and a new log.django will be created.

Upvotes: 2

Related Questions