stackoverflow.com/questions/8467978/python-want-logging-with-log-rotation-and-compression
Python, want logging with log rotation and compression
Can anyone suggest a way in python to do logging with: log rotation every day compression of logs when they're rotated optional - delete oldest log file to preserve X MB of free space optional - s...
stackoverflow.com
import gzip
import os
import logging
import logging.handlers
class GZipRotator:
def __call__(self, source, dest):
os.rename(source, dest)
f_in = open(dest, 'rb')
f_out = gzip.open("%s.gz" % dest, 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
os.remove(dest)
logformatter = logging.Formatter('%(asctime)s;%(levelname)s;%(message)s')
log = logging.handlers.TimedRotatingFileHandler('debug.log', 'midnight', 1, backupCount=5)
log.setLevel(logging.DEBUG)
log.setFormatter(logformatter)
log.rotator = GZipRotator()
logger = logging.getLogger('main')
logger.addHandler(log)
logger.setLevel(logging.DEBUG)
....
'[공부용]참고 사이트 모음 > [python]' 카테고리의 다른 글
[Python] Flask + uWSGI + Nginx를 연결 및 배포 (0) | 2021.04.09 |
---|---|
Starting/stopping a background Python process wtihout nohup + ps aux grep + kill (0) | 2021.03.24 |
Python Backend Program – DBModel (0) | 2021.03.23 |
[python logging] 파이썬 일단위로 로그 파일 생성하기 - 로그 파일에 자동으로 날짜 입히기 - TimedRotatingFileHandler를 이용한 데일리 로깅 (0) | 2021.03.22 |
Python requests 모듈 (0) | 2021.03.19 |