Skip to content

Commit eb96085

Browse files
authored
Merge pull request log4js-node#381 from BlueAcornInc/briceburg/hipchat-apiv2
use HipChat apiv2 in hipchat appender
2 parents 5d9e08a + af8dc5d commit eb96085

File tree

3 files changed

+223
-230
lines changed

3 files changed

+223
-230
lines changed

examples/hipchat-appender.js

+43-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
1-
//Note that hipchat appender needs hipchat-client to work.
2-
//If you haven't got hipchat-client installed, you'll get cryptic
3-
//"cannot find module" errors when using the hipchat appender
1+
/**
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
5+
*/
6+
47
var log4js = require('../lib/log4js');
58

69
log4js.configure({
710
"appenders": [
811
{
912
"type" : "hipchat",
10-
"api_key": 'Hipchat_API_V1_Key',
11-
"room_id": "Room_ID",
12-
"from": "Tester",
13-
"format": "text",
14-
"notify": "NOTIFY",
15-
"category" : "hipchat"
13+
"hipchat_token": process.env.HIPCHAT_TOKEN || '< User token with Notification Privileges >',
14+
"hipchat_room": process.env.HIPCHAT_ROOM || '< Room ID or Name >'
1615
}
1716
]
1817
});
1918

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

lib/appenders/hipchat.js

+80-44
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,90 @@
11
"use strict";
2-
var HipChatClient = require('hipchat-client');
2+
3+
var hipchat = require('hipchat-notifier');
34
var layouts = require('../layouts');
4-
var layout;
5-
6-
var hipchat, config;
7-
8-
//hipchat has more limited colors
9-
var colours = {
10-
ALL: "grey",
11-
TRACE: "purple",
12-
DEBUG: "purple",
13-
INFO: "green",
14-
WARN: "yellow",
15-
ERROR: "red",
16-
FATAL: "red",
17-
OFF: "grey"
18-
};
19-
20-
function hipchatAppender(_config, _layout) {
21-
22-
layout = _layout || layouts.basicLayout;
23-
24-
return function (loggingEvent) {
25-
26-
var data = {
27-
room_id: _config.room_id,
28-
from: _config.from,
29-
message: layout(loggingEvent, _config.timezoneOffset),
30-
format: _config.format,
31-
color: colours[loggingEvent.level.toString()],
32-
notify: _config.notify
33-
};
34-
35-
hipchat.api.rooms.message(data, function (err, res) {
36-
if (err) { throw err; }
37-
});
38-
};
5+
6+
exports.name = 'hipchat';
7+
exports.appender = hipchatAppender;
8+
exports.configure = hipchatConfigure;
9+
10+
/**
11+
@invoke as
12+
13+
log4js.configure({
14+
"appenders": [
15+
{
16+
"type" : "hipchat",
17+
"hipchat_token": "< User token with Notification Privileges >",
18+
"hipchat_room": "< Room ID or Name >",
19+
// optionl
20+
"hipchat_from": "[ additional from label ]",
21+
"hipchat_notify": "[ notify boolean to bug people ]",
22+
"hipchat_host" : "api.hipchat.com"
23+
}
24+
]
25+
});
26+
27+
var logger = log4js.getLogger("hipchat");
28+
logger.warn("Test Warn message");
29+
30+
@invoke
31+
*/
32+
33+
function hipchatNotifierResponseCallback(err, response, body){
34+
if(err) {
35+
throw err;
36+
}
3937
}
4038

41-
function configure(_config) {
39+
function hipchatAppender(config) {
40+
41+
var notifier = hipchat.make(config.hipchat_room, config.hipchat_token);
4242

43-
if (_config.layout) {
44-
layout = layouts.layout(_config.layout.type, _config.layout);
43+
// @lint W074 This function's cyclomatic complexity is too high. (10)
44+
return function(loggingEvent){
45+
46+
var notifierFn;
47+
48+
notifier.setRoom(config.hipchat_room);
49+
notifier.setFrom(config.hipchat_from || '');
50+
notifier.setNotify(config.hipchat_notify || false);
51+
52+
if(config.hipchat_host) {
53+
notifier.setHost(config.hipchat_host);
54+
}
55+
56+
switch (loggingEvent.level.toString()) {
57+
case "TRACE":
58+
case "DEBUG":
59+
notifierFn = "info";
60+
break;
61+
case "WARN":
62+
notifierFn = "warning";
63+
break;
64+
case "ERROR":
65+
case "FATAL":
66+
notifierFn = "failure";
67+
break;
68+
default:
69+
notifierFn = "success";
4570
}
4671

47-
hipchat = new HipChatClient(_config.api_key);
72+
// @TODO, re-work in timezoneOffset ?
73+
var layoutMessage = config.layout(loggingEvent);
4874

49-
return hipchatAppender(_config, layout);
75+
// dispatch hipchat api request, do not return anything
76+
// [overide hipchatNotifierResponseCallback]
77+
notifier[notifierFn](layoutMessage, config.hipchat_response_callback ||
78+
hipchatNotifierResponseCallback);
79+
};
5080
}
5181

52-
exports.name = 'hipchat';
53-
exports.appender = hipchatAppender;
54-
exports.configure = configure;
82+
function hipchatConfigure(config) {
83+
var layout;
84+
85+
if (!config.layout) {
86+
config.layout = layouts.messagePassThroughLayout;
87+
}
88+
89+
return hipchatAppender(config, layout);
90+
}

0 commit comments

Comments
 (0)