2013-05-20 71 views
0

我試圖使用winston記錄器,但它似乎有一個奇怪的行爲時使用的文件傳輸。也許這是我錯過的東西,我無法弄清楚。 我創建了一個簡單的例子來說明這個問題。它使用摩卡進行測試。winston文件傳輸問題

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只是用於測試的測試;-)

回答

0

我有完全相同的問題。這顯然是摩卡和winston文件記錄的一個奇怪的副作用。

如果做了以下的測試和更換全球描述和它與我自己的函數功能:

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

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

,然後直接與節點執行的測試功能。在這種情況下,溫斯頓輸出就像預期的那樣。當再次使用摩卡執行相同的測試文件時,您將再次缺少日誌條目。

似乎winston文件刷新不會在用mocha執行時觸發。