【问题标题】:Cronjobs and NodejsCronjobs 和 Nodejs
【发布时间】:2017-08-15 02:46:07
【问题描述】:

我正在尝试使用 cronjobs 为每天创建日志文件。每当我重新启动节点服务器时,它都会为它创建一个新的日志文件,否则它会将日志写入同一个文件。请帮忙

【问题讨论】:

  • 需要更多信息,这很模糊

标签: node.js cron scheduled-tasks


【解决方案1】:

花一点时间阅读: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,我只是因为我没有时间,但我希望你理解它并编写你自己的代码。

【讨论】:

    猜你喜欢
    • 2020-11-17
    • 2013-05-29
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2011-08-03
    • 2016-11-20
    • 2012-12-09
    • 1970-01-01
    相关资源
    最近更新 更多