如何向 pm2错误日志添加日期?

有没有一种方法可以将时间戳添加到. pm2/log 中的错误日志中?

我注意到 pm2 logs命令显示了带有时间戳的聚合日志,但是查看日志文件——只有没有日期的消息和堆栈跟踪。

enter image description here

72232 次浏览

I use PM2, but I don't care for the logs that much. Instead I use bunyan, which gives a ton of flexibility for logging. If you npm install it with --global you can also use it as a live log viewer:

This won't timestamp your console.log output, though. But If you convert to log.info() or any other Bunyan log function you will get nice logging.

To view live pm2 logs with bunyan, just pipe it:

pm2 logs | bunyan
pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm"

As per the pm2 logs official documentation, you can use --time, which prefixes logs with a standard formatted timestamp.

pm2 start app.js --time

If you have already created the app, you can update it while restarting the application with:

pm2 restart 0 --time

Make sure to pm2 save afterwards.

Note that you can also use a custom formatter as per this issue & this commit:

pm2 start app.js --log-date-format 'DD-MM HH:mm:ss.SSS'

where 'DD-MM HH:mm:ss.SSS' is any momentjs valid format.

As per the command line help (pm2 logs -h) running pm2 logs --timestamp command should add the timestamp to the logs. However it does seem to not affect old logs! Apparently only new logs show up with timestamp.

To fix this issue pass --log-date-format="YYYY-MM-DD HH:mm Z" to pm2 as a param. For example:

pm2 start bin/www --log-date-format="YYYY-MM-DD HH:mm Z"

Using process.json

I like process.json for starting my app for convenience so my process.json contains the following:

{
"apps" : [
{
"name"        : "app",
"script"      : "bin/www",
"log_date_format" : "YYYY-MM-DD HH:mm Z"
}
]
}

then I start my app by just running:

pm2 start process.json

Once done I see the timestamp showing up just by running:pm2 logs Notice that I didn't have to specify --timestamp to see the timestamp.

app (out): 2016-08-04 13:46 +01:00: My log here

A good read: http://pm2.keymetrics.io/docs/usage/log-management/

To use standard formated timestamp:

pm2 start app.js --time

Or if you want to prefix logs with custom formated timestamp:

pm2 start app.js --log-date-format <format>

Where <format> is a moment display format (eg YYYY-MM-DD HH:mm Z).

And if your app is already running you can use reload for a 0-second-downtime reload:

pm2 reload app.js --time

Or

pm2 reload app.js --log-date-format <format>

For process.yml , follow these example format. It Worked for me

 apps:
- script   : ./SampleApi/app.js
name     : 'api-proxy-app'
instances: 2
exec_mode: cluster
watch  : true
log_date_format : "YYYY-MM-DD HH:mm Z"

Sample Log format with DateTime:

2019-07-28 13:46 +06:00: channel created for cancel mandate--####################################
2019-07-28 13:46 +06:00: channel created for cancel mandate--####################################
2019-07-28 13:46 +06:00: channel created for exception scenario--####################################
2019-07-28 13:46 +06:00: channel created for create mandate--####################################
2019-07-28 13:46 +06:00: create channel initiated for cancel mandate--------------------->
2019-07-28 13:46 +06:00: create channel initiated for create mandate--------------------->
2019-07-28 13:46 +06:00: create channel initiated for update mandate--------------------->

Using --log-date-format didn't worked for me.

Wasted 30 mins on this.


  • Din't work
    • Other answers din't work
    • Official CLI din't work too: pm2 start app.js [OPTIONS], ex: pm2 start app.js --time
  • Worked

  • Create an Ecosystem file pm2-config.js in your application root (ex: beside package.json)

Paste the below contents & save:

module.exports = {
apps: [
{
name: "my-app1",
script: "./index.js",
time: true,  // <----------------------- This is the key to make it work
watch: false,
env: {
PORT: 4001,
NODE_ENV: "production",
},
},
],
};
  • Now create a shell script start.sh (OR, batch file, OR, directly run below commands)

Paste the below contents & save:

pm2 stop pm2-config.js
pm2 delete pm2-config.js
pm2 start pm2-config.js
  1. first update the format (make sure server timezone is what you want)
    pm2 restart 0 --log-date-format "DD-MM-YYYY HH:MM Z"
    
  2. save all processes
    pm2 save
    
  3. run these
    npm i -G pm2 //if not latest
    pm2 update
    

this works and show log in servers time zone other wise time zone will be different