python - 请求操作之前和之后的烧瓶不起作用/打印任何东西
问题描述
所以我有一个奇怪的错误,之前和之后的操作都不起作用。他们以前写入 requests.log 文件,但现在这不起作用,并且控制台的常规打印语句不起作用,我不知道为什么:
# function to handle bson type returned by mongodb
# taken from https://stackoverflow.com/a/18405626
def parse_json(data):
return json.loads(json_util.dumps(data))
app = flask.Flask(__name__)
api = Api(app)
#we will be logging the API end point that was accessed, the time it was accessed, and how long it took to address the request
#all logging will be to a local file, requests.log.
@app.before_request
def start_timer():
g.start = time.time()
@app.after_request
def log_request(response):
now = time.time()
duration = round(now - g.start, 2)
print(response.get_data()) #not working
f = open("requests.log", "a+")
f.write("testing") #also not working
f.write("The following request took ({}) seconds".format(duration)) #not working
f.write("\n")
f.close()
print("dhefei") #not working
app.logger.info('Processing default request') #not working
print(request.url, request.remote_addr,file=sys.stderr)
return response
这些操作之外的任何打印语句都可以工作。
解决方案
我测试了你的代码,看起来不错!但更新很少。
from flask import g, request, Flask
import logging
import time
import sys
logging.basicConfig(
format='%(asctime)-15s %(levelname)-8s %(name)-1s: %(message)s',
level=logging.DEBUG)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
# function to handle bson type returned by mongodb
# taken from https://stackoverflow.com/a/18405626
def parse_json(data):
return json.loads(json_util.dumps(data))
app = Flask(__name__)
@app.route('/')
def hello_world():
time.sleep(4)
return 'Hello, World!'
@app.before_request
def start_timer():
g.start = time.time()
@app.after_request
def log_request(response):
now = time.time()
duration = round(now - g.start, 2)
logging.info(response.get_data()) #not working
logging.info("testing") #also not working
logging.info("The following request took ({}) seconds".format(duration)) #not working
logging.info("dhefei") #not working
logging.info('Processing default request') #not working
return response
if __name__ == '__main__':
app.run(host='localhost', port=9999)
在控制台中:
python3 flaskapp.py
* Serving Flask app "flaskapp" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
2021-03-15 11:13:13,723 INFO werkzeug: * Running on http://localhost:9999/ (Press CTRL+C to quit)
2021-03-15 11:13:19,982 INFO root: b'Hello, World!'
2021-03-15 11:13:19,982 INFO root: testing
2021-03-15 11:13:19,983 INFO root: The following request took (4.0) seconds
2021-03-15 11:13:19,983 INFO root: dhefei
2021-03-15 11:13:19,983 INFO root: Processing default request
2021-03-15 11:13:19,985 INFO werkzeug: 127.0.0.1 - - [15/Mar/2021 11:13:19] "GET / HTTP/1.1" 200 -
推荐阅读
- c - 如何在while循环中正确使用fscanf?
- bash - 我想将所有文件名放入特定路径中的变量中
- java - 即使应用程序关闭或打开,如何在 android 版本 9.0 中每 1 分钟触发一次广播?
- javascript - 嵌套数组按字段 Mnogoose Node.js 排序
- javascript - 如何设计一个每秒都需要更换的减速机?
- java - 中缀算法仅返回最后一个数字
- reactjs - 如何在 ReactJS 中的模糊路由中停止第一次匹配时的路由器匹配
- typescript - 量角器 if else with expect(condition)
- azure-vm-scale-set - Vm 规模集不适用于内部标准 sku Azure 负载均衡器后端池
- java - 在独立的 android 库项目中使用 dagger 2