微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我们如何使用节点js中的pm2 api显示pm2应用程序的最新日志?

如何解决我们如何使用节点js中的pm2 api显示pm2应用程序的最新日志?

我正在尝试制作一个 api,我们可以在其中获取 pm2 应用程序的最新日志,但我不知道如何去做。

//Helper function to get the logs
async function getLogs(id,out,numLines){
  for (const elem of currentInfo){
    //Finds the appropriate process
    if (elem.pm_id == id){
      //Gets the appropiate file
      let file = out == "true" ? elem.pm2_env.pm_out_log_path : elem.pm2_env.pm_err_log_path;
      
      //Uses an external library to get the proper lines
      let logs = await readLastLines.read(file,numLines);

      return {logs: logs}
    }
  }
  return {logs: undefined};
}

//Log route,sends the proper part of the necessary log file.
//Param 1: id - id of the process in the pm2 daemon
//Param 2: out - true if requesting the stdout logs,otherwise gets the stderr logs
//Param 3: numLines - number of lines of the log file requested (gets the most recent lines)
app.get('/logs/:id/:out/:numLines',async function(req,res){
  let temp = await getLogs(req.params.id,req.params.out,req.params.numLines)
  res.send(temp);
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。