|
1 | 1 | "use strict";
|
2 |
| -var HipChatClient = require('hipchat-client'); |
| 2 | + |
| 3 | +var hipchat = require('hipchat-notifier'); |
3 | 4 | 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 | + } |
39 | 37 | }
|
40 | 38 |
|
41 |
| -function configure(_config) { |
| 39 | +function hipchatAppender(config) { |
| 40 | + |
| 41 | + var notifier = hipchat.make(config.hipchat_room, config.hipchat_token); |
42 | 42 |
|
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"; |
45 | 70 | }
|
46 | 71 |
|
47 |
| - hipchat = new HipChatClient(_config.api_key); |
| 72 | + // @TODO, re-work in timezoneOffset ? |
| 73 | + var layoutMessage = config.layout(loggingEvent); |
48 | 74 |
|
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 | + }; |
50 | 80 | }
|
51 | 81 |
|
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