慢日志查询python flask sqlalchemy慢日志记录

时间:2023-12-05 14:51:26
engine = create_engine(ProdConfig.SQLALCHEMY_DATABASE_URI, echo=True)

app = Flask(__name__)
app.config['FLASKY_DB_QUERY_TIMEOUT'] = 0.0001 # 设置sql执行超时时间,#记录执行时间超过 0.0001秒的
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True # 断开设置
app.config['SQLALCHEMY_RECORD_QUERIES'] = True # 启用慢查询记录功能 @app.after_request
def after_request(response):
for query in get_debug_queries():
if query.duration >= app.config['FLASKY_DB_QUERY_TIMEOUT']:
print('#####Slow query:%s \nParameters:%s \nDuration:%fs\nContext:%s\n #####' %
(query.statement, query.parameters, query.duration,query.context)) # 打印超时sql执行信息
return response @app.teardown_request
def handle_teardown_request(ex):
db.session.remove()

https://blog.csdn.net/baidu_30809315/article/details/85319167