Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

node.js

pm2

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",
like image 260
Rod Avatar asked Nov 02 '18 19:11

Rod


People also ask

How do you find the log in pm2?

Application Logs Once an application is started with PM2 you can consult and manage logs easily. Log files are located in the folder $HOME/. pm2/logs .

Where are pm2 logs stored?

By default, all logs are saved into $HOME/. pm2/logs .

How do I stop pm2 logs?

You can pass /dev/null to error_file or out_file to disable logs saving.

What is pm2 runtime?

PM2 Runtime is a Production Process Manager for Node. js applications with a built-in Load Balancer. It allows you to keep applications alive forever, to reload them without downtime and facilitate common Devops tasks. Starting an application in production mode is as easy as: $ pm2 start app.js.


2 Answers

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

like image 190
Alcides Bezerra Avatar answered Nov 07 '22 11:11

Alcides Bezerra


$ pm2 logs app-name or pid | grep -i Keyword
like image 35
Kunal Ranjan Avatar answered Nov 07 '22 12:11

Kunal Ranjan