How to start pm2 process with DEBUG option
Asked Answered
N

2

5

I have an express app that I start in terminal with following command to enable debug logs in it:

DEBUG=custom:* npm start (on Ubuntu)
SET DEBUG=custom:* & npm start (on Windows)

On production server, I start app with PM2 using following command:

pm2 start bin/www -i 0

But this does not enable the debug logs in my code, so the debug statements are not added to the logs, only console.error() are added to the log files. How can I pass the DEBUG=custom:* option while starting my app with PM2?

Nerti answered 8/9, 2017 at 13:14 Comment(0)
E
3

Try DEBUG='custom:*' pm2 start bin/www -i 0

If you are restarting an existing process add the --update-env flag:

DEBUG='custom:*' pm2 restart bin/www -i 0 --update-env

Eileen answered 22/9, 2017 at 4:22 Comment(4)
I tried this and it seems to work. But the debug logs are going to www-error-0.log file not the www-out-0.log file. I was expecting these debug logs to go to output log file not error log file. Is that expected?Nerti
Are you using winston for logging? This is the default behavior for it. You can use stderrLevels to define which logs go to err and which out. More here: github.com/winstonjs/winston/blob/master/docs/…Eileen
I'm using default debug module. Anyway, your solution solved my problem :). Thanks a lot Mikko :)Nerti
Well, yes of course it's debug, I wasn't thinking.. :) Anyway it seems that the it too can also be configured similarly as described here: npmjs.com/package/debug#output-streamsEileen
S
3

Mikko was correct but if you add that to package.json scripts, it won't work!

"scripts": {
    "start": "DEBUG='custom:*' pm2 start bin/www -i 0",
    ...
  },

Because DEBUG='custom:*' is given to pm2 process NOT your process. So in this case you have to use an ecosystem file and add DEBUG setting in ecosystem file, e.g.

"scripts": {
    "start": "pm2 start ecosystem.config.js",
    ...
  },

//in ecosystem.config.js add this
env: {
    NODE_ENV: 'development',
    DEBUG: 'custom:*'
},
Stratosphere answered 11/11, 2019 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.