Sequelize — Disable SQL Query Logging

Sequelize is a Node.js ORM for all SQL databases, like MySQL, MariaDB, PostgreSQL, SQLite, and Microsoft SQL Server. By default, it logs all SQL queries to the console. This is helpful during development.

In production environments, the SQL query logging may become noisy and just fill up your logs. This tutorial shows you how to disable the logging of SQL queries in Sequelize.

Sequelize Series Overview

Disable SQL Query Logging in the Database Connector

Logging every SQL query may become noisy even during development. Especially if you‘re fixing a bug and all your console logs are full of SQL statements instead of more important log messages.

You can disable Sequelize’s query logging by default when creating a new database connector. Use the options.logging configuration to disable query logging:

const Sequelize = require('sequelize')

const sequelize = new Sequelize(database, user, password, {  
  dialect: 'mysql',

  logging: false
})

This disables SQL query logging for all database queries you’re running through your Sequelize models.

Disable SQL Query Logging for a Single Query

You can also disable logging of individual SQL queries. The query options support a logging configuration that you may use to override the default logging setup.

In case the default logging will print all SQL statements to the console, you may disable logging for a single query like this:

const User = require('../sequelize-models/user')

const user = await User.findOne({  
  logging: false,
  where: { id: 2207 }
})

This also works the other way around: you can enable query logging for individual queries. Even if you globally disabled logging, you can enable the logging for individual queries like this:

const User = require('../sequelize-models/user')

const user = await User.findOne({  
  logging: console.log,
  where: { id: 2207 }
})

Starting in Sequelize v5, the logging option supports the boolean false value to disable logging. When enabling logging you should provide a logging function instead of the boolean true.

You can still use the boolean true value to enable logging. Sequelize will then print a deprecation warning to the terminal hinting you should use a logging function.

Disable SQL Query Logging in Production

Another setup is that you only disable query logging during production:

const Sequelize = require('sequelize')

const sequelize = new Sequelize(database, user, password, {  
  dialect: 'mysql',

  logging: process.env.NODE_ENV === 'production' ? false : console.log
})

That’s it. Enjoy SQL statements in your logs!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.