8000 hipchat-appender: allow custom layoutFn · wxqGitHub/log4js-node@beeeac0 · GitHub
[go: up one dir, main page]

Skip to content

Commit beeeac0

Browse files
committed
hipchat-appender: allow custom layoutFn
1 parent 21623c7 commit beeeac0

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

examples/hipchat-appender.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
2-
* !!! The hipchat-appender requires `hipchat-notifier` from npm
3-
* - e.g. list as a dependency in your application's package.json
2+
* !!! The hipchat-appender requires `hipchat-notifier` from npm, e.g.
3+
* - list as a dependency in your application's package.json ||
4+
* - npm install hipchat-notifier
45
*/
56

67
var log4js = require('../lib/log4js');
@@ -9,24 +10,46 @@ log4js.configure({
910
"appenders": [
1011
{
1112
"type" : "hipchat",
12-
"hipchat_token": "< User token with Notification Privileges >",
13-
"hipchat_room": "< Room ID or Name >",
14-
// optional
15-
"hipchat_from": "[ additional from label ]",
16-
"hipchat_notify": "[ notify boolean to bug people ]",
17-
"hipchat_response_callback": function(err, response, body){
18-
console.log("overridden log4js hipchat-appender response callback");
19-
}
13+
"hipchat_token": process.env.HIPCHAT_TOKEN || '< User token with Notification Privileges >',
14+
"hipchat_room": process.env.HIPCHAT_ROOM || '< Room ID or Name >'
2015
}
2116
]
2217
});
2318

2419
var logger = log4js.getLogger("hipchat");
25-
logger.warn("Test Warn message");//yello
26-
logger.info("Test Info message");//green
27-
logger.debug("Test Debug Message");//hipchat client has limited color scheme
28-
logger.trace("Test Trace Message");//so debug and trace are the same color: purple
29-
logger.fatal("Test Fatal Message");//hipchat client has limited color scheme
30-
logger.error("Test Error Message");// fatal and error are same color: red
31-
logger.all("Test All message");//grey
32-
//logger.debug("Test log message");
20+
logger.warn("Test Warn message");
21+
logger.info("Test Info message");
22+
logger.debug("Test Debug Message");
23+
logger.trace("Test Trace Message");
24+
logger.fatal("Test Fatal Message");
25+
logger.error("Test Error Message");
26+
27+
// alternative configuration
28+
29+
// use a custom layout function
30+
// format: [TIMESTAMP][LEVEL][category] - [message]
31+
var customLayout = require('../lib/layouts').basicLayout;
32+
33+
log4js.configure({
34+
"appenders": [
35+
{
36+
"type" : "hipchat",
37+
"hipchat_token": process.env.HIPCHAT_TOKEN || '< User token with Notification Privileges >',
38+
"hipchat_room": process.env.HIPCHAT_ROOM || '< Room ID or Name >',
39+
"hipchat_from": "Mr. Semantics",
40+
"hipchat_notify": false,
41+
"hipchat_response_callback": function(err, response, body){
42+
if(err || response.statusCode > 300){
43+
throw new Error('hipchat-notifier failed');
44+
}
45+
console.log('mr semantics callback success');
46+
},
47+
"layout": customLayout
48+
}
49+
]
50+
});
51+
52+
logger.info("Test from processed by customLayout");
53+
54+
// @TODO: implement configuration of send message to allow HipChat cards &c
55+
// for now, can try to implement by returning object from custom layout

lib/appenders/hipchat.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ function hipchatNotifierResponseCallback(err, response, body){
3636
}
3737
}
3838

39-
function hipchatAppender(config, layout) {
39+
function hipchatAppender(config) {
4040

4141
var notifier = hipchat.make(config.hipchat_room, config.hipchat_token);
4242

43-
layout = layout || layouts.messagePassThroughLayout;
44-
4543
// @lint W074 This function's cyclomatic complexity is too high. (10)
4644
return function(loggingEvent){
4745

@@ -73,7 +71,7 @@ function hipchatAppender(config, layout) {
7371
}
7472

7573
// @TODO, re-work in timezoneOffset ?
76-
var layoutMessage = layout(loggingEvent);
74+
var layoutMessage = config.layout(loggingEvent);
7775

7876
// dispatch hipchat api request, do not return anything
7977
// [overide hipchatNotifierResponseCallback]
@@ -84,8 +82,13 @@ function hipchatAppender(config, layout) {
8482

8583
function hipchatConfigure(config) {
8684
var layout;
87-
if (config.layout) {
88-
layout = layouts.layout(config.layout.type, config.layout);
85+
86+
if (!config.layout) {
87+
config.layout = layouts.messagePassThroughLayout;
8988
}
89+
90+
// @TODO: implement configuration of send message to allow HipChat cards &c
91+
// for now, can try to implement by returning object from custom layout
92+
9093
return hipchatAppender(config, layout);
9194
}

0 commit comments

Comments
 (0)
0