Move Logger configuration into Cylon#config

This commit is contained in:
Andrew Stewart 2014-10-28 15:53:03 -07:00
parent 903f8a9518
commit 0f59645fd7
6 changed files with 62 additions and 11 deletions

View File

@ -8,4 +8,6 @@
'use strict'; 'use strict';
module.exports = {}; module.exports = {
logging: {}
};

View File

@ -95,12 +95,18 @@ Cylon.start = function start() {
// //
// Returns the current config // Returns the current config
Cylon.config = function(opts) { Cylon.config = function(opts) {
var loggingChanged = (opts.logging && Config.logging !== opts.logging);
if (opts && typeof(opts) === 'object' && !Array.isArray(opts)) { if (opts && typeof(opts) === 'object' && !Array.isArray(opts)) {
for (var o in opts) { for (var o in opts) {
Config[o] = opts[o]; Config[o] = opts[o];
} }
} }
if (loggingChanged) {
Logger.setup();
}
return Config; return Config;
}; };

View File

@ -11,7 +11,8 @@
var levels = ["debug", "info", "warn", "error", "fatal"]; var levels = ["debug", "info", "warn", "error", "fatal"];
var BasicLogger = require('./logger/basic_logger'), var BasicLogger = require('./logger/basic_logger'),
NullLogger = require('./logger/null_logger'); NullLogger = require('./logger/null_logger'),
Config = require('./config');
// The Logger is a global object to facilitate logging stuff to the console (or // The Logger is a global object to facilitate logging stuff to the console (or
// other output) easily and consistently. It's available anywhere in Cylon, as // other output) easily and consistently. It's available anywhere in Cylon, as
@ -27,7 +28,10 @@ var Logger = module.exports = {};
// or above // or above
// //
// Returns the new logger instance // Returns the new logger instance
Logger.setup = function setup(logger, level) { Logger.setup = function setup() {
var logger = Config.logging.logger,
level = Config.logging.level || "info";
if (logger == null) { logger = BasicLogger; } if (logger == null) { logger = BasicLogger; }
this.logger = logger || NullLogger; this.logger = logger || NullLogger;
@ -55,7 +59,7 @@ var setLogLevel = function(level) {
ignored; ignored;
if (index < 0) { if (index < 0) {
throw new Error("Invalid Log Level specified"); throw new Error("Invalid Log Level specified: " + level);
} }
active = levels.slice(index); active = levels.slice(index);

View File

@ -31,6 +31,9 @@ var Cylon = source('cylon');
Cylon.config({ Cylon.config({
mode: "manual", mode: "manual",
logging: {
logger: false
}
}); });
Cylon.Logger.setup(false); Cylon.Logger.setup();

View File

@ -101,6 +101,12 @@ describe("Cylon", function() {
for (var c in Config) { for (var c in Config) {
delete Config[c]; delete Config[c];
} }
stub(Logger, 'setup');
});
afterEach(function() {
Logger.setup.restore();
}); });
it("sets config variables", function() { it("sets config variables", function() {
@ -127,6 +133,14 @@ describe("Cylon", function() {
Cylon.config("hello world"); Cylon.config("hello world");
expect(Config).to.be.eql(config); expect(Config).to.be.eql(config);
}); });
it("updates the Logger setup if that changed", function() {
Cylon.config({ a: 1 });
expect(Logger.setup).to.not.be.called;
Cylon.config({ a: 1, logging: { logger: false } });
expect(Logger.setup).to.be.called;
})
}); });
describe("#halt", function() { describe("#halt", function() {

View File

@ -1,16 +1,20 @@
'use strict'; 'use strict';
var Logger = source('logger'), var Logger = source('logger'),
Config = source('config'),
Utils = source('utils'); Utils = source('utils');
describe('Logger', function() { describe('Logger', function() {
afterEach(function() { afterEach(function() {
Logger.setup(false); // to be friendly to other specs // to be friendly to other specs
Config.logging = { logger: false, level: 'debug' };
Logger.setup();
}); });
describe("#setup", function() { describe("#setup", function() {
context("with no arguments", function() { context("with no arguments", function() {
it("sets up a BasicLogger", function() { it("sets up a BasicLogger", function() {
Config.logging = {};
Logger.setup(); Logger.setup();
expect(Logger.toString()).to.be.eql("BasicLogger"); expect(Logger.toString()).to.be.eql("BasicLogger");
}); });
@ -18,7 +22,8 @@ describe('Logger', function() {
context("with false", function() { context("with false", function() {
it("sets up a NullLogger", function() { it("sets up a NullLogger", function() {
Logger.setup(false); Config.logging = { logger: false };
Logger.setup();
expect(Logger.toString()).to.be.eql("NullLogger"); expect(Logger.toString()).to.be.eql("NullLogger");
}); });
}); });
@ -26,7 +31,8 @@ describe('Logger', function() {
context("with a custom logger", function() { context("with a custom logger", function() {
it("uses the custom logger", function() { it("uses the custom logger", function() {
var logger = { toString: function() { return "custom"; } }; var logger = { toString: function() { return "custom"; } };
Logger.setup(logger); Config.logging = { logger: logger };
Logger.setup();
expect(Logger.toString()).to.be.eql("custom"); expect(Logger.toString()).to.be.eql("custom");
}); });
}); });
@ -36,7 +42,7 @@ describe('Logger', function() {
var logger; var logger;
beforeEach(function() { beforeEach(function() {
logger = { logger = Config.logging.logger = {
debug: spy(), debug: spy(),
info: spy(), info: spy(),
warn: spy(), warn: spy(),
@ -44,7 +50,7 @@ describe('Logger', function() {
fatal: spy() fatal: spy()
}; };
Logger.setup(logger); Logger.setup();
}); });
describe("#debug", function() { describe("#debug", function() {
@ -95,7 +101,12 @@ describe('Logger', function() {
fatal: spy() fatal: spy()
}; };
Logger.setup(logger, 'warn'); Config.logging = {
logger: logger,
level: 'warn'
}
Logger.setup();
}); });
it("prevents logging of anything below the specified log level", function() { it("prevents logging of anything below the specified log level", function() {
@ -115,5 +126,16 @@ describe('Logger', function() {
expect(logger.error).to.be.calledWith('error message'); expect(logger.error).to.be.calledWith('error message');
expect(logger.fatal).to.be.calledWith('fatal message'); expect(logger.fatal).to.be.calledWith('fatal message');
}); });
it("defaults to 'info' level", function() {
delete Config.logging.level;
Logger.setup();
Logger.debug("debug message");
Logger.info("info message");
expect(logger.debug).to.not.be.called;
expect(logger.info).to.be.calledWith('info message');
})
}); });
}); });