Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom logging under pm2

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.

like image 772
user542319 Avatar asked Sep 15 '15 15:09

user542319


People also ask

Where are PM2 logs kept?

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

Where is PM2 config file?

The default configuration file is ecosystem. core3. config. js , and is located in the root folder of lisk-service : It contains a configuration to connect to a local Lisk Core node.

What is PM2 log rotate?

The module pm2-logrotate automatically rotate and keep all the logs file using a limited space on disk.


1 Answers

Update 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 and out_file, and start pm2 from that:

  1. Generate a configuration file: pm2 ecosystem simple. This would generate a file ecosystem.config.js, with following content:

    module.exports = {
      apps : [{
        name   : "app1",
        script : "./app.js"
      }]
    }
    
  2. Define error_file (for error log) and out_file (for info log) in the file, such as:

    module.exports = {
      apps : [{
        name   : "app1",
        script : "./app.js",
        error_file : "./err.log",
        out_file : "./out.log"
      }]
    }
    
  3. Start the process from the configuration file:

    pm2 start ecosystem.config.js
    

In this way, the logs are saved to ./err.log and ./out.log.

Please refer to the document for detail information.

like image 65
shaochuancs Avatar answered Sep 18 '22 22:09

shaochuancs