I have some useful logging in my node app that I write to console.log
node server.js >> /var/log/nodeserver.log 2>&1
However, when trying the same under pm2:
pm2 start server.js >> /var/log/pm2server.log 2>&1
the log file only shows the pm2 startup information
Is application logging at all possible with pm2? On their page they discuss logging, and show an image with text like "log message from echo.js"
, but I see nothing about getting custom information into the pm2 log.
In case of new start, you just:
pm2 start/reload ecosystem.config.js [--only your_app]
But when it is already started (pm2 already managing it) you have to do (someone can find a better way, but this works for me):
pm2 delete your_app
pm2 start
When running with pm2 your application logs will reside in
$HOME/.pm2/logs
as described here. Verifying this locally with a simpleindex.js
file that outputsconsole.log('test')
Notice how I see no
console.log
output here, but, if I navigate to$HOME/.pm2/logs
I seeUpdate in 2017.
Define log path as parameter when pm2 command is executed (
-l
,-o
,-e
) is very easy to use and normally is the best choice.However, if you don't want to define log path every time when pm2 is executed, you can generate a configuration file, define
error_file
andout_file
, and start pm2 from that:Generate a configuration file:
pm2 ecosystem simple
. This would generate a fileecosystem.config.js
, with following content:Define
error_file
(for error log) andout_file
(for info log) in the file, such as:Start the process from the configuration file:
In this way, the logs are saved to
./err.log
and./out.log
.Please refer to the document for detail information.
One nifty feature is to use the
logs
feature in terminal:this will live stream the all the logs. Other handy commands are:
pm2 flush
pm2 reloadLogs