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.
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.
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"
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/
Wasted 30 mins on this.
pm2 start app.js [OPTIONS]
, ex: pm2 start app.js --time
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",
},
},
],
};
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
pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm"
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>
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 restart 0 --log-date-format "DD-MM-YYYY HH:MM Z"
pm2 save
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
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.
The date time format has to be in double quotes as below:
pm2 restart 0 --log-date-format="YYYY-MM-DD HH:mm Z"
Using single quotes I got an error: [PM2][ERROR] Process or Namespace HH:mm not found