10000 Merge pull request #357 from shakacode/add-loggly-tagged · mKoder/log4js-node@b1ae121 · GitHub
[go: up one dir, main page]

Skip to content

Commit b1ae121

Browse files
committed
Merge pull request log4js-node#357 from shakacode/add-loggly-tagged
Add new appender logglyTagged
2 parents b1a36cb + a9f52fa commit b1ae121

File tree

2 files changed

+95
-21
lines changed

2 files changed

+95
-21
lines changed

lib/appenders/loggly.js

Lines changed: 49 additions & 3 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,46 @@ var layouts = require('../layouts')
55
, passThrough = layouts.messagePassThroughLayout;
66

77

8+
function isAnyObject(value) {
9+
return value != null && (typeof value === 'object' || typeof value === 'function');
10+
}
11+
12+
function numKeys(o) {
13+
var res = 0;
14+
for (var k in o) {
15+
if (o.hasOwnProperty(k)) res++;
16+
}
17+
return res;
18+
}
19+
820
/**
9-
* Loggly Appender. Sends logging events to Loggly using node-loggly
21+
* @param msg - array of args for logging.
22+
* @returns { deTaggedMsg: [], additionalTags: [] }
23+
*/
24+
function processTags(msgListArgs) {
25+
var msgList = (msgListArgs.length === 1 ? [msgListArgs[0]] : Array.apply(null, msgListArgs));
26+
27+
return msgList.reduce(function (accum, element, currentIndex, array) {
28+
if (isAnyObject(element) && Array.isArray(element.tags) && numKeys(element) == 1) {
29+
accum.additionalTags = accum.additionalTags.concat(element.tags);
30+
} else {
31+
accum.deTaggedData.push(element);
32+
}
33+
return accum;
34+
}, { deTaggedData: [], additionalTags: [] });
35+
}
36+
37+
/**
38+
* Loggly Appender. Sends logging events to Loggly using node-loggly, optionally adding tags.
39+
*
40+
* This appender will scan the msg from the logging event, and pull out any argument of the
41+
* shape `{ tags: [] }` so that it's possibleto add tags in a normal logging call.
42+
*
43+
* For example:
44+
*
45+
* logger.info({ tags: ['my-tag-1', 'my-tag-2'] }, 'Some message', someObj, ...)
46+
*
47+
* And then this appender will remove the tags param and append it to the config.tags.
1048
*
1149
* @param config object with loggly configuration data
1250
* {
@@ -21,13 +59,21 @@ function logglyAppender(config, layout) {
2159
if(!layout) layout = passThrough;
2260

2361
return function(loggingEvent) {
24-
var msg = layout(loggingEvent);
62+
var result = processTags(loggingEvent.data);
63+
var deTaggedData = result.deTaggedData;
64+
var additionalTags = result.additionalTags;
65+
66+
// Replace the data property with the deTaggedData
67+
loggingEvent.data = deTaggedData;
68+
69+
var msg = layout(loggingEvent);
70+
2571
client.log({
2672
msg: msg,
2773
level: loggingEvent.level.levelStr,
2874
category: loggingEvent.categoryName,
2975
hostname: os.hostname().toString(),
30-
});
76+
}, additionalTags);
3177
}
3278
}
3379

test/logglyAppender-test.js

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"use strict";
22
var vows = require('vows')
3-
, assert = require('assert')
4-
, log4js = require('../lib/log4js')
5-
, sandbox = require('sandboxed-module')
6-
;
3+
, assert = require('assert')
4+
, log4js = require('../lib/log4js')
5+
, sandbox = require('sandboxed-module')
6+
;
77

88
function setupLogging(category, options) {
99
var msgs = [];
10-
10+
1111
var fakeLoggly = {
12-
createClient: function (options) {
12+
createClient: function(options) {
1313
return {
1414
config: options,
15-
log: function (msg, tags) {
15+
log: function(msg, tags) {
1616
msgs.push({
1717
msg: msg,
1818
tags: tags
@@ -50,7 +50,7 @@ function setupLogging(category, options) {
5050
});
5151

5252
log4js.addAppender(logglyModule.configure(options), category);
53-
53+
5454
return {
5555
logger: log4js.getLogger(category),
5656
loggly: fakeLoggly,
@@ -61,22 +61,50 @@ function setupLogging(category, options) {
6161
}
6262

6363
log4js.clearAppenders();
64+
65+
function setupTaggedLogging() {
66+
return setupLogging('loggly', {
67+
token: 'your-really-long-input-token',
68+
subdomain: 'your-subdomain',
69+
tags: ['loggly-tag1', 'loggly-tag2', 'loggly-tagn']
70+
});
71+
}
72+
6473
vows.describe('log4js logglyAppender').addBatch({
65-
'minimal config': {
74+
'with minimal config': {
6675
topic: function() {
67-
var setup = setupLogging('loggly', {
68-
token: 'your-really-long-input-token',
69-
subdomain: 'your-subdomain',
70-
tags: ['loggly-tag1', 'loggly-tag2', 'loggly-tagn']
71-
});
72-
73-
setup.logger.log('trace', 'Log event #1');
76+
var setup = setupTaggedLogging();
77+
setup.logger.log('trace', 'Log event #1', 'Log 2', { tags: ['tag1', 'tag2'] });
7478
return setup;
7579
},
76-
'there should be one message only': function (topic) {
77-
//console.log('topic', topic);
80+
'has a results.length of 1': function(topic) {
7881
assert.equal(topic.results.length, 1);
82+
},
83+
'has a result msg with both args concatenated': function(topic) {
84+
assert.equal(topic.results[0].msg.msg, 'Log event #1 Log 2');
85+
},
86+
'has a result tags with the arg that contains tags': function(topic) {
87+
assert.deepEqual(topic.results[0].tags, ['tag1', 'tag2']);
7988
}
8089
}
90+
}).addBatch({
91+
'config with object with tags and other keys': {
92+
topic: function() {
93+
var setup = setupTaggedLogging();
8194

95+
// ignore this tags object b/c there are 2 keys
96+
setup.logger.log('trace', 'Log event #1', { other: 'other', tags: ['tag1', 'tag2'] });
97+
return setup;
98+
},
99+
'has a results.length of 1': function(topic) {
100+
assert.equal(topic.results.length, 1);
101+
},
102+
'has a result msg with the args concatenated': function(topic) {
103+
assert.equal(topic.results[0].msg.msg,
104+
'Log event #1 { other: \'other\', tags: [ \'tag1\', \'tag2\' ] }');
105+
},
106+
'has a result tags with the arg that contains no tags': function(topic) {
107+
assert.deepEqual(topic.results[0].tags, []);
108+
}
109+
}
82110
}).export(module);

0 commit comments

Comments
 (0)
0