gelfAppender-test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. "use strict";
  2. var vows = require('vows')
  3. , assert = require('assert')
  4. , sandbox = require('sandboxed-module')
  5. , log4js = require('../lib/log4js')
  6. , realLayouts = require('../lib/layouts')
  7. , setupLogging = function(options, category, compressedLength) {
  8. var fakeDgram = {
  9. sent: false,
  10. socket: {
  11. packetLength: 0,
  12. closed: false,
  13. close: function() {
  14. this.closed = true;
  15. },
  16. send: function(pkt, offset, pktLength, port, host) {
  17. fakeDgram.sent = true;
  18. this.packet = pkt;
  19. this.offset = offset;
  20. this.packetLength = pktLength;
  21. this.port = port;
  22. this.host = host;
  23. }
  24. },
  25. createSocket: function(type) {
  26. this.type = type;
  27. return this.socket;
  28. }
  29. }
  30. , fakeZlib = {
  31. gzip: function(objectToCompress, callback) {
  32. fakeZlib.uncompressed = objectToCompress;
  33. if (this.shouldError) {
  34. callback({ stack: "oh noes" });
  35. return;
  36. }
  37. if (compressedLength) {
  38. callback(null, { length: compressedLength });
  39. } else {
  40. callback(null, "I've been compressed");
  41. }
  42. }
  43. }
  44. , exitHandler
  45. , fakeConsole = {
  46. error: function(message) {
  47. this.message = message;
  48. }
  49. }
  50. , fakeLayouts = {
  51. layout: function(type, options) {
  52. this.type = type;
  53. this.options = options;
  54. return realLayouts.messagePassThroughLayout;
  55. },
  56. messagePassThroughLayout: realLayouts.messagePassThroughLayout
  57. }
  58. , appender = sandbox.require('../lib/appenders/gelf', {
  59. requires: {
  60. dgram: fakeDgram,
  61. zlib: fakeZlib,
  62. '../layouts': fakeLayouts
  63. },
  64. globals: {
  65. process: {
  66. on: function(evt, handler) {
  67. if (evt === 'exit') {
  68. exitHandler = handler;
  69. }
  70. }
  71. },
  72. console: fakeConsole
  73. }
  74. });
  75. log4js.clearAppenders();
  76. log4js.addAppender(appender.configure(options || {}), category || "gelf-test");
  77. return {
  78. dgram: fakeDgram,
  79. compress: fakeZlib,
  80. exitHandler: exitHandler,
  81. console: fakeConsole,
  82. layouts: fakeLayouts,
  83. logger: log4js.getLogger(category || "gelf-test")
  84. };
  85. };
  86. vows.describe('log4js gelfAppender').addBatch({
  87. 'with default gelfAppender settings': {
  88. topic: function() {
  89. var setup = setupLogging();
  90. setup.logger.info("This is a test");
  91. return setup;
  92. },
  93. 'the dgram packet': {
  94. topic: function(setup) {
  95. return setup.dgram;
  96. },
  97. 'should be sent via udp to the localhost gelf server': function(dgram) {
  98. assert.equal(dgram.type, "udp4");
  99. assert.equal(dgram.socket.host, "localhost");
  100. assert.equal(dgram.socket.port, 12201);
  101. assert.equal(dgram.socket.offset, 0);
  102. assert.ok(dgram.socket.packetLength > 0, "Received blank message");
  103. },
  104. 'should be compressed': function(dgram) {
  105. assert.equal(dgram.socket.packet, "I've been compressed");
  106. }
  107. },
  108. 'the uncompressed log message': {
  109. topic: function(setup) {
  110. var message = JSON.parse(setup.compress.uncompressed);
  111. return message;
  112. },
  113. 'should be in the gelf format': function(message) {
  114. assert.equal(message.version, '1.0');
  115. assert.equal(message.host, require('os').hostname());
  116. assert.equal(message.level, 6); //INFO
  117. assert.equal(message.facility, 'nodejs-server');
  118. assert.equal(message.full_message, message.short_message);
  119. assert.equal(message.full_message, 'This is a test');
  120. }
  121. }
  122. },
  123. 'with a message longer than 8k': {
  124. topic: function() {
  125. var setup = setupLogging(undefined, undefined, 10240);
  126. setup.logger.info("Blah.");
  127. return setup;
  128. },
  129. 'the dgram packet': {
  130. topic: function(setup) {
  131. return setup.dgram;
  132. },
  133. 'should not be sent': function(dgram) {
  134. assert.equal(dgram.sent, false);
  135. }
  136. }
  137. },
  138. 'with non-default options': {
  139. topic: function() {
  140. var setup = setupLogging({
  141. host: 'somewhere',
  142. port: 12345,
  143. hostname: 'cheese',
  144. facility: 'nonsense'
  145. });
  146. setup.logger.debug("Just testing.");
  147. return setup;
  148. },
  149. 'the dgram packet': {
  150. topic: function(setup) {
  151. return setup.dgram;
  152. },
  153. 'should pick up the options': function(dgram) {
  154. assert.equal(dgram.socket.host, 'somewhere');
  155. assert.equal(dgram.socket.port, 12345);
  156. }
  157. },
  158. 'the uncompressed packet': {
  159. topic: function(setup) {
  160. var message = JSON.parse(setup.compress.uncompressed);
  161. return message;
  162. },
  163. 'should pick up the options': function(message) {
  164. assert.equal(message.host, 'cheese');
  165. assert.equal(message.facility, 'nonsense');
  166. }
  167. }
  168. },
  169. 'on process.exit': {
  170. topic: function() {
  171. var setup = setupLogging();
  172. setup.exitHandler();
  173. return setup;
  174. },
  175. 'should close open sockets': function(setup) {
  176. assert.isTrue(setup.dgram.socket.closed);
  177. }
  178. },
  179. 'on zlib error': {
  180. topic: function() {
  181. var setup = setupLogging();
  182. setup.compress.shouldError = true;
  183. setup.logger.info('whatever');
  184. return setup;
  185. },
  186. 'should output to console.error': function(setup) {
  187. assert.equal(setup.console.message, 'oh noes');
  188. }
  189. },
  190. 'with layout in configuration': {
  191. topic: function() {
  192. var setup = setupLogging({
  193. layout: {
  194. type: 'madeuplayout',
  195. earlgrey: 'yes, please'
  196. }
  197. });
  198. return setup;
  199. },
  200. 'should pass options to layout': function(setup) {
  201. assert.equal(setup.layouts.type, 'madeuplayout');
  202. assert.equal(setup.layouts.options.earlgrey, 'yes, please');
  203. }
  204. },
  205. 'with custom fields options': {
  206. topic: function() {
  207. var setup = setupLogging({
  208. host: 'somewhere',
  209. port: 12345,
  210. hostname: 'cheese',
  211. facility: 'nonsense',
  212. customFields: {
  213. _every1: 'Hello every one',
  214. _every2: 'Hello every two'
  215. }
  216. });
  217. var myFields = {
  218. GELF: true,
  219. _every2: 'Overwritten!',
  220. _myField: 'This is my field!'
  221. };
  222. setup.logger.debug(myFields, "Just testing.");
  223. return setup;
  224. },
  225. 'the dgram packet': {
  226. topic: function(setup) {
  227. return setup.dgram;
  228. },
  229. 'should pick up the options': function(dgram) {
  230. assert.equal(dgram.socket.host, 'somewhere');
  231. assert.equal(dgram.socket.port, 12345);
  232. }
  233. },
  234. 'the uncompressed packet': {
  235. topic: function(setup) {
  236. var message = JSON.parse(setup.compress.uncompressed);
  237. return message;
  238. },
  239. 'should pick up the options': function(message) {
  240. assert.equal(message.host, 'cheese');
  241. assert.equal(message.facility, 'nonsense');
  242. assert.equal(message._every1, 'Hello every one'); // the default value
  243. assert.equal(message._every2, 'Overwritten!'); // the overwritten value
  244. assert.equal(message._myField, 'This is my field!'); // the value for this message only
  245. assert.equal(message.short_message, 'Just testing.'); // skip the field object
  246. assert.equal(message.full_message, 'Just testing.'); // should be as same as short_message
  247. }
  248. }
  249. }
  250. }).export(module);