colorize.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. const colors = require('@colors/colors/safe');
  3. const { LEVEL, MESSAGE } = require('triple-beam');
  4. //
  5. // Fix colors not appearing in non-tty environments
  6. //
  7. colors.enabled = true;
  8. /**
  9. * @property {RegExp} hasSpace
  10. * Simple regex to check for presence of spaces.
  11. */
  12. const hasSpace = /\s+/;
  13. /*
  14. * Colorizer format. Wraps the `level` and/or `message` properties
  15. * of the `info` objects with ANSI color codes based on a few options.
  16. */
  17. class Colorizer {
  18. constructor(opts = {}) {
  19. if (opts.colors) {
  20. this.addColors(opts.colors);
  21. }
  22. this.options = opts;
  23. }
  24. /*
  25. * Adds the colors Object to the set of allColors
  26. * known by the Colorizer
  27. *
  28. * @param {Object} colors Set of color mappings to add.
  29. */
  30. static addColors(clrs) {
  31. const nextColors = Object.keys(clrs).reduce((acc, level) => {
  32. acc[level] = hasSpace.test(clrs[level])
  33. ? clrs[level].split(hasSpace)
  34. : clrs[level];
  35. return acc;
  36. }, {});
  37. Colorizer.allColors = Object.assign({}, Colorizer.allColors || {}, nextColors);
  38. return Colorizer.allColors;
  39. }
  40. /*
  41. * Adds the colors Object to the set of allColors
  42. * known by the Colorizer
  43. *
  44. * @param {Object} colors Set of color mappings to add.
  45. */
  46. addColors(clrs) {
  47. return Colorizer.addColors(clrs);
  48. }
  49. /*
  50. * function colorize (lookup, level, message)
  51. * Performs multi-step colorization using @colors/colors/safe
  52. */
  53. colorize(lookup, level, message) {
  54. if (typeof message === 'undefined') {
  55. message = level;
  56. }
  57. //
  58. // If the color for the level is just a string
  59. // then attempt to colorize the message with it.
  60. //
  61. if (!Array.isArray(Colorizer.allColors[lookup])) {
  62. return colors[Colorizer.allColors[lookup]](message);
  63. }
  64. //
  65. // If it is an Array then iterate over that Array, applying
  66. // the colors function for each item.
  67. //
  68. for (let i = 0, len = Colorizer.allColors[lookup].length; i < len; i++) {
  69. message = colors[Colorizer.allColors[lookup][i]](message);
  70. }
  71. return message;
  72. }
  73. /*
  74. * function transform (info, opts)
  75. * Attempts to colorize the { level, message } of the given
  76. * `logform` info object.
  77. */
  78. transform(info, opts) {
  79. if (opts.all && typeof info[MESSAGE] === 'string') {
  80. info[MESSAGE] = this.colorize(info[LEVEL], info.level, info[MESSAGE]);
  81. }
  82. if (opts.level || opts.all || !opts.message) {
  83. info.level = this.colorize(info[LEVEL], info.level);
  84. }
  85. if (opts.all || opts.message) {
  86. info.message = this.colorize(info[LEVEL], info.level, info.message);
  87. }
  88. return info;
  89. }
  90. }
  91. /*
  92. * function colorize (info)
  93. * Returns a new instance of the colorize Format that applies
  94. * level colors to `info` objects. This was previously exposed
  95. * as { colorize: true } to transports in `winston < 3.0.0`.
  96. */
  97. module.exports = opts => new Colorizer(opts);
  98. //
  99. // Attach the Colorizer for registration purposes
  100. //
  101. module.exports.Colorizer
  102. = module.exports.Format
  103. = Colorizer;