[node.js] Prevent Sequelize from outputting SQL to the console on execution of query?

I have a function to retrieve a user's profile.

app.get('/api/user/profile', function (request, response)
{
  // Create the default error container
  var error = new Error();

  var User = db.User;
  User.find({
    where: { emailAddress: request.user.username}
  }).then(function(user)
  {
    if(!user)
    {
      error.status = 500; error.message = "ERROR_INVALID_USER"; error.code = 301;
      return next(error);
    }

    // Build the profile from the user object
    profile = {
      "firstName": user.firstName,
      "lastName": user.lastName,
      "emailAddress": user.emailAddress
    }
    response.status(200).send(profile);
  });
});

When the "find" function is called it displays the select statement on the console where the server was started.

Executing (default): SELECT `id`, `firstName`, `lastName`, `emailAddress`, `password`, `passwordRecoveryToken`, `passwordRecoveryTokenExpire`, `createdAt`, `updatedAt` FROM `Users` AS `User` WHERE `User`.`emailAddress` = '[email protected]' LIMIT 1;

Is there a way to get this not to be display? Some flag that I set in a config file somewhere?

This question is related to node.js sequelize.js

The answer is


Based on this discussion, I built this config.json that works perfectly:

{
  "development": {
    "username": "root",
    "password": null,
    "logging" : false,
    "database": "posts_db_dev",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "operatorsAliases": false 
  }
}

As in other answers, you can just set logging:false, but I think better than completely disabling logging, you can just embrace log levels in your app. Sometimes you may want to take a look at the executed queries so it may be better to configure Sequelize to log at level verbose or debug. for example (I'm using winston here as a logging framework but you can use any other framework) :

var sequelize = new Sequelize('database', 'username', 'password', {
  logging: winston.debug
});

This will output SQL statements only if winston log level is set to debug or lower debugging levels. If log level is warn or info for example SQL will not be logged


All of these answers are turned off the logging at creation time.

But what if we need to turn off the logging on runtime ?

By runtime i mean after initializing the sequelize object using new Sequelize(.. function.

I peeked into the github source, found a way to turn off logging in runtime.

// Somewhere your code, turn off the logging
sequelize.options.logging = false

// Somewhere your code, turn on the logging
sequelize.options.logging = true 

If 'config/config.json' file is used then add 'logging': false to the config.json in this case under development configuration section.

  // file config/config.json
  {
      {
      "development": {
        "username": "username",
        "password": "password",
        "database": "db_name",
        "host": "127.0.0.1",
        "dialect": "mysql",
        "logging": false
      },
      "test": {
    ...
   }

Here is my answer:

Brief: I was using typeorm as a ORM library. So, to set the query logging level I have used the following option instead of directly setting the logging option as false.

Solution: File name - ormconfig.ts

{
    'type': process.env.DB_DRIVER,
    'host': process.env.DB_HOST,
    'port': process.env.DB_PORT,
    'username': process.env.DB_USER,
    'password': process.env.DB_PASS,
    'database': process.env.DB_NAME,
    'migrations': [process.env.MIGRATIONS_ENTITIES],
    'synchronize': false,
    'logging': process.env.DB_QUERY_LEVEL,
    'entities': [
        process.env.ORM_ENTITIES
    ],
    'cli': {
        'migrationsDir': 'migrations'
     }
}

And, in the envrionment variable set the DB_QUERY_LEVEL as ["query", "error"].

Result: As a result it will log only when the query has error else it won't.

Ref link: typeorm db query logging doc

Hope this helps! Thanks.


I am using Sequelize ORM 6.0.0 and am using "logging": false as the rest but posted my answer for latest version of the ORM.

const sequelize = new Sequelize(
        process.env.databaseName,
        process.env.databaseUser,
        process.env.password,
        {
            host: process.env.databaseHost,
            dialect: process.env.dialect,
            "logging": false,
            define: {
                // Table names won't be pluralized.
                freezeTableName: true,
                // All tables won't have "createdAt" and "updatedAt" Auto fields.
                timestamps: false
            }
        }
    );

Note: I am storing my secretes in a configuration file .env observing the 12-factor methodology.


I solved a lot of issues by using the following code. Issues were : -

  1. Not connecting with database
  2. Database connection Rejection issues
  3. Getting rid of logs in console (specific for this).
const sequelize = new Sequelize("test", "root", "root", {
  host: "127.0.0.1",
  dialect: "mysql",
  port: "8889",
  connectionLimit: 10,
  socketPath: "/Applications/MAMP/tmp/mysql/mysql.sock",
  // It will disable logging
  logging: false
});