use hipchat-notifier lib for apiv2 support

This commit is contained in:
Brice Burgess 2016-04-25 16:11:18 -04:00
parent 32070ca8e8
commit 4ab05a5cc9

View File

@ -1,22 +1,34 @@
"use strict";
var HipChatClient = require('hipchat-client');
var hipchat = require('hipchat-notifier');
var layouts = require('../layouts');
exports.name = 'hipchat';
exports.appender = hipchatAppender;
exports.configure = hipchatConfigure;
//hipchat has more limited colors
var colours = {
ALL: "grey",
TRACE: "purple",
DEBUG: "purple",
INFO: "green",
WARN: "yellow",
ERROR: "red",
FATAL: "red",
OFF: "grey"
};
/**
@invoke as
log4js.configure({
"appenders": [
{
"type" : "hipchat",
"hipchat_token": "< User token with Notification Privileges >",
"hipchat_room": "< Room ID or Name >",
// optionl
"hipchat_from": "[ additional from label ]",
"hipchat_notify": "[ notify boolean to bug people ]",
"hipchat_host" : "api.hipchat.com"
}
]
});
@invoke
*/
function hipchatNotifierResponseCallback(err, response, body){
if(err) {
throw err;
}
}
function hipchatAppender(config, layout) {
@ -26,20 +38,38 @@ function hipchatAppender(config, layout) {
return function(loggingEvent) {
var data = {
room_id: _config.room_id,
from: _config.from,
message: layout(loggingEvent, _config.timezoneOffset),
format: _config.format,
color: colours[loggingEvent.level.toString()],
notify: _config.notify
};
notifier.setRoom(config.hipchat_room);
notifier.setRoom(config.hipchat_room);
notifier.setFrom(config.hipchat_from || '');
notifier.setNotify(config.hipchat_notify || false);
client.api.rooms.message(data, function(err, res) {
if (err) {
throw err;
}
});
if(config.hipchat_host) {
notifier.setHost(config.hipchat_host);
}
var notifierFn = "success";
switch (loggingEvent.level.toString()) {
case "TRACE":
case "DEBUG":
notifierFn = "notice";
break;
case "WARN":
notifierFn = "warning";
break;
case "ERROR":
case "FATAL":
notifierFn = "failure";
break;
default:
notifierFn = "info";
}
// @TODO, re-work in timezoneOffset ?
var layoutMessage = layout(loggingEvent);
// dispatch hipchat api request, do not return anything
// [overide hipchatNotifierResponseCallback]
notifier[notifierFn](layoutMessage, hipchatNotifierResponseCallback);
}
}