How to add dates to pm2 error logs?
node.jsPm2node.js Problem Overview
Is there a way to add timestamps to error logs in .pm2/logs?
I noticed that pm2 logs
command shows aggregated logs with timestamps, but looking into log files - there are only messages and stacktraces without dates.
node.js Solutions
Solution 1 - node.js
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.
Solution 2 - node.js
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/
Solution 3 - node.js
pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm"
Solution 4 - node.js
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
Solution 5 - node.js
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>
Solution 6 - node.js
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
Solution 7 - node.js
- first update the format (make sure server timezone is what you want)
pm2 restart 0 --log-date-format "DD-MM-YYYY HH:MM Z"
- save all processes
pm2 save
- 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
Solution 8 - node.js
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.