防止 Sequelize 在执行查询时将 SQL 输出到控制台?
Prevent Sequelize from outputting SQL to the console on execution of query?
我有一个检索用户个人资料的功能。
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;
有没有办法让它不显示?我在某个配置文件中设置的一些标志?
创建 Sequelize 对象时,将 false
传递给 logging
参数:
var sequelize = new Sequelize('database', 'username', 'password', {
// disable logging; default: console.log
logging: false
});
如需更多选项,请查看 docs。
如果使用 config/config.json
文件,则将 "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
,但我认为比完全禁用日志记录更好的是,您可以在应用程序中接受日志级别。有时您可能想查看已执行的查询,因此最好将 Sequelize 配置为在详细或调试级别记录。例如(我在这里使用 winston 作为日志记录框架,但您可以使用任何其他框架):
var sequelize = new Sequelize('database', 'username', 'password', {
logging: winston.debug
});
仅当 winston 日志级别设置为调试或更低的调试级别时,才会输出 SQL 语句。如果日志级别是警告或信息,例如 SQL 将不会被记录
基于这个讨论,我构建了这个 config.json
完美运行的:
{
"development": {
"username": "root",
"password": null,
"logging" : false,
"database": "posts_db_dev",
"host": "127.0.0.1",
"dialect": "mysql",
"operatorsAliases": false
}
}
我正在使用 Sequelize ORM 6.0.0 并使用 "logging": false 作为其余部分,但发布了我对最新版本 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
}
}
);
注意:我将我的秘密存储在配置文件中.env
遵守 12 因素方法。
所有这些答案在创建时都关闭了日志记录。
但是如果我们需要在运行时关闭日志记录怎么办?
我所说的运行时是指在使用 new Sequelize(..
函数初始化 sequelize
对象之后。
我查看了 github source,找到了一种在运行时关闭日志记录的方法。
// Somewhere your code, turn off the logging
sequelize.options.logging = false
// Somewhere your code, turn on the logging
sequelize.options.logging = true
我使用下面的代码解决了很多问题。
问题是:-
- 未连接数据库
- 数据库连接拒绝问题
- 删除控制台中的日志(特定于此)。
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
});
这是我的回答:
简介:我使用 typeorm
作为 ORM 库。因此,为了设置查询日志记录级别,我使用了以下选项而不是直接将日志记录选项设置为 false
.
解决方法:文件名-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'
}
}
并且,在环境变量中将 DB_QUERY_LEVEL
设置为 ["query"、"error"]。
结果: 结果只有当查询有错误时才会记录,否则不会记录。
参考 link: typeorm db query logging doc
希望对您有所帮助!
谢谢。
new Sequelize({
host: "localhost",
database: "database_name",
dialect: "mysql",
username: "root",
password: "password",
logging: false // for disable logs
})
即使您打开了日志记录,您也可以通过添加 'logging: false' 作为其选项来禁用对一个查询的日志记录:
User.findAll(
{ where: { emailAddress: request.user.username}
}, {logging: false}
)
.then(function(user)....
更多信息来自 https://sequelize.org/master/class/lib/model.js~Model.html#static-method-findAll
我有一个检索用户个人资料的功能。
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;
有没有办法让它不显示?我在某个配置文件中设置的一些标志?
创建 Sequelize 对象时,将 false
传递给 logging
参数:
var sequelize = new Sequelize('database', 'username', 'password', {
// disable logging; default: console.log
logging: false
});
如需更多选项,请查看 docs。
如果使用 config/config.json
文件,则将 "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
,但我认为比完全禁用日志记录更好的是,您可以在应用程序中接受日志级别。有时您可能想查看已执行的查询,因此最好将 Sequelize 配置为在详细或调试级别记录。例如(我在这里使用 winston 作为日志记录框架,但您可以使用任何其他框架):
var sequelize = new Sequelize('database', 'username', 'password', {
logging: winston.debug
});
仅当 winston 日志级别设置为调试或更低的调试级别时,才会输出 SQL 语句。如果日志级别是警告或信息,例如 SQL 将不会被记录
基于这个讨论,我构建了这个 config.json
完美运行的:
{
"development": {
"username": "root",
"password": null,
"logging" : false,
"database": "posts_db_dev",
"host": "127.0.0.1",
"dialect": "mysql",
"operatorsAliases": false
}
}
我正在使用 Sequelize ORM 6.0.0 并使用 "logging": false 作为其余部分,但发布了我对最新版本 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
}
}
);
注意:我将我的秘密存储在配置文件中.env
遵守 12 因素方法。
所有这些答案在创建时都关闭了日志记录。
但是如果我们需要在运行时关闭日志记录怎么办?
我所说的运行时是指在使用 new Sequelize(..
函数初始化 sequelize
对象之后。
我查看了 github source,找到了一种在运行时关闭日志记录的方法。
// Somewhere your code, turn off the logging
sequelize.options.logging = false
// Somewhere your code, turn on the logging
sequelize.options.logging = true
我使用下面的代码解决了很多问题。 问题是:-
- 未连接数据库
- 数据库连接拒绝问题
- 删除控制台中的日志(特定于此)。
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
});
这是我的回答:
简介:我使用 typeorm
作为 ORM 库。因此,为了设置查询日志记录级别,我使用了以下选项而不是直接将日志记录选项设置为 false
.
解决方法:文件名-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'
}
}
并且,在环境变量中将 DB_QUERY_LEVEL
设置为 ["query"、"error"]。
结果: 结果只有当查询有错误时才会记录,否则不会记录。
参考 link: typeorm db query logging doc
希望对您有所帮助! 谢谢。
new Sequelize({
host: "localhost",
database: "database_name",
dialect: "mysql",
username: "root",
password: "password",
logging: false // for disable logs
})
即使您打开了日志记录,您也可以通过添加 'logging: false' 作为其选项来禁用对一个查询的日志记录:
User.findAll(
{ where: { emailAddress: request.user.username}
}, {logging: false}
)
.then(function(user)....
更多信息来自 https://sequelize.org/master/class/lib/model.js~Model.html#static-method-findAll