119

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.

enter image description here

Plastic Rabbit
  • 2,859
  • 4
  • 25
  • 27

9 Answers9

189

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.

Cyral
  • 13,999
  • 6
  • 50
  • 90
AbdelHady
  • 9,334
  • 8
  • 56
  • 83
  • 1
    it appears that if you set `--watch` to look for changes you may lose the log-date-format on app reload, getting you back to logging without date: `pm2 start app.js --log-date-format 'DD-MM HH:mm:ss.SSS' --watch`. I just noticed that on my development machine. **LE** corrected: only the `pm2 logs` command will skip showing date/time on app refresh. – bosch Jan 22 '16 at 11:42
  • 4
    Hello, this dose not work for me.I'm using pm2 to manage python script,could you tell me what's the problem? TXU. – Lordran Oct 06 '16 at 01:11
  • 4
    Is there some way to only timestamp the *error* log and not the standard console log? I am already time stamping everything that goes to console.log. – Michael Apr 04 '19 at 18:23
48

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/

Ahsan
  • 3,845
  • 2
  • 36
  • 36
16

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 
Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
15
pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm"
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Randost
  • 151
  • 1
  • 2
8

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>
Fraction
  • 11,668
  • 5
  • 28
  • 48
  • 1
    +1 for reload option, the no downtime was great, I manage about 22 processes right now and was able to add timestamps to all logs via `pm2 reload all --time` – Trey Oct 15 '20 at 20:36
6

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
Michael
  • 1,764
  • 2
  • 20
  • 36
  • The command pm2 logs prefix your logs message, meaning that piping the content to bunyan doesn't work as expected. There is a closed request for pm2 to implement a raw log format to support the above. – Gerard Downes Apr 16 '15 at 16:15
  • 2
    PM2 has added a --raw flag for the pm2 logs commands enabling the above by using pm2 logs --raw | bunyan. This will be available in the next release. – Gerard Downes Apr 21 '15 at 21:46
5
  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

Amar Singh
  • 435
  • 5
  • 9
3

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.

mnhmilu
  • 2,327
  • 1
  • 30
  • 50
0

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

user2677034
  • 624
  • 10
  • 20