var date = new Date();
var logPath = __dirname + '/log/transcript.' + date.getUTCHours() + ':' + date.getUTCMinutes() + ':' + date.getUTCSeconds() + '.' + date.getUTCDate() + '-' + date.getUTCMonth() + '-' + date.getUTCFullYear() + '.log';
var logger = {
deglog: function (degree, value) {
var now = new Date().toUTCString();
stream = fs.createWriteStream(logPath, {
'flags': 'a+',
'encoding': 'utf8'
});
stream.write(now + ' ');
stream.write(degree + ': ');
stream.write(value + '\n');
stream.end();
},
log: function (value) {
this.deglog('INFO', value);
},
warning: function (value) {
this.deglog('WARN', value);
},
error: function (value) {
this.deglog('ERROR', value);
}
}
如果文件不存在,上面的代码片段应该创建一个文件,但我收到 ENOENT 错误。谁能给我解释一下这个问题吗?
最佳答案
该错误与使用标志无关,只是文件名中包含非法字符。摆脱它们,它就会正常工作。
transcript.6:25:16.13-9-2013.log
│ │
└──└── illegal characters
关于javascript - ENOENT 为 'a+' 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19342542/