Merge pull request #236 from hybridgroup/fix/move-logger-config

Move Logger configuration into Cylon#config
This commit is contained in:
Ron Evans 2014-10-28 23:03:54 +00:00
commit 0cd85988e4
6 changed files with 62 additions and 11 deletions

View File

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

View File

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

View File

@ -11,7 +11,8 @@
var levels = ["debug", "info", "warn", "error", "fatal"];
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
// other output) easily and consistently. It's available anywhere in Cylon, as
@ -27,7 +28,10 @@ var Logger = module.exports = {};
// or above
//
// 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; }
this.logger = logger || NullLogger;
@ -55,7 +59,7 @@ var setLogLevel = function(level) {
ignored;
if (index < 0) {
throw new Error("Invalid Log Level specified");
throw new Error("Invalid Log Level specified: " + level);
}
active = levels.slice(index);

View File

@ -31,6 +31,9 @@ var Cylon = source('cylon');
Cylon.config({
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) {
delete Config[c];
}
stub(Logger, 'setup');
});
afterEach(function() {
Logger.setup.restore();
});
it("sets config variables", function() {
@ -127,6 +133,14 @@ describe("Cylon", function() {
Cylon.config("hello world");
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() {

View File

@ -1,16 +1,20 @@
'use strict';
var Logger = source('logger'),
Config = source('config'),
Utils = source('utils');
describe('Logger', 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() {
context("with no arguments", function() {
it("sets up a BasicLogger", function() {
Config.logging = {};
Logger.setup();
expect(Logger.toString()).to.be.eql("BasicLogger");
});
@ -18,7 +22,8 @@ describe('Logger', function() {
context("with false", function() {
it("sets up a NullLogger", function() {
Logger.setup(false);
Config.logging = { logger: false };
Logger.setup();
expect(Logger.toString()).to.be.eql("NullLogger");
});
});
@ -26,7 +31,8 @@ describe('Logger', function() {
context("with a custom logger", function() {
it("uses the custom logger", function() {
var logger = { toString: function() { return "custom"; } };
Logger.setup(logger);
Config.logging = { logger: logger };
Logger.setup();
expect(Logger.toString()).to.be.eql("custom");
});
});
@ -36,7 +42,7 @@ describe('Logger', function() {
var logger;
beforeEach(function() {
logger = {
logger = Config.logging.logger = {
debug: spy(),
info: spy(),
warn: spy(),
@ -44,7 +50,7 @@ describe('Logger', function() {
fatal: spy()
};
Logger.setup(logger);
Logger.setup();
});
describe("#debug", function() {
@ -95,7 +101,12 @@ describe('Logger', function() {
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() {
@ -115,5 +126,16 @@ describe('Logger', function() {
expect(logger.error).to.be.calledWith('error 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');
})
});
});