Skip to content

Commit

Permalink
[api] Enable instances of winston.Container to share transports of …
Browse files Browse the repository at this point in the history
…`options.transports` is passed to the constructor function, `.get()`, or `.add()`
  • Loading branch information
indexzero committed Sep 23, 2011
1 parent c6ea420 commit 71cbd35
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/winston/container.js
Expand Up @@ -19,10 +19,12 @@ var Container = exports.Container = function (options) {
this.loggers = {};
this.options = options || {};
this.default = {
console: {
level: 'silly',
colorize: false
}
tranports: [
new winston.transports.Console({
level: 'silly',
colorize: false
})
]
}
};

Expand All @@ -36,7 +38,7 @@ var Container = exports.Container = function (options) {
Container.prototype.get = Container.prototype.add = function (id, options) {
if (!this.loggers[id]) {
options = common.clone(options || this.options || this.default);
options.transports = [];
options.transports = options.transports || [];

Object.keys(options).forEach(function (key) {
if (key === 'transports') {
Expand Down
42 changes: 42 additions & 0 deletions test/container-test.js
Expand Up @@ -8,6 +8,7 @@

var assert = require('assert'),
fs = require('fs'),
http = require('http'),
path = require('path'),
vows = require('vows'),
winston = require('../lib/winston'),
Expand Down Expand Up @@ -53,5 +54,46 @@ vows.describe('winston/container').addBatch({
}
}
}
},
"An instance of winston.Container with explicit transports": {
topic: function () {
this.port = 9412;
this.transports = [
new winston.transports.Webhook({
port: this.port
})
];

this.container = new winston.Container({
transports: this.transports
});

return null;
},
"the get() method": {
topic: function (container) {
var server = http.createServer(function (req, res) {
res.end();
});

server.listen(this.port, this.callback.bind(this, null));
},
"should add the logger correctly": function () {
this.someLogger = this.container.get('some-logger');
assert.isObject(this.someLogger.transports);
assert.instanceOf(this.someLogger.transports['webhook'], winston.transports.Webhook);
assert.strictEqual(this.someLogger.transports['webhook'], this.transports[0]);
},
"a second call to get()": {
"should respond with the same transport object": function () {
this.someOtherLogger = this.container.get('some-other-logger');

assert.isObject(this.someOtherLogger.transports);
assert.instanceOf(this.someOtherLogger.transports['webhook'], winston.transports.Webhook);
assert.strictEqual(this.someOtherLogger.transports['webhook'], this.transports[0]);
assert.strictEqual(this.someOtherLogger.transports['webhook'], this.someLogger.transports['webhook']);
}
}
}
}
}).export(module);

0 comments on commit 71cbd35

Please sign in to comment.