Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan lavon committed Nov 10, 2015
1 parent 85be07a commit 21f9fc8
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
npm-debug.log
98 changes: 49 additions & 49 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

var log4js = require('log4js');

var levelMap = {};

// Check whether the given level limit was reached - returns whether to log (true = not reached limit).
// Also increases the current number of log for the given level
function checkLimit(limit) {
Expand All @@ -14,55 +12,57 @@ function checkLimit(limit) {
return false;
}

// These functions correspond to the names of the levels, to enable this code: checks["INFO"]()
// Each function checks the limit of its level, and increases its current number, as well as for all the lower levels.
// This way, each debug message raises the number of the debug limit, but each error message raises the limit for error, warn, info, debug, trace.
var checks = {};
checks.TRACE = function () {
var limit = levelMap[log4js.levels.TRACE.level];
return checkLimit(limit);
};
checks.DEBUG = function () {
var limit = levelMap[log4js.levels.DEBUG.level];
checks.TRACE();
return checkLimit(limit);
};
checks.INFO = function () {
var limit = levelMap[log4js.levels.INFO.level];
checks.DEBUG();
return checkLimit(limit);
};
checks.WARN = function () {
var limit = levelMap[log4js.levels.WARN.level];
checks.INFO();
return checkLimit(limit);
};
checks.ERROR = function () {
var limit = levelMap[log4js.levels.ERROR.level];
checks.WARN();
return checkLimit(limit);
};
checks.FATAL = function () {
var limit = levelMap[log4js.levels.FATAL.level];
checks.ERROR();
return checkLimit(limit);
};
checks.MARK = function () {
var limit = levelMap[log4js.levels.MARK.level];
checks.FATAL();
return checkLimit(limit);
};

// Initialize log level limits
['Trace', 'Debug', 'Info', 'Warn', 'Error', 'Fatal', 'Mark'].forEach(
function (levelString) {
var level = log4js.levels.toLevel(levelString);
levelMap[level.level] = { limit: -1, current: 0 };
}
);

// Create filter appender
function rateFilter(limits, interval, appender) {
var levelMap = {};

// These functions correspond to the names of the levels, to enable this code: checks["INFO"]()
// Each function checks the limit of its level, and increases its current number, as well as for all the lower levels.
// This way, each debug message raises the number of the debug limit, but each error message raises the limit for error, warn, info, debug, trace.
var checks = {};
checks.TRACE = function () {
var limit = levelMap[log4js.levels.TRACE.level];
return checkLimit(limit);
};
checks.DEBUG = function () {
var limit = levelMap[log4js.levels.DEBUG.level];
checks.TRACE();
return checkLimit(limit);
};
checks.INFO = function () {
var limit = levelMap[log4js.levels.INFO.level];
checks.DEBUG();
return checkLimit(limit);
};
checks.WARN = function () {
var limit = levelMap[log4js.levels.WARN.level];
checks.INFO();
return checkLimit(limit);
};
checks.ERROR = function () {
var limit = levelMap[log4js.levels.ERROR.level];
checks.WARN();
return checkLimit(limit);
};
checks.FATAL = function () {
var limit = levelMap[log4js.levels.FATAL.level];
checks.ERROR();
return checkLimit(limit);
};
checks.MARK = function () {
var limit = levelMap[log4js.levels.MARK.level];
checks.FATAL();
return checkLimit(limit);
};

// Initialize log level limits
['Trace', 'Debug', 'Info', 'Warn', 'Error', 'Fatal', 'Mark'].forEach(
function (levelString) {
var level = log4js.levels.toLevel(levelString);
levelMap[level.level] = { limit: -1, current: 0 };
}
);

if (limits) {
// Read limits from config - each level can define its own limit, but this is accumalated:
// If debug is 10 and error is 10, the limits are 10,10,10,20,20.
Expand Down
93 changes: 93 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var log4js = require('log4js');
var assert = require("assert");

describe('test filter', function(){

it ('should limit to 1 log', function(){
var logEvents = [];
log4js.clearAppenders();
var appender = require('../lib/filter')
.appender({TRACE:1},
1000,
function(evt) { logEvents.push(evt); }
);
log4js.addAppender(appender, ["app"]);

var logger = log4js.getLogger("app");
logger.info('hey');
logger.info('hey');
logger.info('hey');

assert.equal(logEvents.length,1, 'Did not limit calls');
});

it ('should limit to 1 error log', function(){
var logEvents = [];
log4js.clearAppenders();
var appender = require('../lib/filter')
.appender({
TRACE:1,
ERROR:1
},
500,
function(evt) { logEvents.push(evt); }
);
log4js.addAppender(appender, ["app"]);

var logger = log4js.getLogger("app");
logger.error('hey');
logger.info('hey');
logger.trace('hey');

assert.equal(logEvents.length,1, 'Did not limit calls');
assert.equal(logEvents[0].level.level, log4js.levels.ERROR.level, 'Not correct error');
});

it ('should limit to 2 logs', function(){
var logEvents = [];
log4js.clearAppenders();
var appender = require('../lib/filter')
.appender({
TRACE:1,
ERROR:1
},
500,
function(evt) { logEvents.push(evt); }
);
log4js.addAppender(appender, ["app"]);

var logger = log4js.getLogger("app");
logger.info('hey');
logger.error('hey');
logger.trace('hey');

assert.equal(logEvents.length,2, 'Did not limit calls');
assert.equal(logEvents[0].level.level, log4js.levels.INFO.level, 'Not correct error');
assert.equal(logEvents[1].level.level, log4js.levels.ERROR.level, 'Not correct error');
});

it ('should limit to 2 logs, with time limit', function(done){
var logEvents = [];
log4js.clearAppenders();
var appender = require('../lib/filter')
.appender({TRACE:1},
500,
function(evt) { logEvents.push(evt); }
);
log4js.addAppender(appender, ["app"]);

var logger = log4js.getLogger("app");
logger.info('hey');
logger.info('hey');
logger.info('hey');

assert.equal(logEvents.length,1, 'Did not limit calls');
setTimeout(function(){
logger.info('hey');
logger.info('hey');
logger.info('hey');
assert.equal(logEvents.length,2, 'Did not limit calls');
done();
}, 800);
});
});

0 comments on commit 21f9fc8

Please sign in to comment.