From ae97ae6c9b9d549e30077541026b3d8464682274 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 26 Nov 2017 09:00:57 +1100 Subject: [PATCH 1/5] Updated codecov badge and link --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 06d353b..19797c0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# log4js-node [![Build Status](https://secure.travis-ci.org/log4js-node/log4js-node.png?branch=master)](http://travis-ci.org/log4js-node/log4js-node) +# log4js-node [![Build Status](https://secure.travis-ci.org/log4js-node/log4js-node.png?branch=master)](http://travis-ci.org/log4js-node/log4js-node) [![codecov](https://codecov.io/gh/log4js-node/log4js-node/branch/master/graph/badge.svg)](https://codecov.io/gh/log4js-node/log4js-node) + [![NPM](https://nodei.co/npm/log4js.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/log4js/) -[![codecov](https://codecov.io/gh/nomiddlename/log4js-node/branch/master/graph/badge.svg)](https://codecov.io/gh/nomiddlename/log4js-node) This is a conversion of the [log4js](https://github.com/stritti/log4js) framework to work with [node](http://nodejs.org). I started out just stripping out the browser-specific code and tidying up some of the javascript to work better in node. It grew from there. Although it's got a similar name to the Java library [log4j](https://logging.apache.org/log4j/2.x/), thinking that it will behave the same way will only bring you sorrow and confusion. From 835189696d6da34605df7a8d69c862df0b4d5f44 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 12 Dec 2017 08:17:53 +1100 Subject: [PATCH 2/5] fix(cluster): added circular json dep --- lib/appenders/multiprocess.js | 7 ++++--- lib/log4js.js | 7 ++++--- package.json | 1 + test/tap/cluster-test.js | 6 +++++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/appenders/multiprocess.js b/lib/appenders/multiprocess.js index 09e75a4..3c90ec1 100644 --- a/lib/appenders/multiprocess.js +++ b/lib/appenders/multiprocess.js @@ -2,6 +2,7 @@ const debug = require('debug')('log4js:multiprocess'); const net = require('net'); +const CircularJSON = require('circular-json'); const END_MSG = '__LOG4JS__'; @@ -19,7 +20,7 @@ function logServer(config, actualAppender, levels) { debug('deserialising log event'); let loggingEvent; try { - loggingEvent = JSON.parse(msg); + loggingEvent = CircularJSON.parse(msg); loggingEvent.startTime = new Date(loggingEvent.startTime); loggingEvent.level = levels.getLevel(loggingEvent.level.levelStr); } catch (e) { @@ -98,13 +99,13 @@ function workerAppender(config) { // The following allows us to serialize errors correctly. // Validate that we really are in this case const logData = loggingEvent.data.map((e) => { - if (e && e.stack && JSON.stringify(e) === '{}') { + if (e && e.stack && CircularJSON.stringify(e) === '{}') { e = { stack: e.stack }; } return e; }); loggingEvent.data = logData; - socket.write(JSON.stringify(loggingEvent), 'utf8'); + socket.write(CircularJSON.stringify(loggingEvent), 'utf8'); socket.write(END_MSG, 'utf8'); } diff --git a/lib/log4js.js b/lib/log4js.js index 9799c2b..c639c4b 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -24,6 +24,7 @@ */ const debug = require('debug')('log4js:main'); const fs = require('fs'); +const CircularJSON = require('circular-json'); const Configuration = require('./configuration'); const connectModule = require('./connect-logger'); const logger = require('./logger'); @@ -92,13 +93,13 @@ function serialise(logEvent) { // Validate that we really are in this case try { const logData = logEvent.data.map((e) => { - if (e && e.stack && JSON.stringify(e) === '{}') { + if (e && e.stack && CircularJSON.stringify(e) === '{}') { e = { message: e.message, stack: e.stack }; } return e; }); logEvent.data = logData; - return JSON.stringify(logEvent); + return CircularJSON.stringify(logEvent); } catch (e) { return serialise(new LoggingEvent( 'log4js', @@ -111,7 +112,7 @@ function serialise(logEvent) { function deserialise(serialised) { let event; try { - event = JSON.parse(serialised); + event = CircularJSON.parse(serialised); event.startTime = new Date(event.startTime); event.level = config.levels.getLevel(event.level.levelStr); event.data = event.data.map((e) => { diff --git a/package.json b/package.json index 726b93d..2a15aa2 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "lib": "lib" }, "dependencies": { + "circular-json": "^0.4.0", "date-format": "^1.2.0", "debug": "^3.1.0", "semver": "^5.3.0", diff --git a/test/tap/cluster-test.js b/test/tap/cluster-test.js index f3e1dc6..e3f0634 100644 --- a/test/tap/cluster-test.js +++ b/test/tap/cluster-test.js @@ -42,6 +42,8 @@ if (cluster.isMaster) { t.equal(logEvents[1].pid, workerPid); t.type(logEvents[1].data[1], 'Error'); t.contains(logEvents[1].data[1].stack, 'Error: oh dear'); + t.type(logEvents[1].data[2], 'object'); + t.type(logEvents[1].data[2].me, 'object'); t.equal(logEvents[2].categoryName, 'log4js'); t.equal(logEvents[2].level.toString(), 'ERROR'); t.equal(logEvents[2].data[0], 'Unable to parse log:'); @@ -61,7 +63,9 @@ if (cluster.isMaster) { }); } else { const workerLogger = log4js.getLogger('worker'); - workerLogger.info('this is worker', new Error('oh dear')); + const circle = {}; + circle.me = circle; + workerLogger.info('this is worker', new Error('oh dear'), circle); // can't run the test in the worker, things get weird process.send({ type: '::testing', From f2f9790becae9ad5f16265620d6ec5d4d81a543f Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 12 Dec 2017 08:32:35 +1100 Subject: [PATCH 3/5] fix(cluster): potential bug when cluster not available --- lib/log4js.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/log4js.js b/lib/log4js.js index c639c4b..c0b72d8 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -207,7 +207,9 @@ function configure(configurationFileOrObject) { // just in case configure is called after shutdown process.removeListener('message', receiver); - cluster.removeListener('message', receiver); + if (cluster) { + cluster.removeListener('message', receiver); + } if (config.disableClustering) { debug('Not listening for cluster messages, because clustering disabled.'); } else if (isPM2Master()) { From 2d30d3dc85f3238ecfa5b713e1ddf0c26defdb6a Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 12 Dec 2017 08:32:56 +1100 Subject: [PATCH 4/5] 2.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2a15aa2..7de71cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "log4js", - "version": "2.3.12", + "version": "2.4.0", "description": "Port of Log4js to work with node.", "homepage": "https://log4js-node.github.io/log4js-node/", "keywords": [ From 10dac6d6fd3612da730f07bd2bcf7b0f28ba3541 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 12 Dec 2017 08:34:41 +1100 Subject: [PATCH 5/5] 2.4.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7de71cc..9f49f53 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "log4js", - "version": "2.4.0", + "version": "2.4.1", "description": "Port of Log4js to work with node.", "homepage": "https://log4js-node.github.io/log4js-node/", "keywords": [