11

How do I use pm2 to log only based off a keyword?

This is how pm2 is being called currently:

package.json file

"services": "pm2 start pm2.config.js --no-daemon",
Rod
  • 14,529
  • 31
  • 118
  • 230

3 Answers3

9

I don't know if by default pm2 saves logs in a file, but, when using pm2 log rotate it does. So it works for me:

grep -r "KEYWORD"  ~/.pm2/logs

I'm using EC2 Ubuntu and that is the default pm2 logs folder, you may change it according to your setup.

Update: It will not filter the log in real time, but it's useful if you need to find anything on older logs. If you need to filter logs in real time, you can grep the outpu with Hesam B answer, I just tested it, and it works, as shown below.

enter image description here

Alcides Bezerra
  • 417
  • 4
  • 11
4
$ pm2 logs app-name or pid | grep -i Keyword
Kunal Ranjan
  • 192
  • 1
  • 2
  • 13
1

You can grep the log output if that helps

$ pm2 logs | grep -i "KEYWORD"
Hesam B.
  • 45
  • 3
  • 1
    Hi, grepping the output won't help unfortunately because `pm2 logs` never exits (and also display by default only the last 20 lines)... – TOPKAT Feb 08 '21 at 16:46
  • 3
    I tried this and it works. You can change the number of lines it displays by using the --lines argument. Something like: pm2 logs --lines 1000 | grep -i "keyword" – Mr Stanev Nov 03 '21 at 11:49