Skip to content

Supporting log4js in PM2 cluster mode, this package is originally made to fix broken "pm2-intercom"

Notifications You must be signed in to change notification settings

liujingbreak/log4js-pm2-intercom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8a9f073 · Sep 24, 2019

History

7 Commits
Sep 24, 2019
Sep 24, 2019
Jul 3, 2018
Jul 3, 2018
Jul 3, 2018
Sep 23, 2019
Jul 3, 2018
Jul 3, 2018
Sep 24, 2019
Sep 24, 2019
Sep 24, 2019
Jul 3, 2018

Repository files navigation

To fix pm2-intercom in PM2 2.x

Refer to log4js API doc https://log4js-node.github.io/log4js-node/api.html, regarding pm2: true

Problems of pm2-intercom and log4js:

  • Duplicate log messages PM2 v2.10.4 somehow runs multiple instances of pm2-intercom for cluster mode.

    While you start with pm2 install pm2-intercom then start your application in cluster mode, it runs multiple instances which leads to duplicate log messages.

    I am guessing it is PM2 module system's fault, so I write this simple version which is not supposed to be run as pm2 install module anymore.

  • Not supporting multiple applications in single PM2 box If you have 2 applications with difference names, and both in cluster mode, all of your log will go to first cluster instance/process of your first application's log appender.

1. Start lj-log4js-pm2intercom

Run this package as a normal PM2 application at first place,

It must be the first application to be started in PM2 (fork mode)

npm i lj-log4js-pm2intercom
pm2 start node_modules/.bin/log4js-pm2 --name log4js-intercom

Or you can: Clone this repo, go to root directory and run command:

yarn install # or npm install
pm2 start encosystem.conf.js

2. Change your application source code

In your application source where you configure log4js, add following code:

const pm2InstanceId = process.env.NODE_APP_INSTANCE;
// Only log4js master process need to handle all log appenders
if (pm2InstanceId === '0') {
	// Tell lj-log4js-pm2intercom: I am the log4js master process
	// lj-log4js-pm2intercom will need to know my process id and application name
	process.send({topic: 'log4js:master'});
}

then you start your applications in pm2 cluster mode as normal:

pm2 start app.js -i 2 --name <your application name>

Be aware to always use --name to specify a name

Now run pm2 ls, you should see something like below

┌───────────────────┬────┬─────────┬───────┬────────┬─────────┬────────┬─────┬───────────┬─────────┬──────────┐
│ App name          │ id │ mode    │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user    │ watching │
├───────────────────┼────┼─────────┼───────┼────────┼─────────┼────────┼─────┼───────────┼─────────┼──────────┤
│ credit-appl-local │ 1  │ cluster │ 67022 │ online │ 0       │ 10m    │ 0%  │ 59.4 MB   │ liujing │ disabled │
│ credit-appl-local │ 2  │ cluster │ 67023 │ online │ 0       │ 10m    │ 0%  │ 62.2 MB   │ liujing │ disabled │
│ log4js-intercom   │ 0  │ fork    │ 67011 │ online │ 0       │ 10m    │ 0%  │ 29.6 MB   │ liujing │ disabled │
└───────────────────┴────┴─────────┴───────┴────────┴─────────┴────────┴─────┴───────────┴─────────┴──────────┘

About

Supporting log4js in PM2 cluster mode, this package is originally made to fix broken "pm2-intercom"

Resources

Stars

Watchers

Forks

Packages

No packages published