TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published


build npm node license

A tool to solve log4js log synchronization in PM2's multiprocess mode.

English | 简体中文

Table of Contents


Have you encountered a failure when you were executing pm2 install pm2-intercom? If you want to use log4js for stable log output in PM2's multiprocess mode, then using pm2-intercom-log4js for multiprocess log synchronization will be a good choice. It imitates the cooperation method of pm2-intercom + log4js, and aggregates the logs of the sub-processes to the main process for unified output, so as to avoid unpredictable exceptions in file reading or writing in multiprocess mode.

Get Started

pm2-intercom-log4js only exports one function, which is very convenient to use.


This project uses node and npm. Make sure that you have installed them locally.

npm install @camus-design/pm2-intercom-log4js


When running the program in PM2 multiprocess mode, pm2-intercom-log4js will automatically find and use PM2 to handle the communication between the processes. This process is asynchronous, so you need to wait for this process to finish before initializing log4js.

const pm2Intercom = require('@camus-design/pm2-intercom-log4js');

pm2Intercom().catch((err) => {
  // If the initialization fails, you can check the reason according to the error log. If it still cannot be solved, you can submit an issue.
}).finally(() => {
  // Be sure to wait for the process communication to be initialized before initializing log4js.
    // Make sure logs4js has PM2 mode enabled.
    pm2: true,


The pm2Intercom function above supports passing in an optional parameters object (like pm2Intercom({ nodeModulesPath: '/root/xxx' })), the parameters in the object are as follows:

Attributes Descriptions Type Default
nodeModulesPath Manually specify the absolute path to the node_modules folder where PM2 is located (if auto finding PM2 fails) String -


We use PM2 to run the script in the examples directory and start two processes.

pm2 start ./examples/index.js -i 2

When we don't use the pm2Intercom function (ie, comment out the await pm2Intercom(); in the script), we see only one process successfully printing the log through the pm2 logs command.

xxx\.pm2\logs\index-error-0.log last 15 lines:
xxx\.pm2\logs\index-out-1.log last 15 lines:
xxx\.pm2\logs\index-error-1.log last 15 lines:
xxx\.pm2\logs\index-out-0.log last 15 lines:
0|index    | [2022-03-11T14:50:21.335] [INFO] default - Log from process 0

When we use the pm2Intercom function, we see that both processes successfully print logs, and both output to the log file of the main process.

xxx\.pm2\logs\index-error-0.log last 15 lines:
xxx\.pm2\logs\index-error-1.log last 15 lines:
xxx\.pm2\logs\index-out-1.log last 15 lines:
1|index    | Process 1 is waiting main process ready...
1|index    | Process 1 is ready to log.

xxx\.pm2\logs\index-out-0.log last 15 lines:
0|index    | Start pm2 intercom for log4js...
0|index    | [2022-03-11T14:53:14.804] [INFO] default - Log from process 0
0|index    | Intercom's main process started. (0: index)
0|index    | [2022-03-11T14:53:14.883] [INFO] default - Log from process 1


You are very welcome to join us! Submit an issue or submit a pull request.


MIT © Camus Design

Package Sidebar


npm i @camus-design/pm2-intercom-log4js

Weekly Downloads






Unpacked Size

23 kB

Total Files


Last publish


  • nepaul