Skip to content

Commit

Permalink
Merge pull request #381 from BlueAcornInc/briceburg/hipchat-apiv2
Browse files Browse the repository at this point in the history
use HipChat apiv2 in hipchat appender
  • Loading branch information
nomiddlename committed Jun 14, 2016
2 parents 5d9e08a + af8dc5d commit eb96085
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 230 deletions.
60 changes: 43 additions & 17 deletions examples/hipchat-appender.js
@@ -1,28 +1,54 @@
//Note that hipchat appender needs hipchat-client to work.
//If you haven't got hipchat-client installed, you'll get cryptic
//"cannot find module" errors when using the hipchat appender
/**
* !!! The hipchat-appender requires `hipchat-notifier` from npm, e.g.
* - list as a dependency in your application's package.json ||
* - npm install hipchat-notifier
*/

var log4js = require('../lib/log4js');

log4js.configure({
"appenders": [
{
"type" : "hipchat",
"api_key": 'Hipchat_API_V1_Key',
"room_id": "Room_ID",
"from": "Tester",
"format": "text",
"notify": "NOTIFY",
"category" : "hipchat"
"hipchat_token": process.env.HIPCHAT_TOKEN || '< User token with Notification Privileges >',
"hipchat_room": process.env.HIPCHAT_ROOM || '< Room ID or Name >'
}
]
});

var logger = log4js.getLogger("hipchat");
logger.warn("Test Warn message");//yello
logger.info("Test Info message");//green
logger.debug("Test Debug Message");//hipchat client has limited color scheme
logger.trace("Test Trace Message");//so debug and trace are the same color: purple
logger.fatal("Test Fatal Message");//hipchat client has limited color scheme
logger.error("Test Error Message");// fatal and error are same color: red
logger.all("Test All message");//grey
//logger.debug("Test log message");
logger.warn("Test Warn message");
logger.info("Test Info message");
logger.debug("Test Debug Message");
logger.trace("Test Trace Message");
logger.fatal("Test Fatal Message");
logger.error("Test Error Message");


// alternative configuration demonstrating callback + custom layout
///////////////////////////////////////////////////////////////////

// use a custom layout function (in this case, the provided basicLayout)
// format: [TIMESTAMP][LEVEL][category] - [message]
var customLayout = require('../lib/layouts').basicLayout;

log4js.configure({
"appenders": [
{
"type" : "hipchat",
"hipchat_token": process.env.HIPCHAT_TOKEN || '< User token with Notification Privileges >',
"hipchat_room": process.env.HIPCHAT_ROOM || '< Room ID or Name >',
"hipchat_from": "Mr. Semantics",
"hipchat_notify": false,
"hipchat_response_callback": function(err, response, body){
if(err || response.statusCode > 300){
throw new Error('hipchat-notifier failed');
}
console.log('mr semantics callback success');
},
"layout": customLayout
}
]
});

logger.info("Test customLayout from Mr. Semantics");
124 changes: 80 additions & 44 deletions lib/appenders/hipchat.js
@@ -1,54 +1,90 @@
"use strict";
var HipChatClient = require('hipchat-client');

var hipchat = require('hipchat-notifier');
var layouts = require('../layouts');
var layout;

var hipchat, config;

//hipchat has more limited colors
var colours = {
ALL: "grey",
TRACE: "purple",
DEBUG: "purple",
INFO: "green",
WARN: "yellow",
ERROR: "red",
FATAL: "red",
OFF: "grey"
};

function hipchatAppender(_config, _layout) {

layout = _layout || layouts.basicLayout;

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
};

hipchat.api.rooms.message(data, function (err, res) {
if (err) { throw err; }
});
};

exports.name = 'hipchat';
exports.appender = hipchatAppender;
exports.configure = hipchatConfigure;

/**
@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"
}
]
});
var logger = log4js.getLogger("hipchat");
logger.warn("Test Warn message");
@invoke
*/

function hipchatNotifierResponseCallback(err, response, body){
if(err) {
throw err;
}
}

function configure(_config) {
function hipchatAppender(config) {

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

if (_config.layout) {
layout = layouts.layout(_config.layout.type, _config.layout);
// @lint W074 This function's cyclomatic complexity is too high. (10)
return function(loggingEvent){

var notifierFn;

notifier.setRoom(config.hipchat_room);
notifier.setFrom(config.hipchat_from || '');
notifier.setNotify(config.hipchat_notify || false);

if(config.hipchat_host) {
notifier.setHost(config.hipchat_host);
}

switch (loggingEvent.level.toString()) {
case "TRACE":
case "DEBUG":
notifierFn = "info";
break;
case "WARN":
notifierFn = "warning";
break;
case "ERROR":
case "FATAL":
notifierFn = "failure";
break;
default:
notifierFn = "success";
}

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

return hipchatAppender(_config, layout);
// dispatch hipchat api request, do not return anything
// [overide hipchatNotifierResponseCallback]
notifier[notifierFn](layoutMessage, config.hipchat_response_callback ||
hipchatNotifierResponseCallback);
};
}

exports.name = 'hipchat';
exports.appender = hipchatAppender;
exports.configure = configure;
function hipchatConfigure(config) {
var layout;

if (!config.layout) {
config.layout = layouts.messagePassThroughLayout;
}

return hipchatAppender(config, layout);
}

0 comments on commit eb96085

Please sign in to comment.