Browse files

[api] Enable instances of `winston.Container` to share transports of …

…`options.transports` is passed to the constructor function, `.get()`, or `.add()`
  • Loading branch information...
1 parent c6ea420 commit 71cbd35e90dccc71ee338b9adc70f8f1af6de199 @indexzero indexzero committed Sep 23, 2011
Showing with 49 additions and 5 deletions.
  1. +7 −5 lib/winston/container.js
  2. +42 −0 test/container-test.js
View
12 lib/winston/container.js
@@ -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
+ })
+ ]
}
};
@@ -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') {
View
42 test/container-test.js
@@ -8,6 +8,7 @@
var assert = require('assert'),
fs = require('fs'),
+ http = require('http'),
path = require('path'),
vows = require('vows'),
winston = require('../lib/winston'),
@@ -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.