diff --git a/lib/common/globals.js b/lib/common/globals.js index 599ac33..f6221f0 100644 --- a/lib/common/globals.js +++ b/lib/common/globals.js @@ -8,99 +8,6 @@ * Some rights reserved. See LICENSE, AUTHORS. **/ - -var - os = require('os'), - cluster = require('cluster'); - -global.stopped = false; -global.shutdown = function (code, callback) { - if (stopped) - return; - - stopped = true; - joola.logger.info('Gracefully shutting down, code: ' + code); - joola.state.set('core', 'stop', 'received code [' + code + ']+'); - - joola.dispatch.emit('nodes:state:change', [joola.UID, joola.state.get()]); - - var node = nodeState(); - if (node.http) { - //We're running an http node, let's try to start another one. - joola.logger.info('Trying to start Web Services on another node'); - joola.dispatch.request(0, 'startWebServer', [node], function () { - //do we care? - }); - } - joola.redis.del('nodes:' + joola.UID, function () { - //do we care? - }); - - setTimeout(function () { - process.exit(code || 0); - }, 10); - - if (typeof callback === 'function') - return callback(null); -}; - -global.emptyfunc = function () { - -}; - -global.rt = function () { - joola.dispatch.roundtrip(function (err, delta) { - if (err) - return console.log('Roundtrip: ' + err); - return console.log('Roundtrip: ' + delta + 'ms'); - }); -}; - -global.nodeState = function () { - - var hostname = joola.hostname = os.hostname(); - var isCluster = false; //Object.keys(cluster.workers).length > 0; - var clusterID = null;// = cluster.worker ? cluster.worker.id : null; - var isMaster = null;// = Object.keys(cluster.workers).length > 0 ? cluster.isMaster : null; - var isWorker = null;// = Object.keys(cluster.workers).length > 0 ? cluster.isWorker : null; - - try { - if (typeof cluster.workers === 'object') { - if (cluster.workers === null) { - isCluster = true; - clusterID = cluster.worker ? cluster.worker.id : null; - isMaster = cluster.isMaster; - isWorker = cluster.isWorker; - } - } - } - catch (ex) { - //ignore errors - } - - var state = joola.state.get(); - var status = state.status; - - var result = { - uid: joola.UID, - state: status, - uptime: process.uptime(), - 'last-seen': new Date().getTime(), - 'last-seen-nice': new Date(), - hostname: hostname, - 'usage-cpu': null, - 'usage-mem': null, - http: joola.webserver.http._handle ? true : false, - https: joola.webserver.https._handle ? true : false, - cluster: isCluster, - 'cluster-id': clusterID, - 'cluster-master': isMaster, - 'cluster-slave': isWorker - }; - - return result; -}; - joolaio.timezone = function (tz) { if (tz) joolaio.options.timezoneOffset = tz; diff --git a/lib/common/logger.js b/lib/common/logger.js index c7cd8dc..25430aa 100644 --- a/lib/common/logger.js +++ b/lib/common/logger.js @@ -12,83 +12,57 @@ var logger = exports; logger._id = 'logger'; -if (process.env.NODE_ENV == 'test') { - logger = module.exports = { - log: function () { - }, - silly: function () { - - }, - debug: function () { - - }, - info: function () { - - }, - error: function () { - - }, - notice: function () { - - }, - setLevel: function () { - - } - }; -} -else { - logger._log = function (level, message, callback) { - switch (level) { - case 'debug': - case 'info': - case 'warn': - case 'error': - break; - case 'silly': - level = 'debug'; - break; - default: - break; - } - - if (!joolaio.options.debug.enabled) - return; - - if (typeof message === 'object') - message = '[' + new Date().format('hh:nn:ss.fff') + '] ' + JSON.stringify(message); - else - message = '[' + new Date().format('hh:nn:ss.fff') + '] ' + message; - - if (joolaio.options.isBrowser && console.debug) { - if (['silly', 'debug'].indexOf(level) == -1) - console[level](message); - else if (joolaio.options.debug.enabled && ['silly', 'debug'].indexOf(level) > -1) - console[level](message); - } - else - console.log(message); - - if (callback) - return callback(null); - }; - - logger.silly = function (message, callback) { - return this._log('silly', message, callback); - }; - - logger.info = function (message, callback) { - return this._log('info', message, callback); - }; - - logger.debug = function (message, callback) { - return this._log('debug', message, callback); - }; - - logger.warn = function (message, callback) { - return this._log('warn', message, callback); - }; - - logger.error = function (message, callback) { - return this._log('error', message, callback); - }; -} \ No newline at end of file +logger._log = function (level, message, callback) { + switch (level) { + case 'debug': + case 'info': + case 'warn': + case 'error': + break; + case 'silly': + level = 'debug'; + break; + default: + break; + } + + if (!joolaio.options.debug.enabled) + return; + + if (typeof message === 'object') + message = '[' + new Date().format('hh:nn:ss.fff') + '] ' + JSON.stringify(message); + else + message = '[' + new Date().format('hh:nn:ss.fff') + '] ' + message; + + if (joolaio.options.isBrowser && console.debug) { + if (['silly', 'debug'].indexOf(level) == -1) + console[level](message); + else if (joolaio.options.debug.enabled && ['silly', 'debug'].indexOf(level) > -1) + console[level](message); + } + else + console.log(message); + + if (callback) + return callback(null); +}; + +logger.silly = function (message, callback) { + return this._log('silly', message, callback); +}; + +logger.info = function (message, callback) { + return this._log('info', message, callback); +}; + +logger.debug = function (message, callback) { + return this._log('debug', message, callback); +}; + +logger.warn = function (message, callback) { + return this._log('warn', message, callback); +}; + +logger.error = function (message, callback) { + return this._log('error', message, callback); +}; diff --git a/test/unit/2_common/logger.spec.js b/test/unit/2_common/logger.spec.js new file mode 100644 index 0000000..b1142d3 --- /dev/null +++ b/test/unit/2_common/logger.spec.js @@ -0,0 +1,36 @@ +describe("logger", function () { + before(function () { + this._enabled = joolaio.options.debug.enabled; + joolaio.options.debug.enabled = true; + }); + + it("should output log message [silly]", function () { + joolaio.logger.silly('test log message'); + }); + + it("should output log message [debug]", function () { + joolaio.logger.debug('test log message'); + }); + + it("should output log message [info]", function () { + joolaio.logger.info('test log message'); + }); + + it("should output log message [warn]", function () { + joolaio.logger.warn('test log message'); + }); + + it("should output log message [error]", function () { + joolaio.options.isBrowser = true; //check also other routes for coverage. + console.debug = console.log; + joolaio.logger.error('test log message'); + }); + + it("should output log message [json]", function () { + joolaio.logger.silly({test: 'log message'}); + }); + + after(function () { + joolaio.options.debug.enabled = this._enabled; + }); +}); \ No newline at end of file