我正在尝试使用cronjobs每天创建日志文件。每当我重新启动Node服务器时,它都会创建一个新的日志文件,否则它会将日志写入同一个文件。请帮忙
答案 0 :(得分:0)
请花一点时间阅读:https://www.npmjs.com/package/cron
这个:https://www.npmjs.com/package/moment
function startLogger() {
var name = (require('moment')()).format('YYYY-MM-DD')
var path = ('~/' + name);
// ONLY if (path) doesn't exist, create it. (Do yourself the code)
// Write to the end of the existent or created file:
(require('fs')).appendFile(path, 'Your log line or multiple lines', function (err) {
if (err) throw err;
console.log('Log saved successfully!');
});
}
PS:我不建议在require
内使用()
,我只是因为我没有时间而这样做,但我希望你理解它并制作自己的代码。