skip to Main Content

I’m trying to add Log4js-Node to a Node.js server running on Apache. Here’s my code:

const path = require("path");
const express = require("express");
const log4js = require('log4js');

const app = express();
const logger = log4js.getLogger();
logger.level = "debug";
const port = 443;

log4js.configure({
  appenders: { everything: { type: 'file', filename: 'logs.log', flags: 'w' } },
  categories: { default: { appenders: ['everything'], level: 'ALL' } }
});

const server = app.listen(port, () => {
    logger.debug("listening to requests on port " + port);
});

app.get("/log", (req, res) => {
  res.sendFile(path.join(__dirname + "/logs.log"));
});

When I run the script on Node.js on my computer and navigate to localhost:443/log I see what I expect, which is this:

[2020-03-17T22:50:43.145] [DEBUG] default – listening to requests on port 443

But when I run the code on a remote server it crashes and I get this in the error page (with part of the path replaced by me with "[removed]"):

App 25925 output: at Server. ([removed]/index.js:27:9)

App 25925 output: at Logger. [as debug] ([removed]/12/lib/node_modules/log4js/lib/logger.js:124:10)

App 25925 output: at Logger.log ([removed]/12/lib/node_modules/log4js/lib/logger.js:73:12)

App 25925 output: at Logger._log ([removed]/12/lib/node_modules/log4js/lib/logger.js:90:16)

App 25925 output: at Object.send ([removed]/12/lib/node_modules/log4js/lib/clustering.js:97:15)

App 25925 output: [removed]/12/lib/node_modules/log4js/lib/clustering.js:97

App 25925 output: at Object. ([removed]/12/lib/node_modules/log4js/lib/clustering.js:8:13)

I’m using A2 Hosting which uses Apache 2.4.41. I opted for Node.js 12.9.0, and Log4js 6.1.2. The package.json should be the same on both my computer and the server, and I’ve run npm install on both.

Is this just an issue with Log4js and the server, or have I missed something somewhere?

2

Answers


  1. Chosen as BEST ANSWER

    This was actually a relatively simple fix. The path referenced by the last error in the stack trace is a Log4js module that implements clustering support through Node's "cluster" module. The line "8" referenced is cluster = require("cluster"). It's wrapped in a try/catch block like this:

    try {
      cluster = require("cluster"); //eslint-disable-line
    } catch (e) {
      debug("cluster module not present");
      disabled = true;
    }
    

    The installation of Node.js on my computer came with the "cluster" module, however as far as I can tell, the server I'm using doesn't support it. Also, the version of Node I'm using on my computer is newer than what I'm using on the server (so I've now installed 12.9 on my machine). I believe the older version of Node doesn't bother trying to catch the exception and tries to load the cluster module, fails, and then throws the error.

    So the simple fix was to comment out most of the "try/catch" block, leaving just the contents of "catch" like this:

    // try {
    //   cluster = require("cluster"); //eslint-disable-line
    // } catch (e) {
      debug("cluster module not present");
      disabled = true;
    // }
    

    If someone has a better fix, I'm open to suggestions.


  2. The same response of @skittleswrapper,thx, it work for me.
    I use Node.js 14.18.1 with log4js 6.3.0.
    But i wondering what’is the necessary of this module ‘cluster’ and if we can
    add it to our app in other way.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search