blondelg
blondelg

Reputation: 986

Automatically delete logs in Django

Building a Django app, I am trying to figure out what would be the best way to automate log file deletion.

My try so far is to build a admin command that delete old logs. From this I can setup a scheduler in python or at server's level but I am not very satisfied with this option because it makes more configuration steps to do during deployment.

I was wondering if sub-classing log module to trigger a delete-log function on a log write would be a good idea. The pros would be that there is not setup to do a deployment, the cons would be to rely on log write to clean log directory could be useless if there is no log records for a long time. On the other hand it could be resource consuming if there is too many log records.

So first of all, is a log-delete triggered by any action could be a good idea and what would be the best action to choose (a daily one)?

If not, what would be the best option to perform this in a beautiful way?

Upvotes: 0

Views: 2488

Answers (2)

blondelg
blondelg

Reputation: 986

Here is my solution,

first forgetting about admin command and scheduler,

then I have setup my logging file handler as follow:

    'handlers': {

        ...
        'log_file':{
            'level': 'INFO',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'when': 'D',
            'interval': 1,
            'backupCount': 3,
            'encoding': 'utf8',
            'filename': os.path.join(log_path, log_name),
            'formatter': 'verbose',
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        }
    },

Using TimedRotatingFileHandler class

  • 'when': 'D', means I rotate logs on a daily basis
  • 'interval': 1, means the rotating occurs every 1 days
  • 'backupCount': 3, means I want to keek a 3 day history before log deletion

Upvotes: 4

frost-nzcr4
frost-nzcr4

Reputation: 1620

Main purpose of web frameworks is handling of web requests. And here logging is a kind of subsystem for creating breadcrumbs to getting a picture of what was done and what was not. Best match for subsystem is a separate log service i.e system log. Have you tried writing the logs to the system and rotate them using log rotation mechanics provided by your system?

Upvotes: 0

Related Questions