Search code examples
node.jsloggingpm2

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.


Solution

  • 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