PM2 logs in docker
What's going wrong?
My application logs are going to the PM2 logs (/root/.pm2/pm2.log) instead of standard output.
How could we reproduce this issue?
Run PM2 in docker, using the pm2-runtime binary with the following process file:
/ # cat /opt/pm2.json
{
"apps": [{
"name": "ux",
"script": "npm",
"args": "start",
"cwd": "/opt/ux",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"pid_file": "/tmp/ux_pm2.pid",
"log_type": "raw",
"log_file": "/dev/null",
"out_file": "/dev/null",
"error_file": "/dev/null"
"merge_logs": true,
"instances": 1,
"exec_mode": "cluster",
"max_restarts": 10,
"restart_delay": 1000
}]
}
Supporting information
I've tried different combinations for the log configuration and nothing works, If I try for example, to set the error_file and out_file to /dev/stdout or /dev/fd/1 I get an error from event.js because it's trying to seek in the log file and you can't do that with stdout or stderr, I've even tried omitting any log related configuration from my process file and let PM2 use the defaults and I still got the same result.
I'm using keymetrics/pm2:8-alpine as my base image to run my application and the application uses the bunyan lib to write the logs.
Is there a way to completely disable log handling on PM2 and just let it pass everything to stdout?
As you can see in the output below my application logs are in /root/.pm2/pm2.log and I get nothing in stdout.
Please run the following command (available on PM2 >= 2.6)
$ pm2 report
===============================================================================
--- PM2 REPORT (Thu Feb 01 2018 13:35:35 GMT+0000 (UTC)) ----------------------
===============================================================================
--- Daemon -------------------------------------------------
pm2d version : 2.9.3
node version : 8.9.4
node path : undefined
argv : /usr/local/bin/node,/usr/local/lib/node_modules/pm2/lib/Daemon.js
argv0 : node
user : undefined
uid : 0
gid : 0
uptime : 7min
===============================================================================
--- CLI ----------------------------------------------------
local pm2 : 2.9.3
node version : 8.9.4
node path : undefined
argv : /usr/local/bin/node,/usr/local/bin/pm2,report
argv0 : node
user : undefined
uid : 0
gid : 0
===============================================================================
--- System info --------------------------------------------
arch : x64
platform : linux
type : Linux
cpus : Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
cpus nb : 4
freemem : 516497408
totalmem : 2095816704
home : /root
===============================================================================
--- PM2 list -----------------------------------------------
┌──────────┬────┬─────────┬─────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼─────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ ux │ 0 │ cluster │ 134 │ online │ 1 │ 2m │ 0% │ 38.9 MB │ root │ disabled │
└──────────┴────┴─────────┴─────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
/root/.pm2/pm2.log last 20 lines:
PM2 | {"name":"API","hostname":"6bad7132cad2","pid":145,"level":50,"err":{"message":"listen EADDRINUSE :::8003","name":"Error","stack":"Error: listen EADDRINUSE :::8003\n at Object._errnoException (util.js:1022:11)\n at _exceptionWithHostPort (util.js:1044:20)\n at Server.setupListenHandle [as _listen2] (net.js:1351:14)\n at listenInCluster (net.js:1392:12)\n at Server.listen (net.js:1476:7)\n at setServer (/opt/ux/helpers/server.js:94:21)\n at nextTask (/opt/ux/node_modules/async/dist/async.js:5297:14)\n at next (/opt/ux/node_modules/async/dist/async.js:5304:9)\n at /opt/ux/node_modules/async/dist/async.js:906:16\n at /opt/ux/helpers/server.js:61:13\n at nextTask (/opt/ux/node_modules/async/dist/async.js:5297:14)\n at Object.waterfall (/opt/ux/node_modules/async/dist/async.js:5307:5)\n at exports.instance (/opt/ux/helpers/server.js:59:11)\n at /opt/ux/helpers/server.js:357:9\n at new Promise (<anonymous>)\n at Object.exports.start (/opt/ux/helpers/server.js:356:25)\n at Object.<anonymous> (/opt/ux/index.js:2:8)\n at Module._compile (module.js:643:30)\n at Object.Module._extensions..js (module.js:654:10)\n at Module.load (module.js:556:32)\n at tryModuleLoad (module.js:499:12)\n at Function.Module._load (module.js:491:3)","code":"EADDRINUSE"},"msg":"listen EADDRINUSE :::8003","time":"2018-02-01T13:33:25.279Z","v":0}
PM2 | Initiated mongo environment: app
PM2 | Initiating log mongo: mongodb://DB_USER:DB_PASSWD@DB_SERVER:DB_PORT/logs
PM2 | Initiated mongo environment: log
PM2 | Oauth service configured.
PM2 | Plan constants got set.
PM2 | PubNub got set.
PM2 | Authy got set.
PM2 | Google got set.
PM2 | Setting exit processes.
PM2 | Exit processes got set.
PM2 | {"name":"API","hostname":"6bad7132cad2","pid":145,"level":30,"msg":"Server started.","time":"2018-02-01T13:33:25.828Z","v":0}
PM2 | (node:145) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Issue with 'Authy' configuration.
PM2 | (node:145) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
PM2 | (node:145) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Issue with 'Authy' configuration.
PM2 | POST /enterprise/v1/fetchconfig 200 4.402 ms - 503
PM2 | GET /customCss 200 3.414 ms - 2
PM2 | {"name":"API","hostname":"6bad7132cad2","pid":77,"level":50,"error":"UNAUTHENTICATED_USER","req":{"url":"/currentUser","params":{},"body":{"authtoken":"fl81526c54d970294bca35c4"},"query":{},"method":"GET","authtoken":"fl81526c54d970294bca35c4"},"step":"01","function":"handleOldSessions","authtoken":"fl81526c54d970294bca35c4","msg":"","time":"2018-02-01T13:33:31.880Z","v":0}
PM2 | {"name":"API","hostname":"6bad7132cad2","pid":77,"level":50,"error":{"message":"Login Required.","code":401},"msg":"","time":"2018-02-01T13:33:31.880Z","v":0}
PM2 | GET /currentUser 401 4.723 ms - 50
Could you try without these lines ?
"log_file": "/dev/null",
"out_file": "/dev/null",
"error_file": "/dev/null"
"merge_logs": true,
I've already did, I mentioned that in the Supporting information section.
Update: This is not a problem if I use exec_mode": "fork" instead of cluster so this could be related to #3384
BTW, I see that logs are prefixed with <id>|<name> | <time_stamp>: , in this case, all my logs are prefixed with 0|ux | 2018-02-01 15:50 +00:00: my application is logging in json but with this my logs are no longer valid json, if I use "log_type": "json" my log messages become valid json but the json logs being output by my app are escaped and this also breaks my parsers in logstash. Is there a way to disable this behavior? I would be happy if PM2 would just leave my logs alone and just let them be displayed to stdout.
I don't get the issue.
Could you do:
# Install the pm2 from development branch
RUN npm install Unitech/pm2#development -g
[...]
RUN ["pm2-runtime", "process.config.js", "--json"]
No activity. Feel free to reopen if you need.
@luisdavim I've got the same problem : try to generate a Json one line without pm2 prefix. Have you found a solution ? Thx
@luisdavim @JBustin Hi guys, I've got the same issue. have you guys found a solution for this ? thank you!
I currently have this issue where Pm2-runtime transforms my JSON logs into unescaped JSON, thus breaking my Logstash parsers and prevents elastic search from indexing the fields.
same issue. why closed
I solve the pm2-prefix problem by running with pm2-runtime --raw processes.json in container mode. See this line in pm2-runtime option. Hope this could save the times for those who suffering this issue as me.
Anyone figured out how to get this to work? I think it only happens to some people when running in cluster for the exec_mode which is maybe why its hard to reproduce.
Hi Same here, running in cluster-mode in a container, I have no logs at all. pm2 logs shows me logs in the /root dir but they are empty and nothing in the docker output...