Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

360
Views
Does log4js require any extra code to work on an Apache server?

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!