Is there a way to only show log based on a keyword in pm2?
Asked Answered
R

3

13

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",
Roemer answered 2/11, 2018 at 19:58 Comment(0)
A
11

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

Alternative answered 6/4, 2021 at 14:21 Comment(0)
I
4
$ pm2 logs app-name or pid | grep -i Keyword
Izzy answered 15/12, 2021 at 11:37 Comment(3)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From ReviewMetzler
The answer is correct, if u have a correct answer , can answer hereIzzy
This answer may be correct, but you need to provide an explanation as well.Metzler
B
1

You can grep the log output if that helps

$ pm2 logs | grep -i "KEYWORD"
Boyles answered 18/7, 2019 at 19:36 Comment(2)
Hi, grepping the output won't help unfortunately because pm2 logs never exits (and also display by default only the last 20 lines)...Hutchison
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"Rameriz

© 2022 - 2024 — McMap. All rights reserved.