1 minute read

Enable SQL Query Logging

Learn how to enable query logging in Fluent to see the generated SQL queries

Enable SQL Query Logging

The Fluent drivers log the SQL they generate at the debug log level. This allows you to opt in to seeing the SQL generated without making it too noisy. This also follow the guidance from the SSWG on library logging.

Some drivers, like FluentPostgreSQL, allow this to be configured when you configure the database. For example:

app.databases.use(.postgres(
    hostname: Environment.get("DATABASE_HOST") ?? "localhost",
    port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? PostgresConfiguration.ianaPortNumber,
    username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
    password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
    database: Environment.get("DATABASE_NAME") ?? "vapor_database",
    sqlLogLevel: .info
), as: .psql)

That would log SQL statements at the info level.

To set the log level, in configure.swift (or where you set up your application) add:

app.logger.logLevel = .debug

This sets the log level to debug. When you next build and run your app, the SQL statements generated by Fluent will be logged to the console.