如何进行pm2日志格式化?

问题描述 投票:0回答:1

嗨,我已经实现了 pm2。现在我在每行中获取时间戳。

//当前日志

2021-05-04T00:34:49:       { MongoError: server instance pool was destroyed
2021-05-04T00:34:49:           at Function.MongoError.create (C:\Article\snode_modules\mongodb-core\lib\error.js:29:11)
2021-05-04T00:34:49:           at basicWriteValidations (C:\Articles\node_modules\mongodb-core\lib\topologies\server.js:448:51)
2021-05-04T00:34:49:           at Server.insert (C:\Articles\node_modules\mongodb-core\lib\topologies\server.js:536:16)
2021-05-04T00:34:49:           at Server.insert (C:\Articles\node_modules\mongodb\lib\server.js:385:17)
2021-05-04T00:34:49:           at executeCommands (C:\Articles\node_modules\mongodb\lib\bulk\ordered.js:455:23)
2021-05-04T00:34:49:           at resultHandler (C:\Articles\node_modules\mongodb\lib\bulk\ordered.js:425:5)
2021-05-04T00:34:49:           at C:\Articles\node_modules\mongodb-core\lib\connection\pool.js:461:18
2021-05-04T00:34:49:           at process._tickCallback (internal/process/next_tick.js:61:11)

我想实现这种日志格式意味着pm2中每个错误和日志级别都有一个时间戳

2021-05-04T00:34:49: [Error]       { MongoError: server instance pool was destroyed
          at Function.MongoError.create (C:\Article\snode_modules\mongodb-core\lib\error.js:29:11)
          at basicWriteValidations (C:\Articles\node_modules\mongodb-core\lib\topologies\server.js:448:51)
          at Server.insert (C:\Articles\node_modules\mongodb-core\lib\topologies\server.js:536:16)
          at Server.insert (C:\Articles\node_modules\mongodb\lib\server.js:385:17)
          at executeCommands (C:\Articles\node_modules\mongodb\lib\bulk\ordered.js:455:23)
          at resultHandler (C:\Articles\node_modules\mongodb\lib\bulk\ordered.js:425:5)
          at C:\Articles\node_modules\mongodb-core\lib\connection\pool.js:461:18
          at process._tickCallback (internal/process/next_tick.js:61:11)

这是我的配置文件:-

module.exports = {
  apps : [{
    name   : "app",
    script : "./app.js",
    error_file : "/err.log",
    out_file : "/out.log",
    time: true

  }]
}

提前致谢!!

node.js sails.js pm2 pm2-logrotate
1个回答
0
投票

此解决方案并不完全按照您的要求处理 PM2 日志;认为这是一种替代方法。

您可以使用 Pino Pretty 模块自定义日志文件输出。

开始之前,请安装
pino-pretty

npm i -g pino-pretty

您还可以使用
tail
命令来实现此目的

tail -f out.log | pino-pretty 
// cat ~/.pm2/logs/<name>.log | pino-pretty 

使用
pino-pretty

运行脚本
{
    "interpreter": "none",
    "scripts": "node ./app.js | pino-pretty",
    // node ./app.js | node_modules/.bin/pino-pretty
}
© www.soinside.com 2019 - 2024. All rights reserved.