How to add dates to pm2 error logs?
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/
pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm"
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
- Official Ecosystem file worked great (procedure is below)
- Create an Ecosystem file
pm2-config.js
in your application root (ex: besidepackage.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