custom logging under pm2
Asked Answered
D

4

10

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.

Department answered 15/9, 2015 at 15:43 Comment(0)
K
10

When running with pm2 your application logs will reside in $HOME/.pm2/logs as described here. Verifying this locally with a simple index.js file that outputs console.log('test')

$ pm2 start index.js
[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized
[PM2] Starting index.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬────────────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ memory     │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼────────────┼──────────┤
│ index    │ 0  │ fork │ 36976 │ online │ 0       │ 0s     │ 9.258 MB   │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

Notice how I see no console.log output here, but, if I navigate to $HOME/.pm2/logs I see

logs $ ls
index-error-0.log   index-out-0.log

logs $ cat index-out-0.log
test
Kaylor answered 16/9, 2015 at 9:36 Comment(3)
Can we change log path?Ogletree
You sure can, have a look at the list of options here, particularly -l, -o, and -eDystopia
I don't want to write out info logs to file. I only want to keep logging on console in production, no file should be created , how to do that with pm2 ?Blowtorch
P
5

One nifty feature is to use the logs feature in terminal:

pm2 logs [--raw]

this will live stream the all the logs. Other handy commands are:

  • pm2 flush
  • pm2 reloadLogs
Phan answered 27/6, 2016 at 10:2 Comment(0)
D
3

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.

Datcha answered 30/8, 2017 at 8:10 Comment(0)
G
0

In case of new start, you just:

  • run 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):

  • run pm2 delete your_app
  • run pm2 start
Greyhound answered 6/3, 2019 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.