2017-07-06 51 views
0

我想使用winston記錄器在node應用程序的日誌文件錯誤。但winston不寫入文件。溫斯頓記錄器不寫入文件

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

logger.error('test'); 

文件somefile.log已創建但爲空。

回答

1

您需要在構造函數中定義一個級別。

var tsFormat =() => (new Date()).toLocaleTimeString(); 
var log = new (winston.Logger)({ 
    transports: [ 
     new (winston.transports.Console)({ 
      timestamp: tsFormat, 
      colorize: true, 
      level: 'info' 
     }), 
     new (winston.transports.File)({ 
      filename: 'somefile.log', 
      level: 'error' 
     }) 
    ] 
}); 


log.error('test'); // writes to log and console out 
log.info('testing info'); // writes just in console