cocalc/src / smc-project / node_modules / forever / node_modules / winston / lib / winston / container.js
50663 views/*1* container.js: Inversion of control container for winston logger instances2*3* (C) 2010 Charlie Robbins4* MIT LICENCE5*6*/78var common = require('./common'),9winston = require('../winston');1011//12// ### function Container (options)13// #### @options {Object} Default pass-thru options for Loggers14// Constructor function for the Container object responsible for managing15// a set of `winston.Logger` instances based on string ids.16//17var Container = exports.Container = function (options) {18this.loggers = {};19this.options = options || {};20this.default = {21transports: [22new winston.transports.Console({23level: 'silly',24colorize: false25})26]27}28};2930//31// ### function get / add (id, options)32// #### @id {string} Id of the Logger to get33// #### @options {Object} **Optional** Options for the Logger instance34// Retreives a `winston.Logger` instance for the specified `id`. If35// an instance does not exist, one is created.36//37Container.prototype.get = Container.prototype.add = function (id, options) {38if (!this.loggers[id]) {39options = common.clone(options || this.options || this.default);40options.transports = options.transports || [];4142if (options.transports.length === 0 && (!options || !options['console'])) {43options.transports.push(this.default.transports[0]);44}4546Object.keys(options).forEach(function (key) {47if (key === 'transports') {48return;49}5051var name = common.capitalize(key);5253if (!winston.transports[name]) {54throw new Error('Cannot add unknown transport: ' + name);55}5657var namedOptions = options[key];58namedOptions.id = id;59options.transports.push(new (winston.transports[name])(namedOptions));60});6162this.loggers[id] = new winston.Logger(options);63}6465return this.loggers[id];66};6768//69// ### function close (id)70// #### @id {string} **Optional** Id of the Logger instance to find71// Returns a boolean value indicating if this instance72// has a logger with the specified `id`.73//74Container.prototype.has = function (id) {75return !!this.loggers[id];76};7778//79// ### function close (id)80// #### @id {string} **Optional** Id of the Logger instance to close81// Closes a `Logger` instance with the specified `id` if it exists.82// If no `id` is supplied then all Loggers are closed.83//84Container.prototype.close = function (id) {85var self = this;8687function _close (id) {88if (!self.loggers[id]) {89return;90}9192self.loggers[id].close();93delete self.loggers[id];94}9596return id ? _close(id) : Object.keys(this.loggers).forEach(function (id) {97_close(id);98});99};100101102103