Skip to content

Commit

Permalink
FH-2708 - configurable log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
odra committed Aug 12, 2016
1 parent c898dab commit f4be7ea
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = function(grunt) {
'./test/test_sync.js',
/*'./test/test_web.js',*/
'./test/test_fhauth.js',
'./test/test_init.js'
'./test/test_init.js',
'./test/test_log.js'
];
var unit_args = _.map(tests, makeTestArgs);
var test_runner = '_mocha';
Expand Down
8 changes: 7 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ function FHapi(cfg, logr) {
}
});

api.setLogLevel = function(level) {
logr.setLogLevel(level);
};

return api;
}

Expand Down Expand Up @@ -125,6 +129,8 @@ module.exports = (function () {

var mbaas_url = url.parse(mbaas_protocol + "://" + mbaas_host);

var logLevel = parseInt(process.env.MBAAS_LOG_LEVEL) || 2;

try {
ua = JSON.parse(ua);
} catch (x) {
Expand Down Expand Up @@ -193,7 +199,7 @@ module.exports = (function () {
//Initialising MbaasConfig To Make Forms Calls To FH-Mbaas
mbaasClient.initEnvironment(cfg.fhmbaas.environment, cfg.fhmbaas.mbaasConf);

var logger = new consolelogger.ConsoleLogger(3);
var logger = new consolelogger.ConsoleLogger(logLevel);
initScript(logger);
return new FHapi(cfg, logger);
})();
14 changes: 13 additions & 1 deletion lib/consolelogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
//

// 0 error, 1 warning, 2 info, 3 debug
var loglevel = 2;
var LEVEL = {
NONE: -1,
ERROR: 0,
WARN: 1,
INFO: 2,
DEBUG: 3
};
var loglevel = LEVEL.INFO;

function ConsoleLogger(level) {
if (undefined !== level) {
Expand All @@ -23,6 +30,10 @@ function getTS() {
" " + hour + ":" + min + ":" + sec;
}

ConsoleLogger.prototype.setLogLevel = function(level) {
loglevel = level;
};

ConsoleLogger.prototype.error = function (data) {
if (loglevel >= 0) {
console.log(getTS() + " ERROR " + data);
Expand All @@ -48,3 +59,4 @@ ConsoleLogger.prototype.debug = function (data) {
};

exports.ConsoleLogger = ConsoleLogger;
exports.LEVEL = LEVEL;
20 changes: 10 additions & 10 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fh-mbaas-api",
"version": "5.13.7",
"version": "5.13.8",
"description": "FeedHenry MBAAS Cloud APIs",
"main": "lib/api.js",
"dependencies": {
Expand All @@ -27,17 +27,18 @@
"winston": "0.8.0"
},
"devDependencies": {
"chai": "^3.5.0",
"express": "3.3.4",
"grunt": "^0.4.5",
"grunt-fh-build": "^0.5.0",
"istanbul": "0.2.14",
"jshint": "^2.5.2",
"mocha": "2.4.5",
"mocha-sinon": "^1.1.5",
"nock": "0.22.1",
"plato": "^1.0.1",
"proxyquire": "1.4.0",
"mocha": "2.4.5",
"valid-url": "1.0.9",
"sinon": "1.17.1 "
"valid-url": "1.0.9"
},
"scripts": {
"testwindows": "test_win.cmd",
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sonar.projectKey=fh-mbaas-api
sonar.projectName=fh-mbaas-api-nightly-master
sonar.projectVersion=5.13.7
sonar.projectVersion=5.13.8

sonar.sources=./lib
sonar.tests=./test
Expand Down
81 changes: 81 additions & 0 deletions test/test_log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
var sinon = require('sinon');
var consolelogger = require('../lib/consolelogger');
var expect = require('chai').expect;
require('mocha-sinon');

var logger = new consolelogger.ConsoleLogger();


function getTS() {
var d = new Date();
var day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
var mon = d.getMonth() < 10 ? "0" + d.getMonth() : d.getMonth();
var hour = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
var min = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
var sec = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();

return d.getFullYear() + "-" + mon + "-" + day +
" " + hour + ":" + min + ":" + sec;
}


module.exports = {
beforeEach: function() {
var log = console.log;
this.sinon.stub(console, 'log', function() {
return log.apply(log, arguments);
});
},

'test error log level 0': function(done) {
logger.setLogLevel(consolelogger.LEVEL.ERROR);
logger.error('error');
expect(console.log.calledWith(getTS() + ' ERROR error')).to.be.true;
logger.warning('warning');
expect(console.log.calledWith(getTS() + ' WARN warning')).to.be.false;
logger.info('info');
expect(console.log.calledWith(getTS() + ' INFO info')).to.be.false;
logger.debug('debug');
expect(console.log.calledWith(getTS() + ' DEBUG debug')).to.be.false;
done();
},

'test error log level 1': function(done) {
logger.setLogLevel(consolelogger.LEVEL.WARN);
logger.error('error');
expect(console.log.calledWith(getTS() + ' ERROR error')).to.be.true;
logger.warning('warning');
expect(console.log.calledWith(getTS() + ' WARN warning')).to.be.true;
logger.info('info');
expect(console.log.calledWith(getTS() + ' INFO info')).to.be.false;
logger.debug('debug');
expect(console.log.calledWith(getTS() + ' DEBUG debug')).to.be.false;
done();
},

'test error log level 2': function(done) {
logger.setLogLevel(consolelogger.LEVEL.INFO);
logger.error('error');
expect(console.log.calledWith(getTS() + ' ERROR error')).to.be.true;
logger.warning('warning');
expect(console.log.calledWith(getTS() + ' WARN warning')).to.be.true;
logger.info('info');
expect(console.log.calledWith(getTS() + ' INFO info')).to.be.true;
logger.debug('debug');
expect(console.log.calledWith(getTS() + ' DEBUG warning')).to.be.false;
done();
},

'test error log level 3': function(done) {
logger.setLogLevel(consolelogger.LEVEL.DEBUG);
logger.error('error');
expect(console.log.calledWith(getTS() + ' ERROR error')).to.be.true;
logger.warning('warning');
expect(console.log.calledWith(getTS() + ' WARN warning')).to.be.true;
logger.info('info');
expect(console.log.calledWith(getTS() + ' INFO info')).to.be.true;
logger.debug('debug');
expect(console.log.calledWith(getTS() + ' DEBUG debug')).to.be.true;
done();
}
};
2 changes: 1 addition & 1 deletion test_win.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
REM use the command "npm run testwindows" to run this test script, that will use the correct path for turbo.cmd
REM if updating the list of tests, please also ensure that it's reflected in the Gruntfile.js list also
REM
turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhutils.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhact.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhfeed.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhforms.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhsec.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhsession.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhstat.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_cache.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_redis.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_sync.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhauth.js
turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhutils.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhact.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhfeed.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhforms.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhsec.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhsession.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhstat.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_cache.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_redis.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_sync.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_fhauth.js && turbo.cmd --setUp ./test/setup.js --tearDown ./test/setup.js ./test/test_log.js

0 comments on commit f4be7ea

Please sign in to comment.