【问题标题】:winston: Attempt to write logs with no transports - using default logger温斯顿:尝试在没有传输的情况下写入日志 - 使用默认记录器
【发布时间】:2019-01-31 16:25:24
【问题描述】:

我按照教程在我的 express 应用程序中设置了 winston (2.x) 默认记录器。更新到当前版本的 winston (3.0.0) 时,我在添加传输时遇到问题。我已关注latest docs,但我仍然在控制台中收到通知,并且根本没有创建任何日志文件:

[winston] 尝试在没有传输的情况下写入日志

logging.js

const winston = require('winston');

module.exports = function () {

  const files = new winston.transports.File({ filename: 'logfile.log' });
  const myconsole = new winston.transports.Console();

  winston.add(myconsole);
  winston.add(files);

}

index.js

const winston = require('winston');
...

require('./logging');
winston.info("Give some info");

[winston] 尝试在没有传输的情况下写入日志 {"message":"提供一些信息","level":"info"}

我做错了什么?

【问题讨论】:

    标签: javascript node.js winston


    【解决方案1】:

    在winston 3 中,您需要创建一个logger 对象,然后将transports 添加到它。

    Winston 3 有很多examples,但要适应readme,请执行以下操作:

    const logger = winston.createLogger({
      level: 'info',
      format: winston.format.json(),
      transports: [
        new winston.transports.Console(),
        new winston.transports.File({ filename: 'logfile.log' })
      ]
    });
    
    logger.info('it works!!');
    

    【讨论】:

    • 感谢您澄清winston.createLogger 的用法。自述文件指出使用“您自己的记录器”作为推荐方式。不过,我宁愿使用readme 中也列出的“默认记录器”。不幸的是,当使用winston.configure() 时,控制台中的通知仍然与我原来的问题中描述的相同。
    • 这可能是一个值得打开 GH 问题的错误。 3.0 版本仍有一些问题正在处理中,这可能就是其中之一。
    【解决方案2】:

    如果您想在 winston v3 中使用默认记录器,那么您只需将这段代码添加到您的主文件中

    const winston = require('winston')
    
    winston.add(new winston.transports.File({ filename: 'logfile.log' }))
    

    【讨论】:

    • 希望它对最近可能遇到此错误的新人有所帮助
    【解决方案3】:

    我也有类似的问题。如果我没记错的话,我必须在我的 index.js 中将要求作为函数调用。

    require('./logging')();
    

    【讨论】:

      猜你喜欢
      • 2014-01-19
      • 2018-08-22
      • 1970-01-01
      • 2015-05-07
      • 2012-08-27
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多