console-test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * console-test.js: Tests for instances of the Console transport
  3. *
  4. * (C) 2010 Charlie Robbins
  5. * MIT LICENSE
  6. *
  7. */
  8. var path = require('path'),
  9. vows = require('vows'),
  10. assert = require('assert'),
  11. winston = require('../../lib/winston'),
  12. helpers = require('../helpers'),
  13. stdMocks = require('std-mocks');
  14. var npmTransport = new (winston.transports.Console)(),
  15. syslogTransport = new (winston.transports.Console)({ levels: winston.config.syslog.levels }),
  16. alignTransport = new (winston.transports.Console)({ showLevel: true, align: true }),
  17. defaultTransport = new (winston.transports.Console)(),
  18. rawTransport = new (winston.transports.Console)({ level: 'verbose', raw: true }),
  19. debugStdoutTransport = new (winston.transports.Console)({ debugStdout: true }),
  20. stderrLevelsTransport = new (winston.transports.Console)({ stderrLevels: ['info', 'warn'] }),
  21. customLevels = {
  22. alpha: 0,
  23. beta: 1,
  24. gamma: 2,
  25. delta: 3,
  26. epsilon: 4,
  27. },
  28. customLevelsAndStderrTransport = new (winston.transports.Console)({
  29. levels: customLevels,
  30. stderrLevels: ['delta', 'epsilon']
  31. }),
  32. noStderrTransport = new (winston.transports.Console)({ stderrLevels: [] });
  33. vows.describe('winston/transports/console').addBatch({
  34. "An instance of the Console Transport": {
  35. "with showLevel off": {
  36. topic : function() {
  37. npmTransport.showLevel = false;
  38. stdMocks.use();
  39. npmTransport.log('info', 'Le message', { meta: true }, this.callback);
  40. },
  41. "should not have level prepended": function () {
  42. stdMocks.restore();
  43. var output = stdMocks.flush(),
  44. line = output.stdout[0];
  45. assert.equal(line, 'Le message meta=true\n');
  46. }
  47. }
  48. }
  49. }).addBatch({
  50. "An instance of the Console Transport": {
  51. "with showLevel on": {
  52. topic : function() {
  53. npmTransport.showLevel = true;
  54. stdMocks.use();
  55. npmTransport.log('info', '');
  56. },
  57. "should have level prepended": function () {
  58. stdMocks.restore();
  59. var output = stdMocks.flush(),
  60. line = output.stdout[0];
  61. assert.equal(line, 'info: \n');
  62. }
  63. },
  64. }
  65. }).addBatch({
  66. "An instance of the Console Transport": {
  67. "with npm levels": {
  68. "should have the proper methods defined": function () {
  69. helpers.assertConsole(npmTransport);
  70. },
  71. "the log() method": helpers.testNpmLevels(npmTransport, "should respond with true", function (ign, err, logged) {
  72. assert.isNull(err);
  73. assert.isTrue(logged);
  74. })
  75. },
  76. "with syslog levels": {
  77. "should have the proper methods defined": function () {
  78. helpers.assertConsole(syslogTransport);
  79. },
  80. "the log() method": helpers.testSyslogLevels(syslogTransport, "should respond with true", function (ign, err, logged) {
  81. assert.isNull(err);
  82. assert.isTrue(logged);
  83. })
  84. },
  85. "with end-of-line": {
  86. topic : function() {
  87. npmTransport.eol = 'X';
  88. stdMocks.use();
  89. npmTransport.log('info', 'Le message', { meta: true }, this.callback);
  90. },
  91. "should have end-of-line character appended": function () {
  92. stdMocks.restore();
  93. var output = stdMocks.flush(),
  94. line = output.stdout[0];
  95. console.dir(line);
  96. assert.equal(line, 'info: Le message meta=trueX');
  97. }
  98. }
  99. }
  100. }).addBatch({
  101. "An instance of the Console Transport with the align option on": {
  102. topic : function() {
  103. stdMocks.use();
  104. alignTransport.log('info', '');
  105. },
  106. "should have logs aligned": function () {
  107. stdMocks.restore();
  108. var output = stdMocks.flush(),
  109. line = output.stdout[0];
  110. assert.equal(line, 'info\011: \n');
  111. }
  112. }
  113. }).addBatch({
  114. "with align off": {
  115. topic : function() {
  116. alignTransport.align = false;
  117. stdMocks.use();
  118. alignTransport.log('info', '');
  119. },
  120. "should not have logs aligned": function () {
  121. stdMocks.restore();
  122. var output = stdMocks.flush(),
  123. line = output.stdout[0];
  124. assert.equal(line, 'info: \n');
  125. }
  126. }
  127. }).addBatch({
  128. 'An instance of a raw Console transport': {
  129. 'logging to stdout': {
  130. topic: function () {
  131. stdMocks.use();
  132. rawTransport.log('verbose', 'hello there');
  133. }, 'should output json with message property': function () {
  134. stdMocks.restore();
  135. var output = stdMocks.flush();
  136. assert.ok(output.stdout[0].indexOf('"message":"hello there"') > -1);
  137. }
  138. }
  139. }
  140. }).addBatch({
  141. "An instance of the Console Transport with no options": {
  142. "should set stderrLevels to 'error' and 'debug' by default": helpers.assertStderrLevels(
  143. defaultTransport,
  144. ['error', 'debug']
  145. ),
  146. "should log only 'error' and 'debug' to stderr": helpers.testLoggingToStreams(
  147. winston.config.npm.levels, defaultTransport, ['debug', 'error'], stdMocks
  148. )
  149. }
  150. }).addBatch({
  151. "An instance of the Console Transport with debugStdout set": {
  152. "should throw an Error if stderrLevels is set": helpers.assertOptionsThrow(
  153. { debugStdout: true, stderrLevels: ['debug'] },
  154. "Error: Cannot set debugStdout and stderrLevels together"
  155. ),
  156. "should set stderrLevels to 'error' by default": helpers.assertStderrLevels(
  157. debugStdoutTransport,
  158. ['error']
  159. ),
  160. "should log only the 'error' level to stderr": helpers.testLoggingToStreams(
  161. winston.config.npm.levels, debugStdoutTransport, ['error'], stdMocks
  162. )
  163. }
  164. }).addBatch({
  165. "An instance of the Console Transport with stderrLevels set": {
  166. "should throw an Error if stderrLevels is set but not an Array": helpers.assertOptionsThrow(
  167. { debugStdout: false, stderrLevels: new String('Not an Array') },
  168. "Error: Cannot set stderrLevels to type other than Array"
  169. ),
  170. "should throw an Error if stderrLevels contains non-string elements": helpers.assertOptionsThrow(
  171. { debugStdout: false, stderrLevels: ["good", /^invalid$/, "valid"] },
  172. "Error: Cannot have non-string elements in stderrLevels Array"
  173. ),
  174. "should correctly set stderrLevels": helpers.assertStderrLevels(
  175. stderrLevelsTransport,
  176. ['info', 'warn']
  177. ),
  178. "should log only the levels in stderrLevels to stderr": helpers.testLoggingToStreams(
  179. winston.config.npm.levels, stderrLevelsTransport, ['info', 'warn'], stdMocks
  180. )
  181. }
  182. }).addBatch({
  183. "An instance of the Console Transport with stderrLevels set to an empty array": {
  184. "should log only to stdout, and not to stderr": helpers.testLoggingToStreams(
  185. winston.config.npm.levels, noStderrTransport, [], stdMocks
  186. )
  187. }
  188. }).addBatch({
  189. "An instance of the Console Transport with custom levels and stderrLevels set": {
  190. "should log only the levels in stderrLevels to stderr": helpers.testLoggingToStreams(
  191. customLevels, customLevelsAndStderrTransport, ['delta', 'epsilon'], stdMocks
  192. )
  193. }
  194. }).export(module);