Prevent Sequelize from outputting SQL to the console on execution of query?

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

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` = 'johndoe@doe.com' LIMIT 1; 

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

回答1:

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.



回答2:

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


回答3:

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



回答4:

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


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