![](/img/trans.png)
[英]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.