如何在Vapor 3 / Fluent中记录SQL语句? [英] How do I log SQL statements in Vapor 3/Fluent?
问题描述
在Vapor 2中,您可以执行以下操作:
It looks like in Vapor 2 you could do something like:
let query = <some fluent query object>
logger?.debug(query)
,它将打印出完整的SQL语句,但是我现在在Vapor 3中没有看到有关如何执行此操作的任何文档。
and it would print out the full SQL statement, but I'm not seeing any documentation of how to do that now in Vapor 3.
如何查看我的正在生成什么SQL QueryBuilder
?
推荐答案
感谢尼克的评论,他为我指出了正确的文档集。这可以通过使用 enableLogging
方法来完成。所以现在我的configure.swift包含以下代码:
Thanks to Nick in the comments, who pointed me to the right set of docs. This can be accomplished by using the enableLogging
method. So now my configure.swift includes this code:
let dbConfig: PostgreSQLDatabaseConfig
if let url = Environment.get("DATABASE_URL"), let psqlConfig = PostgreSQLDatabaseConfig(url: url, transport: .unverifiedTLS) {
dbConfig = psqlConfig
} else {
dbConfig = ...something for the local db...
}
let postgresql = PostgreSQLDatabase(config: dbConfig)
/// Register the configured SQLite database to the database config.
var databases = DatabasesConfig()
databases.enableLogging(on: .psql)
databases.add(database: postgresql, as: .psql)
services.register(databases)
重要的一行是底部的第三行。一段时间以来,我一直尝试在 PostgreSQLDatabaseConfig
上启用调试,因此对于以后的任何人,请注意,您正在 DatabasesConfig
对象。
The important line being the third from the bottom. For a while I was trying to enable debugging on PostgreSQLDatabaseConfig
, so to anyone in the future, take note that you're enabling it on the DatabasesConfig
object instead.
这篇关于如何在Vapor 3 / Fluent中记录SQL语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!