gpt4 book ai didi

sqlalchemy - SQLAlchemy 中的友好查询日志记录

转载 作者:行者123 更新时间:2023-12-01 03:38:20 29 4
gpt4 key购买 nike

从用户的角度来看,SQLAlchemy 的查询日志似乎有点过于冗长,有时甚至有点神秘:

2015-10-02 13:51:39,500 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-10-02 13:51:39,502 INFO sqlalchemy.engine.base.Engine SELECT anon_1.shelves_title AS anon_1_shelves_title, ..., anon_1.shelves_created_at AS anon_1_shelves_created_at, anon_1.shelves_updated_at AS anon_1_shelves_updated_at, products_1.id AS products_1_id, products_1.title AS products_1_title
FROM (SELECT shelves.title AS shelves_title, ..., shelves.created_at AS shelves_created_at, shelves.updated_at AS shelves_updated_at
FROM shelves
WHERE shelves.title = ?
LIMIT ? OFFSET ?) AS anon_1 LEFT OUTER JOIN products AS products_1 ON anon_1.shelves_title = products_1.shelf_title
2015-10-02 13:51:39,502 INFO sqlalchemy.engine.base.Engine ('sample', 1, 0)
2015-10-02 13:51:39,503 INFO sqlalchemy.engine.base.Engine ROLLBACK

(不一定具有代表性,但希望足以说明问题)

可以说 Ruby on Rails 是一个很好的引用,它提供了实际数据库查询的简洁和彩色输出:

Rails SQL logging

(来自 https://code.google.com/p/pylonsquerybar/#What_Others_Have_Done)

有没有一种简单的方法可以为 SQLAlchemy 获得类似的输出? (前面提到的 Pylons Query Bar 似乎不是为与框架无关的重用而设计的。)

最佳答案

您可以将任何格式设置为记录器,您可以获得记录器:

import logging
logging.basicConfig(filename='db.log')
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

如何设置颜色和格式请参见此处:
How can I color Python logging output?

关于sqlalchemy - SQLAlchemy 中的友好查询日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906686/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com