the*_*imp 151 node.js sequelize.js
我有一个功能来检索用户的个人资料.
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);
});
});
Run Code Online (Sandbox Code Playgroud)
调用"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;
Run Code Online (Sandbox Code Playgroud)
有没有办法让这个不显示?我在某个配置文件中设置的一些标志?
vic*_*ohl 280
创建Sequelize对象时,请传递false给logging参数:
var sequelize = new Sequelize('database', 'username', 'password', {
// disable logging; default: console.log
logging: false
});
Run Code Online (Sandbox Code Playgroud)
有关更多选项,请查看文档.
Sup*_*nno 29
如果使用'config/config.json'文件,则在开发配置部分的本例中将config.json添加'logging':false.
// file config/config.json
{
{
"development": {
"username": "username",
"password": "password",
"database": "db_name",
"host": "127.0.0.1",
"dialect": "mysql",
"logging": false
},
"test": {
...
}
Run Code Online (Sandbox Code Playgroud)
Mos*_*eef 19
与其他答案一样,您可以设置logging:false,但我认为比完全禁用日志记录更好,您可以只在您的应用程序中包含日志级别.有时您可能希望查看已执行的查询,因此最好将Sequelize配置为在详细级别或调试时进行日志记录.例如(我在这里使用winston作为日志框架,但您可以使用任何其他框架):
var sequelize = new Sequelize('database', 'username', 'password', {
logging: winston.debug
});
Run Code Online (Sandbox Code Playgroud)
仅当winston日志级别设置为debug或降低调试级别时,才会输出SQL语句.如果日志级别是警告或信息,例如SQL将不会被记录
Rat*_*ker 12
所有这些答案都在创建时关闭日志记录。
但是如果我们需要关闭运行时的日志记录怎么办?
运行时我的意思是在sequelize使用new Sequelize(..函数初始化对象之后。
我查看了github 源代码,找到了一种关闭运行时登录的方法。
// Somewhere your code, turn off the logging
sequelize.options.logging = false
// Somewhere your code, turn on the logging
sequelize.options.logging = true
Run Code Online (Sandbox Code Playgroud)
这是我的回答:
简介:我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'
}
}
Run Code Online (Sandbox Code Playgroud)
并且,在环境变量中设置DB_QUERY_LEVEL为 ["query", "error"]。
结果:因此它只会在查询有错误时记录,否则不会。
参考链接: typeorm db query logging doc
希望这可以帮助!谢谢。
| 归档时间: |
|
| 查看次数: |
71324 次 |
| 最近记录: |