container.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. const createLogger = require('./create-logger');
  9. /**
  10. * Inversion of control container for winston logger instances.
  11. * @type {Container}
  12. */
  13. module.exports = class Container {
  14. /**
  15. * Constructor function for the Container object responsible for managing a
  16. * set of `winston.Logger` instances based on string ids.
  17. * @param {!Object} [options={}] - Default pass-thru options for Loggers.
  18. */
  19. constructor(options = {}) {
  20. this.loggers = new Map();
  21. this.options = options;
  22. }
  23. /**
  24. * Retreives a `winston.Logger` instance for the specified `id`. If an
  25. * instance does not exist, one is created.
  26. * @param {!string} id - The id of the Logger to get.
  27. * @param {?Object} [options] - Options for the Logger instance.
  28. * @returns {Logger} - A configured Logger instance with a specified id.
  29. */
  30. add(id, options) {
  31. if (!this.loggers.has(id)) {
  32. // Remark: Simple shallow clone for configuration options in case we pass
  33. // in instantiated protoypal objects
  34. options = Object.assign({}, options || this.options);
  35. const existing = options.transports || this.options.transports;
  36. // Remark: Make sure if we have an array of transports we slice it to
  37. // make copies of those references.
  38. options.transports = existing ? existing.slice() : [];
  39. const logger = createLogger(options);
  40. logger.on('close', () => this._delete(id));
  41. this.loggers.set(id, logger);
  42. }
  43. return this.loggers.get(id);
  44. }
  45. /**
  46. * Retreives a `winston.Logger` instance for the specified `id`. If
  47. * an instance does not exist, one is created.
  48. * @param {!string} id - The id of the Logger to get.
  49. * @param {?Object} [options] - Options for the Logger instance.
  50. * @returns {Logger} - A configured Logger instance with a specified id.
  51. */
  52. get(id, options) {
  53. return this.add(id, options);
  54. }
  55. /**
  56. * Check if the container has a logger with the id.
  57. * @param {?string} id - The id of the Logger instance to find.
  58. * @returns {boolean} - Boolean value indicating if this instance has a
  59. * logger with the specified `id`.
  60. */
  61. has(id) {
  62. return !!this.loggers.has(id);
  63. }
  64. /**
  65. * Closes a `Logger` instance with the specified `id` if it exists.
  66. * If no `id` is supplied then all Loggers are closed.
  67. * @param {?string} id - The id of the Logger instance to close.
  68. * @returns {undefined}
  69. */
  70. close(id) {
  71. if (id) {
  72. return this._removeLogger(id);
  73. }
  74. this.loggers.forEach((val, key) => this._removeLogger(key));
  75. }
  76. /**
  77. * Remove a logger based on the id.
  78. * @param {!string} id - The id of the logger to remove.
  79. * @returns {undefined}
  80. * @private
  81. */
  82. _removeLogger(id) {
  83. if (!this.loggers.has(id)) {
  84. return;
  85. }
  86. const logger = this.loggers.get(id);
  87. logger.close();
  88. this._delete(id);
  89. }
  90. /**
  91. * Deletes a `Logger` instance with the specified `id`.
  92. * @param {!string} id - The id of the Logger instance to delete from
  93. * container.
  94. * @returns {undefined}
  95. * @private
  96. */
  97. _delete(id) {
  98. this.loggers.delete(id);
  99. }
  100. };