npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@camus-design/pm2-intercom-log4js

v1.0.3

Published

A tool to solve log4js log synchronization in pm2 multithreading.

Downloads

8

Readme

pm2-intercom-log4js

build npm node license

A tool to solve log4js log synchronization in PM2's multiprocess 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 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.

Install

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

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

Usage

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.
  log4js.getLogger();
  log4js.configure({
    // Make sure logs4js has PM2 mode enabled.
    pm2: true,
  });
});

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 © Camus Design