jousch

simple python logrotate script

Apr 27th, 2022
1,555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. # This script is a simple alternative for rotating e.g. TYPO3 application log files
  4. # whenever the host can't handle those log files from OS level (e.g. on shared/managed hosting environments)
  5. #
  6. #
  7. # It's intended to start the script on a regular base via crontab like:
  8. #
  9. # Argument 1: path to logfile (string)
  10. # Argument 2: backupCount (int)
  11. #
  12. #  0 1 * *  * /path/to/releases/current/bin/logrotate.py /path/to/releases/current/var/log/typo3/typo3.log 4 > /dev/null
  13. #  0 1 * *  * /path/to/releases/current/bin/logrotate.py /path/to/releases/current/var/log/typo3/solr.log 4 > /dev/null
  14. #
  15. #
  16.  
  17. import sys
  18. import logging.handlers
  19. try:
  20.     logfile = sys.argv[1]
  21. except:
  22.     print("param 1: logfile")
  23.     sys.exit(1)
  24.  
  25. try:
  26.     backupCount = int(sys.argv[2])
  27. except:
  28.     print("param 2: backup count")
  29.     sys.exit(1)
  30.  
  31. handler = logging.handlers.RotatingFileHandler(
  32.     logfile,
  33.     mode='a',
  34.     maxBytes=104857600,
  35.     backupCount=backupCount,
  36.     encoding=None,
  37.     delay=False
  38. )
  39. handler.doRollover()
  40.  
Advertisement
Add Comment
Please, Sign In to add comment