如何在 Winston/Node.js 中设置日志级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15535937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set log level in Winston/Node.js
提问by Silent User
I am using Winston logging with my Node.js app and have defined a file transport. Throughout my code, I log using either logger.error, logger.warn, or logger.info.
我在 Node.js 应用程序中使用 Winston 日志记录并定义了文件传输。在我的代码,我登录使用两种logger.error,logger.warn或logger.info。
My question is, how do I specify the log level? Is there a config file and value that I can set so that only the appropriate log messages are logged? For example, I'd like the log level to be "info" in my development environment but "error" in production.
我的问题是,如何指定日志级别?是否有我可以设置的配置文件和值,以便仅记录适当的日志消息?例如,我希望我的开发环境中的日志级别为“信息”,但生产环境中的日志级别为“错误”。
采纳答案by bryanmac
Looks like there is a level option in the options passed covered here
看起来这里传递的选项中有一个级别选项
From that doc:
从那个文档:
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ level: 'error' }),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
Now, those examples show passing level in the option object to the console transport. When you use a file transport, I believe you would pass an options object that not only contains the filepath but also the level.
现在,这些示例显示了将选项对象中的级别传递给控制台传输。当您使用文件传输时,我相信您会传递一个选项对象,该对象不仅包含文件路径,还包含级别。
That should lead to something like:
这应该导致类似的事情:
var logger = new (winston.Logger)({
transports: [
new (winston.transports.File)({ filename: 'somefile.log', level: 'error' })
]
});
Per that doc, note also that as of 2.0, it exposes a setLevel method to change at runtime. Look in the Using Log Levelssection of that doc.
根据该文档,还要注意从 2.0 开始,它公开了一个 setLevel 方法以在运行时更改。查看该文档的使用日志级别部分。
回答by AndreasPizsa
If you are using the default logger, you can adjust the log levels like this:
如果您使用的是默认 logger,您可以像这样调整日志级别:
const winston = require('winston');
// ...
winston.level = 'debug';
will set the log level to 'debug'. (Tested with winston 0.7.3, default logger is still around in 3.2.1).
将日志级别设置为“调试”。(使用 winston 0.7.3 测试,默认记录器仍在 3.2.1 中)。
However, the documentation recommends creating a new loggerwith the appropriate log levels and then using that logger:
但是,文档建议创建一个具有适当日志级别的新记录器,然后使用该记录器:
const myLogger = winston.createLogger({
level: 'debug'
});
myLogger.debug('hello world');
If you are already using the default loggerin your code base this may require you to replace all usages with this new logger that you are using:
如果您已经在代码库中使用默认记录器,这可能需要您用您正在使用的这个新记录器替换所有用法:
const winston = require('winston');
// default logger
winston.log('debug', 'default logger being used');
// custom logger
myLogger.log('debug', 'custom logger being used');
回答by Chen
There are 6 default levels in winston: silly=0(lowest), debug=1, verbose=2, info=3, warn=4, error=5(highest)
winston 有 6 个默认级别:silly=0(最低)、debug=1、verbose=2、info=3、warn=4、error=5(最高)
While creating the logger transports, you can specify the log level like:
在创建记录器传输时,您可以指定日志级别,如:
new (winston.transports.File)({ filename: 'somefile.log', level: 'warn' })
Above code will set log level to warn, which means silly, verboseand infowill not be output to somefile.log, while warn, debugand errorwill.
上面的代码将日志级别设置为warn,这意味着silly,verbose并且info不会输出到 somefile.log ,而warn,debug并且error会。
You can also define your own levels:
您还可以定义自己的级别:
var myCustomLevels = {
levels: {
foo: 0,
bar: 1,
baz: 2,
foobar: 3
}
};
var customLevelLogger = new (winston.Logger)({ levels: myCustomLevels.levels });
customLevelLogger.foobar('some foobar level-ed message');
Note that it's better to always include the 6 predefined levels in your own custom levels, in case somewhere used the predefined levels.
请注意,最好始终将 6 个预定义级别包含在您自己的自定义级别中,以防在某处使用预定义级别。
回答by weekens
You can change the logging level in runtime by modifying the levelproperty of the appropriate transport:
您可以通过修改level相应传输的属性来更改运行时的日志记录级别:
var log = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ level : 'silly' })
]
});
...
// Only messages with level 'info' or higher will be logged after this.
log.transports.Console.level = 'info';
I guess, it works similarly for file but I haven't tried that.
我想,它对文件的工作方式类似,但我还没有尝试过。
回答by Yann VR
If you want to change the log level on the fly. Like for when you need to trace production issue for short amount of time; then revert to error log level. You can use a dynamic logger provided you can expose a service on the web https://github.com/yannvr/Winston-dynamic-loglevel
如果您想即时更改日志级别。就像您需要在短时间内跟踪生产问题一样;然后恢复到错误日志级别。您可以使用动态记录器,前提是您可以在网络上公开服务https://github.com/yannvr/Winston-dynamic-loglevel

