1. 指定 touch-logreopen 文件
[uwsgi] socket = 0.0.0.0:23288 processes = 4 threads = 1 master = true pythonpath = /app/aiphoto/ai_photo module = aiphoto callable = app env = AIPHOTO=Production #env = AIPHOTO_SERVER_NAME=https://aiphoto.howfun.tv env = AIPHOTO_APP=True buffer-size = 32768 logto = /app/aiphoto/logs/uwsgi/aiphoto.log pidfile = /app/aiphoto/logs/uwsgi/aiphoto_uwsgi.pid #memory-report = true gevent=512 gevent-monkey-patch=true touch-logreopen = /app/aiphoto/logs/uwsgi/touchforlogrotate #对应的目录建立此文件,防止压缩过程中日志写入不成功
这里我们指定日志文件就在项目目录下叫 uwsgi.daemonize.log,监听项目目录下的 touchforlogrotate 文件,如果文件发生变化,就重新打开日志,再 touch 一下 touchforlogrotate,之前的文件便停止写入
2. 自动脚本
自动压缩脚本
# # -*- coding: utf8 -*- """ 新建touchlog 防止压缩后日志出现中断的情况 """ import os import shutil import glob import time import gzip import argparse def rotate_to_gz(source, limit=0): if os.path.exists(source): target = "%s.%s.gz" % (source, time.strftime("%Y-%m-%d_%H-%M-%S")) new_name = "{}_1".format(source) shutil.move(source,new_name) # cmd = "mv {} {}".format(source, new_name) # os.popen(cmd).readlines() #touchfile = "/data/ai_photo/logs/uwsgi/touchforlogrotate" # 新建touchlog 防止压缩后日志出现中断的情况 touchfile = args.touchforlogrotate # 新建touchlog 防止压缩后日志出现中断的情况 touch_cmd = "touch {}".format(touchfile) os.popen(touch_cmd).readlines() with open(new_name, 'rb') as fin, gzip.open(target, 'wb') as fout: shutil.copyfileobj(fin, fout) # limit file number if limit > 0: rm_files = sorted(glob.glob( '%s.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' % source + '_[0-9][0-9]-[0-9][0-9]-[0-9][0-9].gz'))[:-limit] for f in rm_files: delete_file(f) def delete_file(file): if os.path.exists(file): os.remove(file) def main(): # rotate_by_date = ["/data/ai_photo/logs/uwsgi/aiphoto.log"] rotate_by_date = [args.aiphoto_log_src] # rotate stdout and stderr by size for f in rotate_by_date: try: if os.path.exists(f): # file size > 1GB, backupCount=5 rotate_to_gz(f, 7) except BaseException: print("rotate %s error", f) if __name__ == "__main__": parser = argparse.ArgumentParser(description="ai_photo log back") parser.add_argument('aiphoto_log_src') # 日志路径 parser.add_argument('touchforlogrotate') # 新建touchlog 防止压缩后日志出现中断的情况 args = parser.parse_args() main()
3. crontab 定时调用
#定时脚本命令推荐放到root用户下执行 00 00 * * * cd /app/aiphoto/ai_photo && python aiphoto/maintenances/rotate_logs.py /app/aiphoto/logs/uwsgi/aiphoto.log /app/aiphoto/logs/uwsgi/touchforlogrotate >/tmp/rotate_logs.cron 2>&1
好了,重启 uwsgi,logs 文件夹下便会有每天的日志了。