Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
.idea
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this change, please.
I don't want to add IDE specific configuration in this repository.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

11 changes: 9 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,22 @@ 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,
levelStr: loggingEvent.level.levelStr,
context: loggingEvent.context,
data: data
};

if (extra.length) {
rec['extra'] = Object.assign(...extra);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use spread operator.
I want to support Node.js 4 until its EOL (2018-04-30).

}

if (options.levelTag !== false) {
logSender.emit(loggingEvent.level.levelStr, rec);
} else {
Expand Down
33 changes: 33 additions & 0 deletions test/test.log4js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

});