Logger should subscribe to updates, not be told what to do
This commit is contained in:
parent
8ac8d7de94
commit
b1c91b7419
13
lib/cylon.js
13
lib/cylon.js
|
@ -120,18 +120,7 @@ Cylon.start = function start() {
|
||||||
//
|
//
|
||||||
// Returns the current config
|
// Returns the current config
|
||||||
Cylon.config = function(opts) {
|
Cylon.config = function(opts) {
|
||||||
var loggingChanged = (
|
if (_.isObject(opts)) { Config.update(opts); }
|
||||||
opts.logging && Config.logging !== _.extend(Config.logging, opts.logging)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (_.isObject(opts)) {
|
|
||||||
Config = _.extend(Config, opts);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (loggingChanged) {
|
|
||||||
Logger.setup();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Config;
|
return Config;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,7 @@ var BasicLogger = require("./logger/basic_logger"),
|
||||||
|
|
||||||
var Logger = module.exports = {
|
var Logger = module.exports = {
|
||||||
setup: function(opts) {
|
setup: function(opts) {
|
||||||
if (_.isObject(opts)) {
|
if (_.isObject(opts)) { _.extend(Config.logging, opts); }
|
||||||
Config.logging = _.extend(Config.logging, opts);
|
|
||||||
}
|
|
||||||
|
|
||||||
var logger = Config.logging.logger,
|
var logger = Config.logging.logger,
|
||||||
level = Config.logging.level || "info";
|
level = Config.logging.level || "info";
|
||||||
|
@ -23,18 +21,19 @@ var Logger = module.exports = {
|
||||||
|
|
||||||
logger = (logger == null) ? BasicLogger : logger;
|
logger = (logger == null) ? BasicLogger : logger;
|
||||||
|
|
||||||
this.logger = logger || NullLogger;
|
Logger.logger = logger || NullLogger;
|
||||||
this.level = level;
|
Logger.level = level;
|
||||||
|
|
||||||
return this;
|
return Logger;
|
||||||
},
|
},
|
||||||
|
|
||||||
toString: function() {
|
toString: function() {
|
||||||
return this.logger.toString();
|
return Logger.logger.toString();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger.setup();
|
Logger.setup();
|
||||||
|
Config.subscribe(Logger.setup);
|
||||||
|
|
||||||
levels.forEach(function(level) {
|
levels.forEach(function(level) {
|
||||||
Logger[level] = function() {
|
Logger[level] = function() {
|
||||||
|
|
|
@ -111,15 +111,8 @@ describe("Cylon", function() {
|
||||||
|
|
||||||
describe("#config", function() {
|
describe("#config", function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
for (var c in Config) {
|
delete Config.a;
|
||||||
delete Config[c];
|
delete Config.b;
|
||||||
}
|
|
||||||
|
|
||||||
stub(Logger, "setup");
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(function() {
|
|
||||||
Logger.setup.restore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sets config variables", function() {
|
it("sets config variables", function() {
|
||||||
|
@ -146,14 +139,6 @@ 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() {
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var Logger = lib("logger"),
|
var Logger = lib("logger"),
|
||||||
Config = lib("config");
|
Config = lib("config"),
|
||||||
|
NullLogger = lib("logger/null_logger");
|
||||||
|
|
||||||
describe("Logger", function() {
|
describe("Logger", function() {
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
|
@ -146,4 +147,11 @@ describe("Logger", function() {
|
||||||
expect(logger.info).to.be.calledWith("info message");
|
expect(logger.info).to.be.calledWith("info message");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("automatically updates if configuration changed", function() {
|
||||||
|
var custom = spy();
|
||||||
|
expect(Logger.logger).to.be.eql(NullLogger);
|
||||||
|
Config.update({ logging: { logger: custom } });
|
||||||
|
expect(Logger.logger).to.be.eql(custom);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue