Log rotation for uwsgi and python related applications

Don’t send SIGHUP or restart your servers if you want to do log rotation.

SIGHUP is NOT used for log rotation, but used for configuration/module reload.
Take uwsgi as example, as Managing the uWSGI server described, SIGHUP will gracefully reload all the workers and the master process.

Thus, the newly coming connection cannot be accepted during the reload process.
If the uwsgi application need 5 seconds to reload, then tons of connection cannot be served at that time. It may also make false positive for health checking mechanisms.

My suggestions

  • Use SIGUSR1 for web servers, such as Nginx.
  • Use logging.handlers.WatchedFileHandler in the application.
  • Use logrotate copytruncate if application upstream does not provide log rotation mechanism. (log file size should not be too big; log missing is acceptable.)
  • Use uwsgi touch-logreopen for uwsgi log rotation.

P.S: You can also use RANDOM_DELAY to avoid all your machines are going to do log rotation at the same time.