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 错误。谁能向我解释这个问题?