thenetimp
thenetimp

Reputation: 9820

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?

Upvotes: 274

Views: 173176

Answers (10)

victorkt
victorkt

Reputation: 14552

When you create your Sequelize object, pass false to the logging parameter:

var sequelize = new Sequelize('database', 'username', 'password', {
  
  // disable logging; default: console.log
  logging: false

});

For more options, check the docs.

Upvotes: 534

Supawat Pusavanno
Supawat Pusavanno

Reputation: 3356

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": {
    // ...
  }
}

Upvotes: 58

Antti A
Antti A

Reputation: 500

Even if you have logging turned on, you can disable logging on one query by adding 'logging: false' as its option:

  User.findAll(
   { where: { emailAddress: request.user.username}
  }, {logging: false}
  )
  .then(function(user)....

More info from https://sequelize.org/master/class/lib/model.js~Model.html#static-method-findAll

Upvotes: 8

Swayam Gupta
Swayam Gupta

Reputation: 11

new Sequelize({
    host: "localhost",
    database: "database_name",
    dialect: "mysql",
    username: "root",
    password: "password",
    logging: false        // for disable logs
})

Upvotes: 1

user12602844
user12602844

Reputation:

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
});

Upvotes: 2

Vikash Choudhary
Vikash Choudhary

Reputation: 1609

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.

Upvotes: 6

Ratul Sharker
Ratul Sharker

Reputation: 8013

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 

Upvotes: 20

STREET MONEY
STREET MONEY

Reputation: 594

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.

Upvotes: 1

Stephen Hulme
Stephen Hulme

Reputation: 39

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 
  }
}

Upvotes: 3

Mostafa Abdellateef
Mostafa Abdellateef

Reputation: 1155

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

Upvotes: 45

Related Questions