【发布时间】:2021-03-15 16:16:19
【问题描述】:
所以我有一个奇怪的错误,之前和之后的操作都不起作用。他们之前写入 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
这些操作之外的任何打印语句都可以工作。
【问题讨论】:
标签: python flask flask-restful