我有这个python flask
代码:
@app.errorhandler(404)
def not_found(e):
logg = open("server_log_404.txt", 'a')
logg.write("\r\n\r\n========================\n")
logg.write("%s\n" % datetime.datetime.today().ctime())
logg.write("%s\n" % request.url )
logg.write("%s" % request.headers)
logg.write("%s\n" % str(request.form))
logg.write("%s\n" % str(request.args))
logg.close()
return render_template('404.html'), 404
我需要在装饰中更改功能logg
以记录更多页面。我需要这样的功能:
@app.errorhandler(404)
@make_logg
def not_found(e):
return render_template('404.html'), 404
有可能吗?
答案 0 :(得分:2)
只需将其转换为装饰器:
from functools import wraps
def make_logg(f):
@wraps(f)
def wrapper(*args, **kwargs):
logg = open("server_log_404.txt", 'a')
logg.write("\r\n\r\n========================\n")
logg.write("%s\n" % datetime.datetime.today().ctime())
logg.write("%s\n" % request.url )
logg.write("%s" % request.headers)
logg.write("%s\n" % str(request.form))
logg.write("%s\n" % str(request.args))
logg.close()
return f(*args, **kwargs)
return wrapper