From 25aed9b6433e75d142be3a07dcb5f8178c59f9cc Mon Sep 17 00:00:00 2001
From: Kochan Piotr
Date: Wed, 31 Jan 2018 13:50:49 +0100
Subject: [PATCH 1/2] data field different format
---
.gitignore | 1 +
lib/index.js | 11 +++++++++--
test/test.log4js.js | 33 +++++++++++++++++++++++++++++++++
3 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/.gitignore b/.gitignore
index 2ccbe46..7aa7fb2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/node_modules/
+.idea
\ No newline at end of file
diff --git a/lib/index.js b/lib/index.js
index c50d581..70df75a 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -32,8 +32,10 @@ function fluentAppender(config, layout) {
const logSender = FluentSender.createFluentSender(tag_prefix, options);
const appender = function(loggingEvent) {
- const data = util.format.apply(null, loggingEvent.data);
- const rec = {
+ const data = loggingEvent.data[0];
+ const extra = loggingEvent.data.splice(1);
+
+ let rec = {
timestamp: loggingEvent.startTime.getTime(),
category: loggingEvent.categoryName,
levelInt: loggingEvent.level.level,
@@ -41,6 +43,11 @@ function fluentAppender(config, layout) {
context: loggingEvent.context,
data: data
};
+
+ if (extra.length) {
+ rec['extra'] = Object.assign(...extra);
+ }
+
if (options.levelTag !== false) {
logSender.emit(loggingEvent.level.levelStr, rec);
} else {
diff --git a/test/test.log4js.js b/test/test.log4js.js
index 821a4f6..1886ee6 100644
--- a/test/test.log4js.js
+++ b/test/test.log4js.js
@@ -118,4 +118,37 @@ describe('log4js-fluent-appender', () => {
done();
});
+ it('should log extra', (done) => {
+ const tag_prefix = 'tag_prefix';
+ const options = {
+ levelTag: false,
+ host: 'localhost',
+ port: 24224
+ };
+ const fakeSender = {
+ emit: td.function()
+ };
+ td
+ .when(fluentLogger.createFluentSender(tag_prefix, options))
+ .thenReturn(fakeSender);
+ const logger = getLogger(tag_prefix, options);
+
+ logger.info('This is info message!', {extra1: true, extra2: false}, {extra3: true});
+
+ td.verify(fakeSender.emit({
+ timestamp: td.matchers.anything(),
+ category: 'default',
+ levelInt: 20000,
+ levelStr: 'INFO',
+ context: {},
+ data: 'This is info message!',
+ extra: {
+ extra1: true,
+ extra2: false,
+ extra3: true,
+ }
+ }));
+ done();
+ });
+
});
From 74017d104489dd6d96652f4f830465286a9d7985 Mon Sep 17 00:00:00 2001
From: Kochan Piotr
Date: Thu, 1 Feb 2018 09:54:29 +0100
Subject: [PATCH 2/2] node 4 compatibility
---
.gitignore | 1 -
lib/index.js | 8 +++++---
package.json | 5 +++--
3 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/.gitignore b/.gitignore
index 7aa7fb2..2ccbe46 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1 @@
/node_modules/
-.idea
\ No newline at end of file
diff --git a/lib/index.js b/lib/index.js
index 70df75a..70fa4cd 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -3,9 +3,10 @@
* log4js appender support
*/
const FluentSender = require('fluent-logger');
-const util = require('util');
const DEFAULT_TAG = 'log4js';
+require('es6-object-assign').polyfill();
+
/**
*
* log4js.configure({
@@ -44,8 +45,8 @@ function fluentAppender(config, layout) {
data: data
};
- if (extra.length) {
- rec['extra'] = Object.assign(...extra);
+ if (extra.length > 0) {
+ rec['extra'] = Object.assign.apply(null, extra);
}
if (options.levelTag !== false) {
@@ -57,6 +58,7 @@ function fluentAppender(config, layout) {
appender.shutdown = (complete) => {
logSender.end(null, null, complete);
};
+
['addListener', 'on', 'once', 'removeListener', 'removeAllListeners', 'setMaxListeners', 'getMaxListeners', '_setupErrorHandler'].forEach((attr, i) => {
appender[attr] = function() {
return logSender[attr].apply(logSender, Array.prototype.slice.call(arguments));
diff --git a/package.json b/package.json
index 88d01e1..4e34f63 100644
--- a/package.json
+++ b/package.json
@@ -20,8 +20,9 @@
"fluentd"
],
"dependencies": {
- "log4js": ">= 2",
- "fluent-logger": ""
+ "es6-object-assign": "^1.1.0",
+ "fluent-logger": "",
+ "log4js": ">= 2"
},
"devDependencies": {
"async": "",