Skip to content

Commit e6873fd

Browse files
colorful3pedro
authored andcommitted
新增系统日志 (#63)
* feat: 新增文件上传API * feat: 改建文件上传 * fix: 删除多余的模块导入 * fix: 修改文件上传返回的key值 * feat: 增加文件上传默认配置 * chore: 更新核心库版本 * fix: 修复win系统在cmd下执行初始化插件脚本的编码问题 * fix: 删除LocalUpload下upload的多余参数 * feat: 新增系统日志功能 * refactor: 格式化日志记录格式 * chroe: 修改核心库依赖版本号 * fix: 修复上传文件到第三方服务器依然会在本地创建目录的bug
1 parent af19d41 commit e6873fd

5 files changed

Lines changed: 56 additions & 3 deletions

File tree

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Flask = "==1.0.2"
1212
Flask-SQLAlchemy = "==2.3.2"
1313
Flask-WTF = "==0.14.2"
1414
Flask-Cors = "==2.1.0"
15-
Lin-CMS = "==0.1.1b3"
15+
Lin-CMS = "==0.1.1b4"
1616

1717
[dev-packages]
1818
pytest = "*"

app/app.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
:copyright: © 2019 by the Lin team.
33
:license: MIT, see LICENSE for more details.
44
"""
5+
import json
6+
import time
57

6-
from flask import Flask
8+
from flask import Flask, request, g
79
from flask_cors import CORS
810
from lin import Lin
911

@@ -25,14 +27,53 @@ def create_tables(app):
2527
db.create_all()
2628

2729

30+
def register_before_request(app):
31+
@app.before_request
32+
def request_cost_time():
33+
g.request_start_time = time.time()
34+
g.request_time = lambda: "%.5f" % (time.time() - g.request_start_time)
35+
36+
37+
def register_after_request(app):
38+
@app.after_request
39+
def log_response(resp):
40+
log_config = app.config.get('LOG')
41+
if not log_config['REQUEST_LOG']:
42+
return resp
43+
message = '[%s] -> [%s] from:%s costs:%.3f ms' % (
44+
request.method,
45+
request.path,
46+
request.remote_addr,
47+
float(g.request_time()) * 1000
48+
)
49+
if log_config['LEVEL'] == 'INFO':
50+
app.logger.info(message)
51+
elif log_config['LEVEL'] == 'DEBUG':
52+
req_body = '{}'
53+
try:
54+
req_body = request.get_json() if request.get_json() else {}
55+
except:
56+
pass
57+
message += " data:{\n\tparam: %s, \n\tbody: %s\n} " % (
58+
json.dumps(request.args, ensure_ascii=False),
59+
req_body
60+
)
61+
app.logger.debug(message)
62+
return resp
63+
64+
2865
def create_app(register_all=True):
2966
app = Flask(__name__, static_folder='./assets')
3067
app.config.from_object('app.config.setting')
3168
app.config.from_object('app.config.secure')
69+
app.config.from_object('app.config.log')
3270
if register_all:
3371
register_blueprints(app)
3472
Lin(app)
73+
register_before_request(app)
74+
register_after_request(app)
3575
apply_cors(app)
3676
# 创建所有表格
3777
create_tables(app)
78+
3879
return app

app/config/log.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
:copyright: © 2019 by the Lin team.
3+
:license: MIT, see LICENSE for more details.
4+
"""
5+
LOG = {
6+
'LEVEL': 'DEBUG',
7+
'DIR': 'logs',
8+
'SIZE_LIMIT': 1024 * 1024 * 5,
9+
'REQUEST_LOG': True,
10+
'FILE': True
11+
}

app/extensions/file/local_uploader.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class LocalUploader(Uploader):
99

1010
def upload(self):
1111
ret = []
12+
self.mkdir_if_not_exists()
1213
site_domain = current_app.config.get('SITE_DOMAIN')\
1314
if current_app.config.get('SITE_DOMAIN') else 'http://127.0.0.1:5000'
1415
for single in self._file_storage:

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Flask-WTF==0.14.2
1010
idna==2.6
1111
itsdangerous==1.1.0
1212
Jinja2==2.10
13-
Lin-CMS==0.1.1b3
13+
Lin-CMS==0.1.1b4
1414
MarkupSafe==1.1.1
1515
pipfile==0.0.2
1616
PyJWT==1.7.1

0 commit comments

Comments
 (0)