hipchat-appender: allow custom layoutFn

This commit is contained in:
Brice Burgess 2016-04-25 17:41:18 -04:00
parent 21623c7947
commit beeeac0810
2 changed files with 50 additions and 24 deletions

View File

@ -1,6 +1,7 @@
/**
* !!! The hipchat-appender requires `hipchat-notifier` from npm
* - e.g. list as a dependency in your application's package.json
* !!! 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');
@ -9,24 +10,46 @@ log4js.configure({
"appenders": [
{
"type" : "hipchat",
"hipchat_token": "< User token with Notification Privileges >",
"hipchat_room": "< Room ID or Name >",
// optional
"hipchat_from": "[ additional from label ]",
"hipchat_notify": "[ notify boolean to bug people ]",
"hipchat_response_callback": function(err, response, body){
console.log("overridden log4js hipchat-appender response callback");
}
"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
// use a custom layout function
// 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 from processed by customLayout");
// @TODO: implement configuration of send message to allow HipChat cards &c
// for now, can try to implement by returning object from custom layout

View File

@ -36,12 +36,10 @@ function hipchatNotifierResponseCallback(err, response, body){
}
}
function hipchatAppender(config, layout) {
function hipchatAppender(config) {
var notifier = hipchat.make(config.hipchat_room, config.hipchat_token);
layout = layout || layouts.messagePassThroughLayout;
// @lint W074 This function's cyclomatic complexity is too high. (10)
return function(loggingEvent){
@ -73,7 +71,7 @@ function hipchatAppender(config, layout) {
}
// @TODO, re-work in timezoneOffset ?
var layoutMessage = layout(loggingEvent);
var layoutMessage = config.layout(loggingEvent);
// dispatch hipchat api request, do not return anything
// [overide hipchatNotifierResponseCallback]
@ -84,8 +82,13 @@ function hipchatAppender(config, layout) {
function hipchatConfigure(config) {
var layout;
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
if (!config.layout) {
config.layout = layouts.messagePassThroughLayout;
}
// @TODO: implement configuration of send message to allow HipChat cards &c
// for now, can try to implement by returning object from custom layout
return hipchatAppender(config, layout);
}