configureNoLevels-test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. // This test shows unexpected behaviour for log4js.configure() in log4js-node@0.4.3 and earlier:
  3. // 1) log4js.configure(), log4js.configure(null),
  4. // log4js.configure({}), log4js.configure(<some object with no levels prop>)
  5. // all set all loggers levels to trace, even if they were previously set to something else.
  6. // 2) log4js.configure({levels:{}}), log4js.configure({levels: {foo:
  7. // bar}}) leaves previously set logger levels intact.
  8. //
  9. // Basic set up
  10. var vows = require('vows');
  11. var assert = require('assert');
  12. var toLevel = require('../lib/levels').toLevel;
  13. // uncomment one or other of the following to see progress (or not) while running the tests
  14. // var showProgress = console.log;
  15. var showProgress = function() {};
  16. // Define the array of levels as string to iterate over.
  17. var strLevels= ['Trace','Debug','Info','Warn','Error','Fatal'];
  18. // setup the configurations we want to test
  19. var configs = {
  20. 'nop': 'nop', // special case where the iterating vows generator will not call log4js.configure
  21. 'is undefined': undefined,
  22. 'is null': null,
  23. 'is empty': {},
  24. 'has no levels': {foo: 'bar'},
  25. 'has null levels': {levels: null},
  26. 'has empty levels': {levels: {}},
  27. 'has random levels': {levels: {foo: 'bar'}},
  28. 'has some valid levels': {levels: {A: 'INFO'}}
  29. };
  30. // Set up the basic vows batches for this test
  31. var batches = [];
  32. function getLoggerName(level) {
  33. return level+'-logger';
  34. }
  35. // the common vows top-level context, whether log4js.configure is called or not
  36. // just making sure that the code is common,
  37. // so that there are no spurious errors in the tests themselves.
  38. function getTopLevelContext(nop, configToTest, name) {
  39. return {
  40. topic: function() {
  41. var log4js = require('../lib/log4js');
  42. // create loggers for each level,
  43. // keeping the level in the logger's name for traceability
  44. strLevels.forEach(function(l) {
  45. log4js.getLogger(getLoggerName(l)).setLevel(l);
  46. });
  47. if (!nop) {
  48. showProgress('** Configuring log4js with', configToTest);
  49. log4js.configure(configToTest);
  50. }
  51. else {
  52. showProgress('** Not configuring log4js');
  53. }
  54. return log4js;
  55. }
  56. };
  57. }
  58. showProgress('Populating batch object...');
  59. function checkForMismatch(topic) {
  60. var er = topic.log4js.levels.toLevel(topic.baseLevel)
  61. .isLessThanOrEqualTo(topic.log4js.levels.toLevel(topic.comparisonLevel));
  62. assert.equal(
  63. er,
  64. topic.expectedResult,
  65. 'Mismatch: for setLevel(' + topic.baseLevel +
  66. ') was expecting a comparison with ' + topic.comparisonLevel +
  67. ' to be ' + topic.expectedResult
  68. );
  69. }
  70. function checkExpectedResult(topic) {
  71. var result = topic.log4js
  72. .getLogger(getLoggerName(topic.baseLevel))
  73. .isLevelEnabled(topic.log4js.levels.toLevel(topic.comparisonLevel));
  74. assert.equal(
  75. result,
  76. topic.expectedResult,
  77. 'Failed: ' + getLoggerName(topic.baseLevel) +
  78. '.isLevelEnabled( ' + topic.comparisonLevel + ' ) returned ' + result
  79. );
  80. }
  81. function setupBaseLevelAndCompareToOtherLevels(baseLevel) {
  82. var baseLevelSubContext = 'and checking the logger whose level was set to '+baseLevel ;
  83. var subContext = { topic: baseLevel };
  84. batch[context][baseLevelSubContext] = subContext;
  85. // each logging level has strLevels sub-contexts,
  86. // to exhaustively test all the combinations of
  87. // setLevel(baseLevel) and isLevelEnabled(comparisonLevel) per config
  88. strLevels.forEach(compareToOtherLevels(subContext));
  89. }
  90. function compareToOtherLevels(subContext) {
  91. var baseLevel = subContext.topic;
  92. return function (comparisonLevel) {
  93. var comparisonLevelSubContext = 'with isLevelEnabled('+comparisonLevel+')';
  94. // calculate this independently of log4js, but we'll add a vow
  95. // later on to check that we're not mismatched with log4js
  96. var expectedResult = strLevels.indexOf(baseLevel) <= strLevels.indexOf(comparisonLevel);
  97. // the topic simply gathers all the parameters for the vow
  98. // into an object, to simplify the vow's work.
  99. subContext[comparisonLevelSubContext] = {
  100. topic: function(baseLevel, log4js) {
  101. return {
  102. comparisonLevel: comparisonLevel,
  103. baseLevel: baseLevel,
  104. log4js: log4js,
  105. expectedResult: expectedResult
  106. };
  107. }
  108. };
  109. var vow = 'should return '+expectedResult;
  110. subContext[comparisonLevelSubContext][vow] = checkExpectedResult;
  111. // the extra vow to check the comparison between baseLevel and
  112. // comparisonLevel we performed earlier matches log4js'
  113. // comparison too
  114. var subSubContext = subContext[comparisonLevelSubContext];
  115. subSubContext['finally checking for comparison mismatch with log4js'] = checkForMismatch;
  116. };
  117. }
  118. // Populating the batches programmatically, as there are
  119. // (configs.length x strLevels.length x strLevels.length) = 324
  120. // possible test combinations
  121. for (var cfg in configs) {
  122. var configToTest = configs[cfg];
  123. var nop = configToTest === 'nop';
  124. var context;
  125. if (nop) {
  126. context = 'Setting up loggers with initial levels, then NOT setting a configuration,';
  127. }
  128. else {
  129. context = 'Setting up loggers with initial levels, then setting a configuration which '+cfg+',';
  130. }
  131. showProgress('Setting up the vows batch and context for '+context);
  132. // each config to be tested has its own vows batch with a single top-level context
  133. var batch={};
  134. batch[context]= getTopLevelContext(nop, configToTest, context);
  135. batches.push(batch);
  136. // each top-level context has strLevels sub-contexts, one per logger
  137. // which has set to a specific level in the top-level context's topic
  138. strLevels.forEach(setupBaseLevelAndCompareToOtherLevels);
  139. }
  140. showProgress('Running tests');
  141. var v = vows.describe('log4js.configure(), with or without a "levels" property');
  142. batches.forEach(function(batch) {v=v.addBatch(batch);});
  143. v.export(module);