createConsoleLogger.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { LogType } = require("./Logger");
  7. /** @typedef {import("../../declarations/WebpackOptions").FilterItemTypes} FilterItemTypes */
  8. /** @typedef {import("../../declarations/WebpackOptions").FilterTypes} FilterTypes */
  9. /** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
  10. /** @typedef {function(string): boolean} FilterFunction */
  11. /** @typedef {function(string, LogTypeEnum, any[]): void} LoggingFunction */
  12. /**
  13. * @typedef {object} LoggerConsole
  14. * @property {function(): void} clear
  15. * @property {function(): void} trace
  16. * @property {(...args: any[]) => void} info
  17. * @property {(...args: any[]) => void} log
  18. * @property {(...args: any[]) => void} warn
  19. * @property {(...args: any[]) => void} error
  20. * @property {(...args: any[]) => void=} debug
  21. * @property {(...args: any[]) => void=} group
  22. * @property {(...args: any[]) => void=} groupCollapsed
  23. * @property {(...args: any[]) => void=} groupEnd
  24. * @property {(...args: any[]) => void=} status
  25. * @property {(...args: any[]) => void=} profile
  26. * @property {(...args: any[]) => void=} profileEnd
  27. * @property {(...args: any[]) => void=} logTime
  28. */
  29. /**
  30. * @typedef {object} LoggerOptions
  31. * @property {false|true|"none"|"error"|"warn"|"info"|"log"|"verbose"} level loglevel
  32. * @property {FilterTypes|boolean} debug filter for debug logging
  33. * @property {LoggerConsole} console the console to log to
  34. */
  35. /**
  36. * @param {FilterItemTypes} item an input item
  37. * @returns {FilterFunction | undefined} filter function
  38. */
  39. const filterToFunction = item => {
  40. if (typeof item === "string") {
  41. const regExp = new RegExp(
  42. `[\\\\/]${item.replace(/[-[\]{}()*+?.\\^$|]/g, "\\$&")}([\\\\/]|$|!|\\?)`
  43. );
  44. return ident => regExp.test(ident);
  45. }
  46. if (item && typeof item === "object" && typeof item.test === "function") {
  47. return ident => item.test(ident);
  48. }
  49. if (typeof item === "function") {
  50. return item;
  51. }
  52. if (typeof item === "boolean") {
  53. return () => item;
  54. }
  55. };
  56. /**
  57. * @enum {number}
  58. */
  59. const LogLevel = {
  60. none: 6,
  61. false: 6,
  62. error: 5,
  63. warn: 4,
  64. info: 3,
  65. log: 2,
  66. true: 2,
  67. verbose: 1
  68. };
  69. /**
  70. * @param {LoggerOptions} options options object
  71. * @returns {LoggingFunction} logging function
  72. */
  73. module.exports = ({ level = "info", debug = false, console }) => {
  74. const debugFilters =
  75. /** @type {FilterFunction[]} */
  76. (
  77. typeof debug === "boolean"
  78. ? [() => debug]
  79. : /** @type {FilterItemTypes[]} */ ([])
  80. .concat(debug)
  81. .map(filterToFunction)
  82. );
  83. /** @type {number} */
  84. const loglevel = LogLevel[`${level}`] || 0;
  85. /**
  86. * @param {string} name name of the logger
  87. * @param {LogTypeEnum} type type of the log entry
  88. * @param {any[]} args arguments of the log entry
  89. * @returns {void}
  90. */
  91. const logger = (name, type, args) => {
  92. const labeledArgs = () => {
  93. if (Array.isArray(args)) {
  94. if (args.length > 0 && typeof args[0] === "string") {
  95. return [`[${name}] ${args[0]}`, ...args.slice(1)];
  96. } else {
  97. return [`[${name}]`, ...args];
  98. }
  99. } else {
  100. return [];
  101. }
  102. };
  103. const debug = debugFilters.some(f => f(name));
  104. switch (type) {
  105. case LogType.debug:
  106. if (!debug) return;
  107. if (typeof console.debug === "function") {
  108. console.debug(...labeledArgs());
  109. } else {
  110. console.log(...labeledArgs());
  111. }
  112. break;
  113. case LogType.log:
  114. if (!debug && loglevel > LogLevel.log) return;
  115. console.log(...labeledArgs());
  116. break;
  117. case LogType.info:
  118. if (!debug && loglevel > LogLevel.info) return;
  119. console.info(...labeledArgs());
  120. break;
  121. case LogType.warn:
  122. if (!debug && loglevel > LogLevel.warn) return;
  123. console.warn(...labeledArgs());
  124. break;
  125. case LogType.error:
  126. if (!debug && loglevel > LogLevel.error) return;
  127. console.error(...labeledArgs());
  128. break;
  129. case LogType.trace:
  130. if (!debug) return;
  131. console.trace();
  132. break;
  133. case LogType.groupCollapsed:
  134. if (!debug && loglevel > LogLevel.log) return;
  135. if (!debug && loglevel > LogLevel.verbose) {
  136. if (typeof console.groupCollapsed === "function") {
  137. console.groupCollapsed(...labeledArgs());
  138. } else {
  139. console.log(...labeledArgs());
  140. }
  141. break;
  142. }
  143. // falls through
  144. case LogType.group:
  145. if (!debug && loglevel > LogLevel.log) return;
  146. if (typeof console.group === "function") {
  147. console.group(...labeledArgs());
  148. } else {
  149. console.log(...labeledArgs());
  150. }
  151. break;
  152. case LogType.groupEnd:
  153. if (!debug && loglevel > LogLevel.log) return;
  154. if (typeof console.groupEnd === "function") {
  155. console.groupEnd();
  156. }
  157. break;
  158. case LogType.time: {
  159. if (!debug && loglevel > LogLevel.log) return;
  160. const ms = args[1] * 1000 + args[2] / 1000000;
  161. const msg = `[${name}] ${args[0]}: ${ms} ms`;
  162. if (typeof console.logTime === "function") {
  163. console.logTime(msg);
  164. } else {
  165. console.log(msg);
  166. }
  167. break;
  168. }
  169. case LogType.profile:
  170. if (typeof console.profile === "function") {
  171. console.profile(...labeledArgs());
  172. }
  173. break;
  174. case LogType.profileEnd:
  175. if (typeof console.profileEnd === "function") {
  176. console.profileEnd(...labeledArgs());
  177. }
  178. break;
  179. case LogType.clear:
  180. if (!debug && loglevel > LogLevel.log) return;
  181. if (typeof console.clear === "function") {
  182. console.clear();
  183. }
  184. break;
  185. case LogType.status:
  186. if (!debug && loglevel > LogLevel.info) return;
  187. if (typeof console.status === "function") {
  188. if (args.length === 0) {
  189. console.status();
  190. } else {
  191. console.status(...labeledArgs());
  192. }
  193. } else {
  194. if (args.length !== 0) {
  195. console.info(...labeledArgs());
  196. }
  197. }
  198. break;
  199. default:
  200. throw new Error(`Unexpected LogType ${type}`);
  201. }
  202. };
  203. return logger;
  204. };