winston.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * winston.js: Top-level include defining Winston.
  3. *
  4. * (C) 2010 Charlie Robbins
  5. * MIT LICENCE
  6. *
  7. */
  8. var winston = exports;
  9. //
  10. // use require method for webpack bundle
  11. //
  12. winston.version = require('../package.json').version
  13. //
  14. // Include transports defined by default by winston
  15. //
  16. winston.transports = require('./winston/transports');
  17. //
  18. // Expose utility methods
  19. //
  20. var common = require('./winston/common');
  21. winston.hash = common.hash;
  22. winston.clone = common.clone;
  23. winston.longestElement = common.longestElement;
  24. winston.exception = require('./winston/exception');
  25. winston.config = require('./winston/config');
  26. winston.addColors = winston.config.addColors;
  27. //
  28. // Expose core Logging-related prototypes.
  29. //
  30. winston.Container = require('./winston/container').Container;
  31. winston.Logger = require('./winston/logger').Logger;
  32. winston.Transport = require('./winston/transports/transport').Transport;
  33. //
  34. // We create and expose a default `Container` to `winston.loggers` so that the
  35. // programmer may manage multiple `winston.Logger` instances without any additional overhead.
  36. //
  37. // ### some-file1.js
  38. //
  39. // var logger = require('winston').loggers.get('something');
  40. //
  41. // ### some-file2.js
  42. //
  43. // var logger = require('winston').loggers.get('something');
  44. //
  45. winston.loggers = new winston.Container();
  46. //
  47. // We create and expose a 'defaultLogger' so that the programmer may do the
  48. // following without the need to create an instance of winston.Logger directly:
  49. //
  50. // var winston = require('winston');
  51. // winston.log('info', 'some message');
  52. // winston.error('some error');
  53. //
  54. var defaultLogger = new winston.Logger({
  55. transports: [new winston.transports.Console()]
  56. });
  57. //
  58. // Pass through the target methods onto `winston`.
  59. //
  60. var methods = [
  61. 'log',
  62. 'query',
  63. 'stream',
  64. 'add',
  65. 'remove',
  66. 'clear',
  67. 'profile',
  68. 'startTimer',
  69. 'extend',
  70. 'cli',
  71. 'handleExceptions',
  72. 'unhandleExceptions',
  73. 'configure'
  74. ];
  75. common.setLevels(winston, null, defaultLogger.levels);
  76. methods.forEach(function (method) {
  77. winston[method] = function () {
  78. return defaultLogger[method].apply(defaultLogger, arguments);
  79. };
  80. });
  81. //
  82. // ### function cli ()
  83. // Configures the default winston logger to have the
  84. // settings for command-line interfaces: no timestamp,
  85. // colors enabled, padded output, and additional levels.
  86. //
  87. winston.cli = function () {
  88. winston.padLevels = true;
  89. common.setLevels(winston, defaultLogger.levels, winston.config.cli.levels);
  90. defaultLogger.setLevels(winston.config.cli.levels);
  91. winston.config.addColors(winston.config.cli.colors);
  92. if (defaultLogger.transports.console) {
  93. defaultLogger.transports.console.colorize = true;
  94. defaultLogger.transports.console.timestamp = false;
  95. }
  96. return winston;
  97. };
  98. //
  99. // ### function setLevels (target)
  100. // #### @target {Object} Target levels to use
  101. // Sets the `target` levels specified on the default winston logger.
  102. //
  103. winston.setLevels = function (target) {
  104. common.setLevels(winston, defaultLogger.levels, target);
  105. defaultLogger.setLevels(target);
  106. };
  107. //
  108. // Define getter / setter for the default logger level
  109. // which need to be exposed by winston.
  110. //
  111. Object.defineProperty(winston, 'level', {
  112. get: function () {
  113. return defaultLogger.level;
  114. },
  115. set: function (val) {
  116. defaultLogger.level = val;
  117. Object.keys(defaultLogger.transports).forEach(function(key) {
  118. defaultLogger.transports[key].level = val;
  119. });
  120. }
  121. });
  122. //
  123. // Define getters / setters for appropriate properties of the
  124. // default logger which need to be exposed by winston.
  125. //
  126. ['emitErrs', 'exitOnError', 'padLevels', 'levelLength', 'stripColors'].forEach(function (prop) {
  127. Object.defineProperty(winston, prop, {
  128. get: function () {
  129. return defaultLogger[prop];
  130. },
  131. set: function (val) {
  132. defaultLogger[prop] = val;
  133. }
  134. });
  135. });
  136. //
  137. // @default {Object}
  138. // The default transports and exceptionHandlers for
  139. // the default winston logger.
  140. //
  141. Object.defineProperty(winston, 'default', {
  142. get: function () {
  143. return {
  144. transports: defaultLogger.transports,
  145. exceptionHandlers: defaultLogger.exceptionHandlers
  146. };
  147. }
  148. });