From 71cbd35e90dccc71ee338b9adc70f8f1af6de199 Mon Sep 17 00:00:00 2001 From: indexzero Date: Fri, 23 Sep 2011 15:26:35 -0400 Subject: [PATCH] [api] Enable instances of `winston.Container` to share transports of `options.transports` is passed to the constructor function, `.get()`, or `.add()` --- lib/winston/container.js | 12 +++++++----- test/container-test.js | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/winston/container.js b/lib/winston/container.js index e9b5db1b6..42e672987 100644 --- a/lib/winston/container.js +++ b/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') { diff --git a/test/container-test.js b/test/container-test.js index ec6ec3424..2fcc26a38 100644 --- a/test/container-test.js +++ b/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); \ No newline at end of file