How do I log SQL statements in Vapor 3/Fluent?

我是研究僧i 提交于 2019-12-07 07:55:16

问题


It looks like in Vapor 2 you could do something like:

let query = <some fluent query object>
logger?.debug(query)

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.

How can I see what SQL is being generated by my QueryBuilder?


回答1:


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)

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.



来源:https://stackoverflow.com/questions/53023892/how-do-i-log-sql-statements-in-vapor-3-fluent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!