[英]Node.js - process.exit AND append the entire console.log messages in a text file
[英]Winston log files not always saved while using process.exit() - node js
我無法理解這個概念...在將日志文件保存到控制台和實際文件的程序上使用process.exit()時,所有日志事件都在控制台中打印出來,但僅打印第一個(或在第一個不打印)全部)保存到文件中。 這是該問題的快速演示:
在下面的代碼中,將創建一個Winston記錄器。 然后在numberScore函數中,為數組中的每個數字創建一個日志,最后我並行記錄所有事件-只是一個演示!
var async = require('async')
var winston = require('winston')
var moment = require('moment')
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: function () {
return moment().format('D/MM/YYYY HH:mm:ss:SSS')
},
colorize: true
}),
new (require('winston-daily-rotate-file'))({
filename: 'logs/-system.log',
datePattern: 'dd-MM-yyyy',
prepend: true,
json: false,
timestamp: function () {
return moment().format('D/MM/YYYY HH:mm:ss:SSS')
}
})
]
})
var values = [1,2,3,4,5]
var numbersScore = function(done) {
var array = []
array.push(function(callback) {
logger.info('test1')
callback()
})
values.forEach(function(number){
number += 1
console.log(number)
array.push(function(callback) {
logger.info('test2', number)
callback()
})
})
async.parallel(array, function(error, data){
console.log('done')
process.exit()
})
}
numbersScore()
上面的代碼在運行時可以在控制台中打印出來-這意味着一切正常-但是在日志文件中什么也沒有保存。
2
3
4
5
6
28/09/2016 10:57:45:911 - info: test1
28/09/2016 11:01:22:677 - info: Numbers are 2
28/09/2016 11:01:22:678 - info: Numbers are 3
28/09/2016 11:01:22:678 - info: Numbers are 4
28/09/2016 11:01:22:678 - info: Numbers are 5
28/09/2016 11:01:22:678 - info: Numbers are 6
done
注釋掉process.exit()
,日志文件將正確保存所有必需的數據。 但是...在我的情況下,我需要有process.exit()-那么解決方法是什么?
我嘗試過的
使用Winston回調-https: //github.com/winstonjs/winston#events-and-callbacks-in-winston-失敗
添加setTimeout-失敗(process.exit似乎不等待setTimeout-無論如何都會退出程序)
如您在上面的示例中看到的,使用async.parallel但是仍然沒有運氣
還有其他解決方案嗎?
相同的問題在github上公開-https: //github.com/winstonjs/winston/issues/228-但是尚未找到完全有效的解決方案。
從github的問題 :
注意:這是問題的相關部分的副本粘貼,由用戶Kegsay編寫。
對於它的價值,您可以繞開setTimeout
的使用,但是您需要獲取底層的_stream
:
// log then exit(1)
logger.error("The thing to log", function(err) {
var numFlushes = 0;
var numFlushed = 0;
Object.keys(logger.transports).forEach(function(k) {
if (logger.transports[k]._stream) {
numFlushes += 1;
logger.transports[k]._stream.once("finish", function() {
numFlushed += 1;
if (numFlushes === numFlushed) {
process.exit(1);
}
});
logger.transports[k]._stream.end();
}
});
if (numFlushes === 0) {
process.exit(1);
}
});
如上所述,僅使用回調函數不足以使其正確刷新到文件。 對我來說,這感覺像是個蟲子。
請注意,我實際上正在偵聽結束事件: https : //nodejs.org/api/stream.html#stream_event_finish而不是使用winston的flush或close事件,因為這也不足以使其正確記錄:/
嘗試這樣的事情,它對我有用(基於Winston):
logger.on('logging', function (transport, level, msg, meta) {
if (msg == "equal your message" && transport.name == "file") {
var id = setInterval(function () {
//when it is done to write the file, it will enter the id condition
if (transport._stream._writableState.bufferedRequestCount==0) {
exitProcess(0);
}
},1000)
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.