Code Monkey home page Code Monkey logo

log4js-pm2-intercom's Introduction

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 │
└───────────────────┴────┴─────────┴───────┴────────┴─────────┴────────┴─────┴───────────┴─────────┴──────────┘

log4js-pm2-intercom's People

Contributors

liujingbreak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.