index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. /* global window: true */
  3. /* eslint-disable
  4. no-shadow,
  5. no-param-reassign,
  6. space-before-function-paren
  7. */
  8. const uuid = require('uuid/v4');
  9. const colors = require('ansi-colors');
  10. const loglevel = require('./loglevel');
  11. const symbols = {
  12. trace: colors.grey('₸'),
  13. debug: colors.cyan('➤'),
  14. info: colors.blue(colors.symbols.info),
  15. warn: colors.yellow(colors.symbols.warning),
  16. error: colors.red(colors.symbols.cross)
  17. };
  18. const defaults = {
  19. name: '<unknown>',
  20. level: 'info',
  21. unique: true
  22. };
  23. const prefix = {
  24. level (options) {
  25. return symbols[options.level];
  26. },
  27. template: `{{level}} ${colors.gray('「{{name}}」')}: `
  28. };
  29. function log (options) {
  30. const opts = Object.assign({}, defaults, options);
  31. const { id } = options;
  32. opts.prefix = Object.assign({}, prefix, options.prefix);
  33. delete opts.id;
  34. Object.defineProperty(opts, 'id', {
  35. get() {
  36. if (!id) {
  37. return this.name + (opts.unique ? `-${uuid()}` : '');
  38. }
  39. return id;
  40. }
  41. });
  42. if (opts.timestamp) {
  43. opts.prefix.template = `[{{time}}] ${opts.prefix.template}`;
  44. }
  45. const log = loglevel.getLogger(opts);
  46. if (!Object.prototype.hasOwnProperty.call(log, 'id')) {
  47. Object.defineProperty(log, 'id', {
  48. get() {
  49. return opts.id;
  50. }
  51. });
  52. }
  53. return log;
  54. }
  55. module.exports = log;
  56. // NOTE: this is exported so that consumers of webpack-log can use the same
  57. // version of ansi-colors to decorate log messages without incurring additional
  58. // dependency overhead
  59. module.exports.colors = colors;
  60. // NOTE: This is an undocumented function solely for the purpose of tests.
  61. // Do not use this method in production code. Using in production code
  62. // may result in strange behavior.
  63. module.exports.delLogger = function delLogger(name) {
  64. delete loglevel.loggers[name];
  65. };
  66. module.exports.factories = loglevel.factories;