如何在Winston/Node.js中设置日志级别 [英] How to set log level in Winston/Node.js

查看:229
本文介绍了如何在Winston/Node.js中设置日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Winston日志记录与Node.js应用程序一起使用,并已定义了文件传输方式.在整个代码中,我使用logger.errorlogger.warnlogger.info登录.

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.

我的问题是,如何指定日志级别?是否可以设置配置文件和值,以便仅记录适当的日志消息?例如,我希望日志级别在我的开发环境中为信息",而在生产环境中为错误".

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.

推荐答案

此处

来自该文档:

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.

那应该导致类似的事情

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.File)({ filename: 'somefile.log', level: 'error' })
  ]
});

对于该文档,还请注意,从2.0版开始,它公开了setLevel方法以在运行时进行更改.在该文档的使用日志级别部分中查找.

Per that doc, note also that as of 2.0, it exposes a setLevel method to change at runtime. Look in the Using Log Levels section of that doc.

这篇关于如何在Winston/Node.js中设置日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆