import os
import time
from os import listdir
from os.path import isfile
import json
TODAY = time.strftime("%Y%m%d", time.localtime())
SPLIT_NUM_FILE = "{0}/{1}".format(os.getcwd(), "split_num.json")
def get_appname_and_logname(log_root_path):
dir_file_dict = {}
dirs = listdir(log_root_path)
for d in dirs:
appname_path = "{0}/{1}".format(log_root_path, d)
for f in listdir(appname_path):
file_path = "{0}/{1}".format(appname_path, f)
if isfile(file_path):
dir_file_dict[d] = f
return dir_file_dict
def reset_split_num(fpath):
current_time = time.time()
create_time = os.path.getctime(fpath)
is_before_today = current_time - create_time - 60 * 60 * 24 > 0
if is_before_today:
with open(fpath) as f:
f.write(json.dumps({}))
def read_big_split_num(fpath):
with open(fpath) as f:
data = json.loads(f.read())
return data
def write_big_split_num(app_split_dict, fpath='.'):
if not os.path.exists(fpath):
with open(fpath, 'w') as f:
f.write(json.dumps({}))
if not os.path.getsize(fpath):
with open(fpath, 'w') as f:
f.write(json.dumps({}))
with open(fpath, 'w') as f:
f.write(json.dumps(app_split_dict))
def logrote_by_size(appname, logname , todir='', size=100, log_root_path='.'):
chunksize = 1024 * 1024 * size #100M
log_path = "{0}/{1}/{2}".format(log_root_path, appname, logname)
todir = "{0}/{1}/{2}".format(log_root_path, appname, todir or TODAY)
if not os.path.exists(todir):
os.mkdir(todir)
reset_split_num(SPLIT_NUM_FILE)
app_split_num = read_big_split_num(SPLIT_NUM_FILE)
app_split_num[appname] = app_split_num.get(appname, 0)
with open(log_path, 'rb') as f:
while True:
chunk = f.read(chunksize)
if not chunk:
break
new_file = os.path.join(todir, appname + '_' + str(app_split_num[appname] + 1))
with open(new_file, 'wb') as w:
w.write(chunk)
app_split_num[appname] += 1
print('app_split_num[appname] = ', app_split_num)
return app_split_num
def main(log_root_path):
app_split_num={}
for appname, logname in get_appname_and_logname(log_root_path).items():
app_dict = logrote_by_size(appname, logname, log_root_path=log_root_path, size=1)
app_split_num.update(app_dict)
print('========',app_split_num)
write_big_split_num(app_split_num, fpath=SPLIT_NUM_FILE)
#TODO 更新json文件有bug
#TODO write_big_split_num有bug
#TODO 添加日志
if __name__ == '__main__':
LOG_ROOT_PATH = '/Users/work/HansDev/test01/logs'
main(LOG_ROOT_PATH)
网友评论