transport.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. var assert = require('assert'),
  2. winston = require('../../lib/winston'),
  3. helpers = require('../helpers');
  4. module.exports = function (transport, options) {
  5. var logger = transport instanceof winston.Logger
  6. ? transport
  7. : new winston.Logger({
  8. transports: [
  9. new transport(options)
  10. ]
  11. });
  12. // hack to fix transports that don't log
  13. // any unit of time smaller than seconds
  14. var common = require('../../lib/winston/common');
  15. common.timestamp = function() {
  16. return new Date().toISOString();
  17. };
  18. var transport = logger.transports[logger._names[0]];
  19. var out = {
  20. 'topic': logger,
  21. 'when passed valid options': {
  22. 'should have the proper methods defined': function () {
  23. switch (transport.name) {
  24. case 'console':
  25. helpers.assertConsole(transport);
  26. break;
  27. case 'file':
  28. helpers.assertFile(transport);
  29. break;
  30. case 'couchdb':
  31. helpers.assertCouchdb(transport);
  32. break;
  33. }
  34. assert.isFunction(transport.log);
  35. }
  36. },
  37. 'the log() method': helpers.testNpmLevels(transport,
  38. 'should respond with true', function (ign, err, logged) {
  39. assert.isNull(err);
  40. assert.isNotNull(logged);
  41. }
  42. ),
  43. 'the stream() method': {
  44. 'using no options': {
  45. 'topic': function () {
  46. if (!transport.stream) return;
  47. logger.log('info', 'hello world', {});
  48. var cb = this.callback,
  49. j = 10,
  50. i = 10,
  51. results = [],
  52. stream = logger.stream();
  53. stream.on('log', function (log) {
  54. results.push(log);
  55. results.stream = stream;
  56. if (!--j) cb(null, results);
  57. });
  58. stream.on('error', function (err) {
  59. j = -1; //don't call the callback again
  60. cb(err);
  61. });
  62. while (i--) logger.log('info', 'hello world ' + i, {});
  63. },
  64. 'should stream logs': function (err, results) {
  65. if (!transport.stream) return;
  66. assert.isNull(err);
  67. results.forEach(function (log) {
  68. assert.ok(log.message.indexOf('hello world') === 0
  69. || log.message.indexOf('test message') === 0);
  70. });
  71. results.stream.destroy();
  72. }
  73. },
  74. 'using the `start` option': {
  75. 'topic': function () {
  76. if (!transport.stream) return;
  77. var cb = this.callback,
  78. stream = logger.stream({ start: 0 });
  79. stream.on('log', function (log) {
  80. log.stream = stream;
  81. if (cb) cb(null, log);
  82. cb = null;
  83. });
  84. },
  85. 'should stream logs': function (err, log) {
  86. if (!transport.stream) return;
  87. assert.isNull(err);
  88. assert.isNotNull(log.message);
  89. log.stream.destroy();
  90. }
  91. }
  92. },
  93. 'after the logs have flushed': {
  94. topic: function () {
  95. setTimeout(this.callback, 1000);
  96. },
  97. 'the query() method': {
  98. 'using basic querying': {
  99. 'topic': function () {
  100. if (!transport.query) return;
  101. var cb = this.callback;
  102. logger.log('info', 'hello world', {}, function () {
  103. logger.query(cb);
  104. });
  105. },
  106. 'should return matching results': function (err, results) {
  107. if (!transport.query) return;
  108. assert.isNull(err);
  109. results = results[transport.name];
  110. while (!Array.isArray(results)) {
  111. results = results[Object.keys(results).pop()];
  112. }
  113. var log = results.pop();
  114. assert.ok(log.message.indexOf('hello world') === 0
  115. || log.message.indexOf('test message') === 0);
  116. }
  117. },
  118. 'using the `rows` option': {
  119. 'topic': function () {
  120. if (!transport.query) return;
  121. var cb = this.callback;
  122. logger.log('info', 'hello world', {}, function () {
  123. logger.query({ rows: 1 }, cb);
  124. });
  125. },
  126. 'should return one result': function (err, results) {
  127. if (!transport.query) return;
  128. assert.isNull(err);
  129. results = results[transport.name];
  130. while (!Array.isArray(results)) {
  131. results = results[Object.keys(results).pop()];
  132. }
  133. assert.equal(results.length, 1);
  134. }
  135. },
  136. 'using `fields` and `order` option': {
  137. 'topic': function () {
  138. if (!transport.query) return;
  139. var cb = this.callback;
  140. logger.log('info', 'hello world', {}, function () {
  141. logger.query({ order: 'asc', fields: ['timestamp'] }, cb);
  142. });
  143. },
  144. 'should return matching results': function (err, results) {
  145. if (!transport.query) return;
  146. assert.isNull(err);
  147. results = results[transport.name];
  148. while (!Array.isArray(results)) {
  149. results = results[Object.keys(results).pop()];
  150. }
  151. assert.equal(Object.keys(results[0]).length, 1);
  152. assert.ok(new Date(results.shift().timestamp)
  153. < new Date(results.pop().timestamp));
  154. }
  155. },
  156. 'using the `from` and `until` option': {
  157. 'topic': function () {
  158. if (!transport.query) return;
  159. var cb = this.callback;
  160. var start = Date.now() - (100 * 1000);
  161. var end = Date.now() + (100 * 1000);
  162. logger.query({ from: start, until: end }, cb);
  163. },
  164. 'should return matching results': function (err, results) {
  165. if (!transport.query) return;
  166. assert.isNull(err);
  167. results = results[transport.name];
  168. while (!Array.isArray(results)) {
  169. results = results[Object.keys(results).pop()];
  170. }
  171. assert.ok(results.length >= 1);
  172. }
  173. },
  174. 'using a bad `from` and `until` option': {
  175. 'topic': function () {
  176. if (!transport.query) return;
  177. var cb = this.callback;
  178. logger.log('info', 'bad from and until', {}, function () {
  179. var now = Date.now() + 1000000;
  180. logger.query({ from: now, until: now }, cb);
  181. });
  182. },
  183. 'should return no results': function (err, results) {
  184. if (!transport.query) return;
  185. assert.isNull(err);
  186. results = results[transport.name];
  187. while (!Array.isArray(results)) {
  188. results = results[Object.keys(results).pop()];
  189. }
  190. results = [results.filter(function(log) {
  191. return log.message === 'bad from and until';
  192. }).pop()];
  193. assert.isUndefined(results[0]);
  194. }
  195. }
  196. }
  197. }
  198. };
  199. return out;
  200. };