winston.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * winston.js: Top-level include defining Winston.
  3. *
  4. * (C) 2010 Charlie Robbins
  5. * MIT LICENCE
  6. */
  7. 'use strict';
  8. const logform = require('logform');
  9. const { warn } = require('./winston/common');
  10. /**
  11. * Setup to expose.
  12. * @type {Object}
  13. */
  14. const winston = exports;
  15. /**
  16. * Expose version. Use `require` method for `webpack` support.
  17. * @type {string}
  18. */
  19. winston.version = require('../package.json').version;
  20. /**
  21. * Include transports defined by default by winston
  22. * @type {Array}
  23. */
  24. winston.transports = require('./winston/transports');
  25. /**
  26. * Expose utility methods
  27. * @type {Object}
  28. */
  29. winston.config = require('./winston/config');
  30. /**
  31. * Hoist format-related functionality from logform.
  32. * @type {Object}
  33. */
  34. winston.addColors = logform.levels;
  35. /**
  36. * Hoist format-related functionality from logform.
  37. * @type {Object}
  38. */
  39. winston.format = logform.format;
  40. /**
  41. * Expose core Logging-related prototypes.
  42. * @type {function}
  43. */
  44. winston.createLogger = require('./winston/create-logger');
  45. /**
  46. * Expose core Logging-related prototypes.
  47. * @type {Object}
  48. */
  49. winston.ExceptionHandler = require('./winston/exception-handler');
  50. /**
  51. * Expose core Logging-related prototypes.
  52. * @type {Container}
  53. */
  54. winston.Container = require('./winston/container');
  55. /**
  56. * Expose core Logging-related prototypes.
  57. * @type {Object}
  58. */
  59. winston.Transport = require('winston-transport');
  60. /**
  61. * We create and expose a default `Container` to `winston.loggers` so that the
  62. * programmer may manage multiple `winston.Logger` instances without any
  63. * additional overhead.
  64. * @example
  65. * // some-file1.js
  66. * const logger = require('winston').loggers.get('something');
  67. *
  68. * // some-file2.js
  69. * const logger = require('winston').loggers.get('something');
  70. */
  71. winston.loggers = new winston.Container();
  72. /**
  73. * We create and expose a 'defaultLogger' so that the programmer may do the
  74. * following without the need to create an instance of winston.Logger directly:
  75. * @example
  76. * const winston = require('winston');
  77. * winston.log('info', 'some message');
  78. * winston.error('some error');
  79. */
  80. const defaultLogger = winston.createLogger();
  81. // Pass through the target methods onto `winston.
  82. Object.keys(winston.config.npm.levels).concat([
  83. 'log',
  84. 'query',
  85. 'stream',
  86. 'add',
  87. 'remove',
  88. 'clear',
  89. 'profile',
  90. 'startTimer',
  91. 'handleExceptions',
  92. 'unhandleExceptions',
  93. 'configure'
  94. ]).forEach(method => (
  95. winston[method] = (...args) => defaultLogger[method](...args)
  96. ));
  97. /**
  98. * Define getter / setter for the default logger level which need to be exposed
  99. * by winston.
  100. * @type {string}
  101. */
  102. Object.defineProperty(winston, 'level', {
  103. get() {
  104. return defaultLogger.level;
  105. },
  106. set(val) {
  107. defaultLogger.level = val;
  108. }
  109. });
  110. /**
  111. * Define getter for `exceptions` which replaces `handleExceptions` and
  112. * `unhandleExceptions`.
  113. * @type {Object}
  114. */
  115. Object.defineProperty(winston, 'exceptions', {
  116. get() {
  117. return defaultLogger.exceptions;
  118. }
  119. });
  120. /**
  121. * Define getters / setters for appropriate properties of the default logger
  122. * which need to be exposed by winston.
  123. * @type {Logger}
  124. */
  125. [
  126. 'exitOnError'
  127. ].forEach(prop => {
  128. Object.defineProperty(winston, prop, {
  129. get() {
  130. return defaultLogger[prop];
  131. },
  132. set(val) {
  133. defaultLogger[prop] = val;
  134. }
  135. });
  136. });
  137. /**
  138. * The default transports and exceptionHandlers for the default winston logger.
  139. * @type {Object}
  140. */
  141. Object.defineProperty(winston, 'default', {
  142. get() {
  143. return {
  144. exceptionHandlers: defaultLogger.exceptionHandlers,
  145. transports: defaultLogger.transports
  146. };
  147. }
  148. });
  149. // Have friendlier breakage notices for properties that were exposed by default
  150. // on winston < 3.0.
  151. warn.deprecated(winston, 'setLevels');
  152. warn.forFunctions(winston, 'useFormat', ['cli']);
  153. warn.forProperties(winston, 'useFormat', [
  154. 'padLevels',
  155. 'stripColors'
  156. ]);
  157. warn.forFunctions(winston, 'deprecated', [
  158. 'addRewriter',
  159. 'addFilter',
  160. 'clone',
  161. 'extend'
  162. ]);
  163. warn.forProperties(winston, 'deprecated', [
  164. 'emitErrs',
  165. 'levelLength'
  166. ]);
  167. // Throw a useful error when users attempt to run `new winston.Logger`.
  168. warn.moved(winston, 'createLogger', 'Logger');