container.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * container.js: Inversion of control container for winston logger instances.
  3. *
  4. * (C) 2010 Charlie Robbins
  5. * MIT LICENCE
  6. */
  7. 'use strict';
  8. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  9. function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
  10. function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
  11. function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
  12. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  13. function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
  14. var createLogger = require('./create-logger');
  15. /**
  16. * Inversion of control container for winston logger instances.
  17. * @type {Container}
  18. */
  19. module.exports = /*#__PURE__*/function () {
  20. /**
  21. * Constructor function for the Container object responsible for managing a
  22. * set of `winston.Logger` instances based on string ids.
  23. * @param {!Object} [options={}] - Default pass-thru options for Loggers.
  24. */
  25. function Container() {
  26. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  27. _classCallCheck(this, Container);
  28. this.loggers = new Map();
  29. this.options = options;
  30. }
  31. /**
  32. * Retrieves a `winston.Logger` instance for the specified `id`. If an
  33. * instance does not exist, one is created.
  34. * @param {!string} id - The id of the Logger to get.
  35. * @param {?Object} [options] - Options for the Logger instance.
  36. * @returns {Logger} - A configured Logger instance with a specified id.
  37. */
  38. return _createClass(Container, [{
  39. key: "add",
  40. value: function add(id, options) {
  41. var _this = this;
  42. if (!this.loggers.has(id)) {
  43. // Remark: Simple shallow clone for configuration options in case we pass
  44. // in instantiated protoypal objects
  45. options = Object.assign({}, options || this.options);
  46. var existing = options.transports || this.options.transports;
  47. // Remark: Make sure if we have an array of transports we slice it to
  48. // make copies of those references.
  49. if (existing) {
  50. options.transports = Array.isArray(existing) ? existing.slice() : [existing];
  51. } else {
  52. options.transports = [];
  53. }
  54. var logger = createLogger(options);
  55. logger.on('close', function () {
  56. return _this._delete(id);
  57. });
  58. this.loggers.set(id, logger);
  59. }
  60. return this.loggers.get(id);
  61. }
  62. /**
  63. * Retreives a `winston.Logger` instance for the specified `id`. If
  64. * an instance does not exist, one is created.
  65. * @param {!string} id - The id of the Logger to get.
  66. * @param {?Object} [options] - Options for the Logger instance.
  67. * @returns {Logger} - A configured Logger instance with a specified id.
  68. */
  69. }, {
  70. key: "get",
  71. value: function get(id, options) {
  72. return this.add(id, options);
  73. }
  74. /**
  75. * Check if the container has a logger with the id.
  76. * @param {?string} id - The id of the Logger instance to find.
  77. * @returns {boolean} - Boolean value indicating if this instance has a
  78. * logger with the specified `id`.
  79. */
  80. }, {
  81. key: "has",
  82. value: function has(id) {
  83. return !!this.loggers.has(id);
  84. }
  85. /**
  86. * Closes a `Logger` instance with the specified `id` if it exists.
  87. * If no `id` is supplied then all Loggers are closed.
  88. * @param {?string} id - The id of the Logger instance to close.
  89. * @returns {undefined}
  90. */
  91. }, {
  92. key: "close",
  93. value: function close(id) {
  94. var _this2 = this;
  95. if (id) {
  96. return this._removeLogger(id);
  97. }
  98. this.loggers.forEach(function (val, key) {
  99. return _this2._removeLogger(key);
  100. });
  101. }
  102. /**
  103. * Remove a logger based on the id.
  104. * @param {!string} id - The id of the logger to remove.
  105. * @returns {undefined}
  106. * @private
  107. */
  108. }, {
  109. key: "_removeLogger",
  110. value: function _removeLogger(id) {
  111. if (!this.loggers.has(id)) {
  112. return;
  113. }
  114. var logger = this.loggers.get(id);
  115. logger.close();
  116. this._delete(id);
  117. }
  118. /**
  119. * Deletes a `Logger` instance with the specified `id`.
  120. * @param {!string} id - The id of the Logger instance to delete from
  121. * container.
  122. * @returns {undefined}
  123. * @private
  124. */
  125. }, {
  126. key: "_delete",
  127. value: function _delete(id) {
  128. this.loggers["delete"](id);
  129. }
  130. }]);
  131. }();