logLevelFilter-test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. var vows = require('vows')
  3. , fs = require('fs')
  4. , assert = require('assert');
  5. function remove(filename) {
  6. try {
  7. fs.unlinkSync(filename);
  8. } catch (e) {
  9. //doesn't really matter if it failed
  10. }
  11. }
  12. vows.describe('log4js logLevelFilter').addBatch({
  13. 'appender': {
  14. topic: function() {
  15. var log4js = require('../lib/log4js'), logEvents = [], logger;
  16. log4js.clearAppenders();
  17. log4js.addAppender(
  18. require('../lib/appenders/logLevelFilter')
  19. .appender(
  20. 'ERROR',
  21. function(evt) { logEvents.push(evt); }
  22. ),
  23. "logLevelTest"
  24. );
  25. logger = log4js.getLogger("logLevelTest");
  26. logger.debug('this should not trigger an event');
  27. logger.warn('neither should this');
  28. logger.error('this should, though');
  29. logger.fatal('so should this');
  30. return logEvents;
  31. },
  32. 'should only pass log events greater than or equal to its own level' : function(logEvents) {
  33. assert.equal(logEvents.length, 2);
  34. assert.equal(logEvents[0].data[0], 'this should, though');
  35. assert.equal(logEvents[1].data[0], 'so should this');
  36. }
  37. },
  38. 'configure': {
  39. topic: function() {
  40. var log4js = require('../lib/log4js')
  41. , logger;
  42. remove(__dirname + '/logLevelFilter.log');
  43. remove(__dirname + '/logLevelFilter-warnings.log');
  44. log4js.configure('test/with-logLevelFilter.json');
  45. logger = log4js.getLogger("tests");
  46. logger.info('main');
  47. logger.error('both');
  48. logger.warn('both');
  49. logger.debug('main');
  50. //wait for the file system to catch up
  51. setTimeout(this.callback, 100);
  52. },
  53. 'tmp-tests.log': {
  54. topic: function() {
  55. fs.readFile(__dirname + '/logLevelFilter.log', 'utf8', this.callback);
  56. },
  57. 'should contain all log messages': function(contents) {
  58. var messages = contents.trim().split('\n');
  59. assert.deepEqual(messages, ['main','both','both','main']);
  60. }
  61. },
  62. 'tmp-tests-warnings.log': {
  63. topic: function() {
  64. fs.readFile(__dirname + '/logLevelFilter-warnings.log','utf8',this.callback);
  65. },
  66. 'should contain only error and warning log messages': function(contents) {
  67. var messages = contents.trim().split('\n');
  68. assert.deepEqual(messages, ['both','both']);
  69. }
  70. }
  71. }
  72. }).export(module);