KenanBek
KenanBek

Reputation: 1067

How to split Python logging file by file size

I am using Django and have included Python's default logging library. I have following configuration for logging (in settings.py):

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format' : "[%(asctime)s %(name)s-%(levelname)s (%(filename)s:%(lineno)s %(funcName)s)]: %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '[%(asctime)s %(name)s-%(levelname)s]: %(message)s',
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'file-django': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename':  os.path.join(PROJECT_LOGS, 'django.log'),
            'formatter': 'simple'
        },
        'file-application': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename':  os.path.join(PROJECT_LOGS, 'application.log'),
            'formatter': 'simple'
        },
        'file-core': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename':  os.path.join(PROJECT_LOGS, 'core.log'),
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {
            'handlers':['file-django'],
            'propagate': True,
            'level':'DEBUG',
        },
        'application': {
            'handlers': ['file-application'],
            'propagate': True,
            'level': 'DEBUG',
        },
        'core': {
            'handlers': ['file-core'],
            'propagate': True,
            'level': 'DEBUG',
        },
    }
}

I am trying to split result logging files using only configuration. I "googled" and found few solutions but all of them are code based.

Upvotes: 2

Views: 3567

Answers (2)

Jingchao Luan
Jingchao Luan

Reputation: 451

You can use the logging.handlers.RotatingFileHandler in your configuration file setting.py, take your handler 'file-django' as example and assume you split it into a new file when its size over than 100MB:

'file-django': {
    'level': 'DEBUG',
    'class': 'logging.handlers.RotatingFileHandler',
    'filename':  os.path.join(PROJECT_LOGS, 'django.log'),
    'maxBytes': 104857600, # 1024*1024*100B (100MB)
    'backupCount': 10, # keep at most 10 log files
    'formatter': 'simple'
},

Reference: https://xxx-cook-book.gitbooks.io/django-cook-book/Logs/Handlers/FileHandler/rotating-file-handler.html

Upvotes: 3

Vinay Sajip
Vinay Sajip

Reputation: 99510

Use Ignacio's comment and look up how to use RotatingFileHandler; it splits by file size and you can configure it declaratively in Django via the LOGGING dictionary.

Upvotes: 1

Related Questions