mirror of https://github.com/webpack/webpack.git
allow to configure runtime logging
This commit is contained in:
parent
c547dd5615
commit
d80989bfbf
|
@ -27,12 +27,17 @@ const ResolverFactory = require("./ResolverFactory");
|
||||||
const RequestShortener = require("./RequestShortener");
|
const RequestShortener = require("./RequestShortener");
|
||||||
const { makePathsRelative } = require("./util/identifier");
|
const { makePathsRelative } = require("./util/identifier");
|
||||||
const ConcurrentCompilationError = require("./ConcurrentCompilationError");
|
const ConcurrentCompilationError = require("./ConcurrentCompilationError");
|
||||||
const { Logger, LogType } = require("./logging/Logger");
|
const { Logger } = require("./logging/Logger");
|
||||||
const logToConsole = require("./logging/logToConsole");
|
const logToConsole = require("./logging/logToConsole");
|
||||||
|
|
||||||
/** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
|
/** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
|
||||||
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
|
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
|
||||||
|
|
||||||
|
const infrastructureLogger = logToConsole({
|
||||||
|
level: "log",
|
||||||
|
debug: false
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} CompilationParams
|
* @typedef {Object} CompilationParams
|
||||||
* @property {NormalModuleFactory} normalModuleFactory
|
* @property {NormalModuleFactory} normalModuleFactory
|
||||||
|
@ -221,9 +226,7 @@ class Compiler extends Tapable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.hooks.log.call(name, type, args) === undefined) {
|
if (this.hooks.log.call(name, type, args) === undefined) {
|
||||||
// ignore debug logging by default
|
infrastructureLogger(name, type, args);
|
||||||
if (type === LogType.debug) return;
|
|
||||||
logToConsole(name, type, args);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,103 +8,176 @@
|
||||||
const { LogType } = require("./Logger");
|
const { LogType } = require("./Logger");
|
||||||
|
|
||||||
/** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
|
/** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
|
||||||
|
/** @typedef {function(string): boolean} FilterFunction */
|
||||||
|
/** @typedef {RegExp|FilterFunction|string|boolean} FilterInputItem */
|
||||||
|
/** @typedef {FilterInputItem[]|FilterInputItem} FilterInput */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} name name of the logger
|
* @typedef {Object} LoggerOptions
|
||||||
* @param {LogTypeEnum} type type of the log entry
|
* @property {false|true|"error"|"warn"|"info"|"log"|"verbose"} options.level loglevel
|
||||||
* @param {any[]} args arguments of the log entry
|
* @property {FilterInput} options.debug filter for debug logging
|
||||||
*/
|
*/
|
||||||
module.exports = (name, type, args) => {
|
|
||||||
const labeledArgs = (prefix = "") => {
|
/**
|
||||||
if (Array.isArray(args)) {
|
* @param {FilterInputItem} item an input item
|
||||||
if (args.length > 0 && typeof args[0] === "string") {
|
* @returns {function(string): boolean} filter funtion
|
||||||
return [`${prefix}[${name}] ${args[0]}`, ...args.slice(1)];
|
*/
|
||||||
} else {
|
const filterToFunction = item => {
|
||||||
return [`${prefix}[${name}]`, ...args];
|
if (typeof item === "string") {
|
||||||
}
|
const regExp = new RegExp(
|
||||||
} else {
|
`[\\\\/]${item.replace(
|
||||||
return [];
|
// eslint-disable-next-line no-useless-escape
|
||||||
}
|
/[-[\]{}()*+?.\\^$|]/g,
|
||||||
};
|
"\\$&"
|
||||||
switch (type) {
|
)}([\\\\/]|$|!|\\?)`
|
||||||
case LogType.debug:
|
);
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
return ident => regExp.test(ident);
|
||||||
if (typeof console.debug === "function") {
|
}
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
if (item && typeof item === "object" && typeof item.test === "function") {
|
||||||
console.debug(...labeledArgs());
|
return ident => item.test(ident);
|
||||||
} else {
|
}
|
||||||
console.log(...labeledArgs());
|
if (typeof item === "function") {
|
||||||
}
|
return item;
|
||||||
break;
|
}
|
||||||
case LogType.log:
|
if (typeof item === "boolean") {
|
||||||
console.log(...labeledArgs());
|
return () => item;
|
||||||
break;
|
|
||||||
case LogType.info:
|
|
||||||
console.info(...labeledArgs("<i> "));
|
|
||||||
break;
|
|
||||||
case LogType.warn:
|
|
||||||
console.warn(...labeledArgs("<w> "));
|
|
||||||
break;
|
|
||||||
case LogType.error:
|
|
||||||
console.error(...labeledArgs("<e> "));
|
|
||||||
break;
|
|
||||||
case LogType.trace:
|
|
||||||
console.trace();
|
|
||||||
break;
|
|
||||||
case LogType.group:
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
if (typeof console.group === "function") {
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
console.group(...labeledArgs());
|
|
||||||
} else {
|
|
||||||
console.log(...labeledArgs());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case LogType.groupCollapsed:
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
if (typeof console.groupCollapsed === "function") {
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
console.groupCollapsed(...labeledArgs());
|
|
||||||
} else {
|
|
||||||
console.log(...labeledArgs("<g> "));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case LogType.groupEnd:
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
if (typeof console.groupEnd === "function") {
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
console.groupEnd();
|
|
||||||
} else {
|
|
||||||
console.log(...labeledArgs("</g> "));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case LogType.time:
|
|
||||||
console.log(
|
|
||||||
`[${name}] ${args[0]}: ${args[1] * 1000 + args[2] / 1000000}ms`
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case LogType.profile:
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
if (typeof console.profile === "function") {
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
console.profile(...labeledArgs());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case LogType.profileEnd:
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
if (typeof console.profileEnd === "function") {
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
console.profileEnd(...labeledArgs());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case LogType.clear:
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
if (typeof console.clear === "function") {
|
|
||||||
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
||||||
console.clear();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error(`Unexpected LogType ${type}`);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @enum {number} */
|
||||||
|
const LogLevel = {
|
||||||
|
false: 6,
|
||||||
|
error: 5,
|
||||||
|
warn: 4,
|
||||||
|
info: 3,
|
||||||
|
log: 2,
|
||||||
|
true: 2,
|
||||||
|
verbose: 1
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {LoggerOptions} options options object
|
||||||
|
* @returns {function(string, LogTypeEnum, any[]): void} logging function
|
||||||
|
*/
|
||||||
|
module.exports = ({ level = "info", debug = false }) => {
|
||||||
|
const debugFilters = /** @type {FilterInputItem[]} */ ([])
|
||||||
|
.concat(debug)
|
||||||
|
.map(filterToFunction);
|
||||||
|
/** @type {number} */
|
||||||
|
const loglevel = LogLevel[`${level}`] || 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} name name of the logger
|
||||||
|
* @param {LogTypeEnum} type type of the log entry
|
||||||
|
* @param {any[]} args arguments of the log entry
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
const logger = (name, type, args) => {
|
||||||
|
const labeledArgs = (prefix = "") => {
|
||||||
|
if (Array.isArray(args)) {
|
||||||
|
if (args.length > 0 && typeof args[0] === "string") {
|
||||||
|
return [`${prefix}[${name}] ${args[0]}`, ...args.slice(1)];
|
||||||
|
} else {
|
||||||
|
return [`${prefix}[${name}]`, ...args];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const debug = debugFilters.some(f => f(name));
|
||||||
|
switch (type) {
|
||||||
|
case LogType.debug:
|
||||||
|
if (!debug) return;
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
if (typeof console.debug === "function") {
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
console.debug(...labeledArgs());
|
||||||
|
} else {
|
||||||
|
console.log(...labeledArgs());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LogType.log:
|
||||||
|
if (!debug && loglevel > LogLevel.log) return;
|
||||||
|
console.log(...labeledArgs());
|
||||||
|
break;
|
||||||
|
case LogType.info:
|
||||||
|
if (!debug && loglevel > LogLevel.info) return;
|
||||||
|
console.info(...labeledArgs("<i> "));
|
||||||
|
break;
|
||||||
|
case LogType.warn:
|
||||||
|
if (!debug && loglevel > LogLevel.warn) return;
|
||||||
|
console.warn(...labeledArgs("<w> "));
|
||||||
|
break;
|
||||||
|
case LogType.error:
|
||||||
|
if (!debug && loglevel > LogLevel.error) return;
|
||||||
|
console.error(...labeledArgs("<e> "));
|
||||||
|
break;
|
||||||
|
case LogType.trace:
|
||||||
|
if (!debug) return;
|
||||||
|
console.trace();
|
||||||
|
break;
|
||||||
|
case LogType.group:
|
||||||
|
if (!debug && loglevel > LogLevel.log) return;
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
if (typeof console.group === "function") {
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
console.group(...labeledArgs());
|
||||||
|
} else {
|
||||||
|
console.log(...labeledArgs());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LogType.groupCollapsed:
|
||||||
|
if (!debug && loglevel > LogLevel.log) return;
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
if (typeof console.groupCollapsed === "function") {
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
console.groupCollapsed(...labeledArgs());
|
||||||
|
} else {
|
||||||
|
console.log(...labeledArgs("<g> "));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LogType.groupEnd:
|
||||||
|
if (!debug && loglevel > LogLevel.log) return;
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
if (typeof console.groupEnd === "function") {
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
console.groupEnd();
|
||||||
|
} else {
|
||||||
|
console.log(...labeledArgs("</g> "));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LogType.time:
|
||||||
|
if (!debug && loglevel > LogLevel.log) return;
|
||||||
|
console.log(
|
||||||
|
`[${name}] ${args[0]}: ${args[1] * 1000 + args[2] / 1000000}ms`
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case LogType.profile:
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
if (typeof console.profile === "function") {
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
console.profile(...labeledArgs());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LogType.profileEnd:
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
if (typeof console.profileEnd === "function") {
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
console.profileEnd(...labeledArgs());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LogType.clear:
|
||||||
|
if (!debug && loglevel > LogLevel.log) return;
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
if (typeof console.clear === "function") {
|
||||||
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
||||||
|
console.clear();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error(`Unexpected LogType ${type}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return logger;
|
||||||
|
};
|
||||||
|
|
|
@ -2,14 +2,34 @@ const SyncBailHook = require("tapable/lib/SyncBailHook");
|
||||||
const { Logger } = require("./Logger");
|
const { Logger } = require("./Logger");
|
||||||
const logToConsole = require("./logToConsole");
|
const logToConsole = require("./logToConsole");
|
||||||
|
|
||||||
|
/** @type {logToConsole.LoggerOptions} */
|
||||||
|
let currentDefaultLoggerOptions = {
|
||||||
|
level: "info",
|
||||||
|
debug: false
|
||||||
|
};
|
||||||
|
let currentDefaultLogger = logToConsole(currentDefaultLoggerOptions);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} name name of the logger
|
||||||
|
* @returns {Logger} a logger
|
||||||
|
*/
|
||||||
exports.getLogger = name => {
|
exports.getLogger = name => {
|
||||||
return new Logger((type, args) => {
|
return new Logger((type, args) => {
|
||||||
if (exports.hooks.log.call(name, type, args) === undefined) {
|
if (exports.hooks.log.call(name, type, args) === undefined) {
|
||||||
logToConsole(name, type, args);
|
currentDefaultLogger(name, type, args);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {logToConsole.LoggerOptions} options new options, merge with old options
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
exports.configureDefaultLogger = options => {
|
||||||
|
Object.assign(currentDefaultLoggerOptions, options);
|
||||||
|
currentDefaultLogger = logToConsole(currentDefaultLoggerOptions);
|
||||||
|
};
|
||||||
|
|
||||||
exports.hooks = {
|
exports.hooks = {
|
||||||
log: new SyncBailHook(["origin", "type", "args"])
|
log: new SyncBailHook(["origin", "type", "args"])
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue