2013-12-12 56 views
5

我不能在我的應用程序所在的目錄是「test.log中」文件中找到?哪裏是日誌文件中的應用程序的NodeJS與溫斯頓

下面的代碼是在server.js

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

mylogger.log('Hello world'); 

我的應用程序目錄:

/ 
    app/ 
    config/ 
    public/ 
    server.js 
+2

也許是當前工作目錄... – Brad

回答

0

有趣的問題。查看文件傳輸的源代碼,看起來該目錄(如果未指定的話)是從文件名參數本身派生的。它似乎建議您使用明確指定的目錄的絕對路徑或相對路徑。

這條線是它計算出的絕對路徑。

var fullname = path.join(self.dirname, target); 

self.dirname是設置在這裏:

this.dirname = options.dirname || path.dirname(options.filename); 

所以,問題是,如果options.filename不包括目錄,這是什麼path.dirname返回?

我真的不知道,但有兩種可能我會懷疑:

  • 過程
  • 的根文件系統的當前工作目錄,因爲如果path.dirname需要剩下的最後的/,那麼它是undefinedundefined + '/test.log' 是 '/test.log'

有兩個步驟,你可以採取:

  • 檢查當前目錄和文件系統根目錄以查看它是哪個目錄。 (換句話說,測試理論)
  • 明確指定目錄(可能反正是個好主意)

https://github.com/flatiron/winston/blob/master/lib/winston/transports/file.js

+0

我在窗口中使用搜索命令,也沒有結果。我改變使用{文件名:「/test.log」},我仍然沒有出現在server.js文件 – user3044147

+0

名目錄:__dirname +「/debug.log」}這種方式還是不行 – user3044147

+0

你檢查對於任何權限問題? – Brandon

0

我寫了一個測試文件是這樣的:

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

logger.log('info', 'Hello distributed log files!'); 
logger.info('Hello again distributed logs'); 

process.exit(); 

和我發現如果我刪除了最後一行,日誌文件將被創建並正確記錄。

我用process.exit()打破我的程序,因爲我只是想測試記錄器,它看起來像它以某種方式打破了日誌文件的過程。

檢查這個問題:https://github.com/winstonjs/winston/issues/228

相關問題