【问题标题】:winston file transport issue温斯顿文件传输问题
【发布时间】:2013-05-15 00:39:27
【问题描述】:

我正在尝试使用 winston 记录器,但在与文件传输一起使用时它似乎有一个奇怪的行为。也许这是我想念的东西,我无法弄清楚。 我创建了一个简单的例子来说明这个问题。它使用 mocha 进行测试。

var log = require('winston')
log.add(log.transports.File, { filename: 'output.log' });

describe('Logger', function() {
        it('should save the 1st message', function(done) {
            log.info('1: this is the 1st message', function() {
                console.log('done 1')
                done()
            })
        })
        it('should save the 2nd message', function(done) {
            console.log('before test 2')
            log.info('2: this is the 2nd message', function() {
                console.log('done 2')
                done()
            })
        })
        it('should save the 3rd message', function(done) {
            log.info('3: this is the 3rd message', function() {
                console.log('done 3')
                done()
            })
        })
    }
)

第一条消息保存在 output.log 文件中,但不保存其他消息。实际上,只调用了第一个测试的回调。 'before test 2' 已打印,但 'done 2' 未打印...并且第二条消息也未保存。

但是,当我评论第二行 (log.add(log.transports.File,...) 时,它表现正常,在控制台中显示所有消息。是我遗漏了什么还是一个错误?

我使用的 winston 版本是 0.7.1。

提前致谢。

ps:console.log 仅用于测试测试;-)

【问题讨论】:

    标签: javascript logging winston


    【解决方案1】:

    我遇到了完全相同的问题。这显然是 mocha 和 winston 文件记录的奇怪副作用。

    如果已经完成以下测试并用我自己的函数替换了全局描述和它的功能:

      describe = function(name, func) {
        return func();
      };
    
      it = function(name, func) {
        return func(function() {});
      };
    

    然后直接用node.在这种情况下,winston 的输出与预期的一样。当再次使用 mocha 执行相同的测试文件时,您又会丢失日志条目。

    使用 mocha 执行时似乎不会触发 winston 文件刷新。

    【讨论】:

      猜你喜欢
      • 2014-12-10
      • 2016-12-13
      • 1970-01-01
      • 2017-09-13
      • 2019-10-28
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      相关资源
      最近更新 更多