@gentleknt/pm2-intercom-log4js v1.0.0-alpha.0
pm2-intercom-log4js
A tool to solve log4js log synchronization in PM2's multi-process mode.
English | 简体中文
Table of Contents
Background
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 multi-process mode, then using pm2-intercom-log4js for multi-process 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 multi-process mode.
Get Started
pm2-intercom-log4js only exports one function, which is very convenient to use.
Install
This project uses node and npm. Make sure you have them installed locally.
$ npm install @gentleknt/pm2-intercom-log4js
Usage
When we run the program through PM2 multi-process mode, pm2-intercom-log4js will automatically find and use PM2 to handle inter-process communication. This process is asynchronous, so we need to wait for the end of this process before initializing log4js.
const pm2Intercom = require('@gentleknt/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.
log4js.getLogger();
// todo
});
API
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 | - |
Example
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
Contributing
You are very welcome to join us! Submit an issue or submit a pull request.
License
MIT © Billion Bottle
2 years ago