From 3a6f670be818db2bf25328a703b80160c7a6932a Mon Sep 17 00:00:00 2001 From: Chris Potter Date: Thu, 11 Feb 2016 17:13:05 -0500 Subject: [PATCH 1/5] Add hipchat and hipchat-test according to mailgun appender. --- lib/appenders/hipchat.js | 56 +++++++++++ test/hipchatAppender-test.js | 182 +++++++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 lib/appenders/hipchat.js create mode 100644 test/hipchatAppender-test.js diff --git a/lib/appenders/hipchat.js b/lib/appenders/hipchat.js new file mode 100644 index 0000000..6e457fa --- /dev/null +++ b/lib/appenders/hipchat.js @@ -0,0 +1,56 @@ +"use strict"; +var HipChatClient = require('hipchat-client'); +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) { + + config = _config; + 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; } + console.log(res); + }); + }; +} + +function configure(_config) { + config = _config; + + if (_config.layout) { + layout = layouts.layout(_config.layout.type, _config.layout); + } + + hipchat = new HipChatClient(_config.api_key); + + return hipchatAppender(_config, layout); +} + +exports.appender = hipchatAppender; +exports.configure = configure; diff --git a/test/hipchatAppender-test.js b/test/hipchatAppender-test.js new file mode 100644 index 0000000..9acee30 --- /dev/null +++ b/test/hipchatAppender-test.js @@ -0,0 +1,182 @@ +"use strict"; +var vows = require('vows'); +var assert = require('assert'); +var log4js = require('../lib/log4js'); +var sandbox = require('sandboxed-module'); + +function setupLogging(category, options) { + var msgs = []; + + var hipchatCredentials = { + api_key: options.api_key, + room_id: options.room_id, + from: options.from, + message: options.message, + format: options.format, + color: options.color, + notify: options.notify + }; + var fakeHipchat = (function (key) { + function constructor() { + return { + options: key, + api: { + rooms: { + message: function (data, callback) { + msgs.push(data); + callback(false, {status: "sent"}); + } + } + } + } + } + + return constructor(key); + }); + + var fakeLayouts = { + layout: function (type, config) { + this.type = type; + this.config = config; + return log4js.layouts.messagePassThroughLayout; + }, + basicLayout: log4js.layouts.basicLayout, + coloredLayout: log4js.layouts.coloredLayout, + messagePassThroughLayout: log4js.layouts.messagePassThroughLayout + }; + + var fakeConsole = { + errors: [], + logs: [], + error: function (msg, value) { + this.errors.push({msg: msg, value: value}); + }, + log: function (msg, value) { + this.logs.push({msg: msg, value: value}); + } + }; + + + var hipchatModule = sandbox.require('../lib/appenders/hipchat', { + requires: { + 'hipchat-client': fakeHipchat, + '../layouts': fakeLayouts + }, + globals: { + console: fakeConsole + } + }); + + + log4js.addAppender(hipchatModule.configure(options), category); + + return { + logger: log4js.getLogger(category), + mailer: fakeHipchat, + layouts: fakeLayouts, + console: fakeConsole, + messages: msgs, + credentials: hipchatCredentials + }; +} + +function checkMessages(result) { + for (var i = 0; i < result.messages.length; ++i) { + assert.equal(result.messages[i].from, 'FROM'); + assert.equal(result.messages[i].room_id, 'ROOMID'); + assert.ok(new RegExp('.+Log event #' + (i + 1)).test(result.messages[i].message)); + } +} + +log4js.clearAppenders(); + +vows.describe('log4js hipchatAppender').addBatch({ + 'hipchat setup': { + topic: setupLogging('hipchat setup', { + api_key: 'APIKEY', + room_id: "ROOMID", + from: "FROM", + message: "This is the message", + format: "FORMAT", + color: "This is the color", + notify: "NOTIFY" + }), + 'hipchat credentials should match': function (result) { + assert.equal(result.credentials.api_key, 'APIKEY'); + assert.equal(result.credentials.room_id, 'ROOMID'); + assert.equal(result.credentials.from, 'FROM'); + assert.equal(result.credentials.format, 'FORMAT'); + assert.equal(result.credentials.notify, 'NOTIFY'); + + } + }, + + 'basic usage': { + topic: function () { + var setup = setupLogging('basic usage', { + api_key: 'APIKEY', + room_id: "ROOMID", + from: "FROM", + message: "This is the message", + format: "FORMAT", + color: "This is the color", + notify: "NOTIFY" + }); + + setup.logger.info("Log event #1"); + return setup; + }, + 'there should be one message only': function (result) { + assert.equal(result.messages.length, 1); + }, + 'message should contain proper data': function (result) { + checkMessages(result); + } + }, + 'config with layout': { + topic: function () { + var setup = setupLogging('config with layout', { + layout: { + type: "tester" + } + }); + return setup; + }, + 'should configure layout': function (result) { + assert.equal(result.layouts.type, 'tester'); + } + }, + 'separate notification for each event': { + topic: function () { + var self = this; + var setup = setupLogging('separate notification for each event', { + api_key: 'APIKEY', + room_id: "ROOMID", + from: "FROM", + message: "This is the message", + format: "FORMAT", + color: "This is the color", + notify: "NOTIFY" + }); + setTimeout(function () { + setup.logger.info('Log event #1'); + }, 0); + setTimeout(function () { + setup.logger.info('Log event #2'); + }, 500); + setTimeout(function () { + setup.logger.info('Log event #3'); + }, 1100); + setTimeout(function () { + self.callback(null, setup); + }, 3000); + }, + 'there should be three messages': function (result) { + assert.equal(result.messages.length, 3); + }, + 'messages should contain proper data': function (result) { + checkMessages(result); + } + } +}).export(module); + From 36d189c595bd33814298a8057bc03018d0148d93 Mon Sep 17 00:00:00 2001 From: Chris Potter Date: Thu, 11 Feb 2016 17:37:17 -0500 Subject: [PATCH 2/5] Add name to hipchat entity. --- lib/appenders/hipchat.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/appenders/hipchat.js b/lib/appenders/hipchat.js index 6e457fa..c4eda8d 100644 --- a/lib/appenders/hipchat.js +++ b/lib/appenders/hipchat.js @@ -52,5 +52,6 @@ function configure(_config) { return hipchatAppender(_config, layout); } +exports.name = 'hipchat'; exports.appender = hipchatAppender; exports.configure = configure; From d35c891a1b1df7e24b4eb2309ddf431f5ad5ae17 Mon Sep 17 00:00:00 2001 From: Chris Potter Date: Thu, 11 Feb 2016 21:19:56 -0500 Subject: [PATCH 3/5] Add example file for reference. --- examples/hipchat-appender.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 examples/hipchat-appender.js diff --git a/examples/hipchat-appender.js b/examples/hipchat-appender.js new file mode 100644 index 0000000..7b8b3db --- /dev/null +++ b/examples/hipchat-appender.js @@ -0,0 +1,28 @@ +//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 +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" + } + ] +}); + +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"); \ No newline at end of file From dc7bda44f0d679c4fe36078f712c63ea96c926d4 Mon Sep 17 00:00:00 2001 From: Chris Potter Date: Thu, 11 Feb 2016 21:34:26 -0500 Subject: [PATCH 4/5] Removed unneeded console.log and extraneous variable declarations. --- lib/appenders/hipchat.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/appenders/hipchat.js b/lib/appenders/hipchat.js index c4eda8d..f72f812 100644 --- a/lib/appenders/hipchat.js +++ b/lib/appenders/hipchat.js @@ -19,29 +19,26 @@ var colours = { function hipchatAppender(_config, _layout) { - config = _config; layout = _layout || layouts.basicLayout; return function (loggingEvent) { var data = { - room_id: config.room_id, - from: config.from, + room_id: _config.room_id, + from: _config.from, message: layout(loggingEvent, config.timezoneOffset), - format: config.format, + format: _config.format, color: colours[loggingEvent.level.toString()], - notify: config.notify + notify: _config.notify }; hipchat.api.rooms.message(data, function (err, res) { if (err) { throw err; } - console.log(res); }); }; } function configure(_config) { - config = _config; if (_config.layout) { layout = layouts.layout(_config.layout.type, _config.layout); From cd92505bbf8909faf345f462f282729e37aec3fe Mon Sep 17 00:00:00 2001 From: Chris Potter Date: Thu, 11 Feb 2016 21:43:20 -0500 Subject: [PATCH 5/5] Missed one config -> _config --- lib/appenders/hipchat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/appenders/hipchat.js b/lib/appenders/hipchat.js index f72f812..6f47f81 100644 --- a/lib/appenders/hipchat.js +++ b/lib/appenders/hipchat.js @@ -26,7 +26,7 @@ function hipchatAppender(_config, _layout) { var data = { room_id: _config.room_id, from: _config.from, - message: layout(loggingEvent, config.timezoneOffset), + message: layout(loggingEvent, _config.timezoneOffset), format: _config.format, color: colours[loggingEvent.level.toString()], notify: _config.notify