- 解决日志服务器按每天收集日志问题,可以用作前端埋点和服务器埋点日志收集
- 解决日志服务器大文件查看问题
- 自动插入服务器时间
- 日志格式为Json格式
from flask import Flask, request
import time
import os
app = Flask(__name__)
if not os.path.exists('./logs'):
os.makedirs('./logs')
@app.route('/', methods=['GET', 'POST'])
def collect_log():
if request.method == 'GET':
data = request.args.get("data")
else:
data = request.form.get('data')
if data:
with open(get_log_path(), "a", encoding='utf-8') as f_a:
server_time = int(round(time.time() * 1000))
line = f'{data[:-1]},"server_time":{server_time}{data[-1]}'
f_a.write(f'{line}\n')
return '1' if data else 'data is null!!!'
@app.route('/logs', methods=['GET'])
def logs():
file_str = ''
log_list = []
for root, dirs, files in os.walk(r"./logs"):
for file in files:
file_path = os.path.join(root, file)
log_list.append(file_path)
fsize = os.path.getsize(file_path)
lines = 0
for _, _ in enumerate(open(file_path, 'r', encoding='utf-8')):
lines += 1
file_str += f'{file_path} {lines} {round(fsize / float(1024 * 1024), 3)}M<br>'
if log_list:
log_list.sort()
last_lines = tail(log_list[-1], 10)
file_str += f'<b>{log_list[-1]} last {len(last_lines)} line:</b> <br>' + '<br><br>'.join(last_lines)
return file_str
def tail(path, n=10):
f_size = os.path.getsize(path)
block_size = 1024*n
last_line = ""
with open(path, 'r', encoding='utf-8') as f_r:
if f_size > block_size:
max_seek_point = (f_size // block_size)
f_r.seek((max_seek_point - 1) * block_size)
elif f_size:
f_r.seek(0, 0)
lines = f_r.readlines()
if lines:
last_line = lines[-n:]
return last_line
def get_log_path():
log_path = f'./logs/app_{time.strftime("%Y-%m-%d", time.localtime(time.time()))}.log'
return log_path
if __name__ == '__main__':
app.run()