From 21f9fc82e1fcfc396e388f389017398104f88ade Mon Sep 17 00:00:00 2001 From: jonathan lavon Date: Tue, 10 Nov 2015 16:56:14 +0200 Subject: [PATCH] Added tests --- .gitignore | 2 ++ lib/filter.js | 98 +++++++++++++++++++++++++-------------------------- test/test.js | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 49 deletions(-) create mode 100644 .gitignore create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ca9571 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +npm-debug.log diff --git a/lib/filter.js b/lib/filter.js index 5b1114f..cb4d178 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -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) { @@ -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. diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..95a63c4 --- /dev/null +++ b/test/test.js @@ -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); + }); +}); \ No newline at end of file