Python使用日志
2020-05-07 本文已影响0人
Odven
使用flask测试日志
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import logging
import json
from logging.handlers import RotatingFileHandler
from flask import Flask, make_response, jsonify, request
# logging.basicConfig(level=logging.DEBUG)
# 设置日志的记录等级
logging.basicConfig(level=logging.DEBUG)
# 创建日志记录器,指明日志保存的路径,每个日志文件的最大大 小,保存日志文件个数上限
file_log_handler = RotatingFileHandler('logs/log',
maxBytes=1024 * 1024 * 100, backupCount=10)
# 创建日志记录的格式
formatter = logging.Formatter('%(levelname)s %(filename)s:%(lineno)d %(message)s')
# 为刚才创建的日志记录器设置日志记录格式
file_log_handler.setFormatter(formatter)
# 为全局的日志工具对象(flask app使用的)提埃纳甲日志记录器
logging.getLogger().addHandler(file_log_handler)
app = Flask(__name__)
@app.route("/", methods=["GET"])
def app_index():
logging.info("test python logging module")
resp_json = {
"app_index": "用flask测试日志功能"
}
return json.dumps(resp_json, ensure_ascii=False), 200, {"Content-Type": "application/json"}
@app.route("/test_make_response", methods=["GET"])
def test_make_response():
logging.info('test make response')
dict_json = {"name": "gao",
"mele": "boy",
"remote_addr": request.remote_addr}
resp = make_response(json.dumps(dict_json))
resp.headers["Content-Type"] = "application/json"
resp.set_cookie("name", "gao")
return resp, 800
if __name__ == '__main__':
app.run(debug=True)