在执行查询时防止将Sequelize输出到控制台?

我有一个function来检索用户的configuration文件。

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

当调用“find”函数时,它会在启动服务器的控制台上显示select语句。

 Executing (default): SELECT `id`, `firstName`, `lastName`, `emailAddress`, `password`, `passwordRecoveryToken`, `passwordRecoveryTokenExpire`, `createdAt`, `updatedAt` FROM `Users` AS `User` WHERE `User`.`emailAddress` = 'johndoe@doe.com' LIMIT 1; 

有没有办法让这个不被显示? 一些标志,我在一个configuration文件中设置的地方?

在创buildSequelize对象时,将false传递给logging参数:

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

有关更多选项,请查看文档 。

如果使用了'config / config.json'文件,那么在这种情况下在开发configuration部分下面添加'logging':false到config.json。

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

正如在其他答案中,你可以设置logging:false ,但是我认为比完全禁用日志logging更好,你可以在你的应用程序中包含日志级别。 有时你可能想看一下执行的查询,所以最好将Sequelizeconfiguration成详细级别或debugging级别。 例如(我在这里使用winston作为日志框架,但你可以使用任何其他框架):

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

只有当winston日志级别设置为debugging级别或更低的debugging级别时,才会输出SQL语句。 如果日志级别警告或信息例如SQL不会被logging