debug logging should be included in stats even if stats.logging is false

This commit is contained in:
Tobias Koppers 2021-02-22 11:38:11 +01:00
parent e09090bbf1
commit 9b448c14d9
5 changed files with 188 additions and 131 deletions

View File

@ -262,7 +262,7 @@ const { isSourceEqual } = require("./util/source");
* @property {any} _env
*/
/** @typedef {KnownNormalizedStatsOptions & StatsOptions & Record<string, any>} NormalizedStatsOptions */
/** @typedef {KnownNormalizedStatsOptions & Omit<StatsOptions, keyof KnownNormalizedStatsOptions> & Record<string, any>} NormalizedStatsOptions */
/**
* @typedef {Object} KnownCreateStatsOptionsContext

View File

@ -460,7 +460,7 @@ const EXTRACT_ERROR = {
/** @type {SimpleExtractors} */
const SIMPLE_EXTRACTORS = {
compilation: {
_: (object, compilation, context) => {
_: (object, compilation, context, options) => {
if (!context.makePathsRelative) {
context.makePathsRelative = makePathsRelative.bindContextCache(
compilation.compiler.context,
@ -495,6 +495,144 @@ const SIMPLE_EXTRACTORS = {
if (compilation.needAdditionalPass) {
object.needAdditionalPass = true;
}
const { logging, loggingDebug, loggingTrace } = options;
if (logging || (loggingDebug && loggingDebug.length > 0)) {
const util = require("util");
object.logging = {};
let acceptedTypes;
let collapsedGroups = false;
switch (logging) {
default:
acceptedTypes = new Set();
break;
case "error":
acceptedTypes = new Set([LogType.error]);
break;
case "warn":
acceptedTypes = new Set([LogType.error, LogType.warn]);
break;
case "info":
acceptedTypes = new Set([
LogType.error,
LogType.warn,
LogType.info
]);
break;
case "log":
acceptedTypes = new Set([
LogType.error,
LogType.warn,
LogType.info,
LogType.log,
LogType.group,
LogType.groupEnd,
LogType.groupCollapsed,
LogType.clear
]);
break;
case "verbose":
acceptedTypes = new Set([
LogType.error,
LogType.warn,
LogType.info,
LogType.log,
LogType.group,
LogType.groupEnd,
LogType.groupCollapsed,
LogType.profile,
LogType.profileEnd,
LogType.time,
LogType.status,
LogType.clear
]);
collapsedGroups = true;
break;
}
const cachedMakePathsRelative = makePathsRelative.bindContextCache(
options.context,
compilation.compiler.root
);
let depthInCollapsedGroup = 0;
for (const [origin, logEntries] of compilation.logging) {
const debugMode = loggingDebug.some(fn => fn(origin));
if (logging === false && !debugMode) continue;
/** @type {KnownStatsLoggingEntry[]} */
const groupStack = [];
/** @type {KnownStatsLoggingEntry[]} */
const rootList = [];
let currentList = rootList;
let processedLogEntries = 0;
for (const entry of logEntries) {
let type = entry.type;
if (!debugMode && !acceptedTypes.has(type)) continue;
// Expand groups in verbose and debug modes
if (
type === LogType.groupCollapsed &&
(debugMode || collapsedGroups)
)
type = LogType.group;
if (depthInCollapsedGroup === 0) {
processedLogEntries++;
}
if (type === LogType.groupEnd) {
groupStack.pop();
if (groupStack.length > 0) {
currentList = groupStack[groupStack.length - 1].children;
} else {
currentList = rootList;
}
if (depthInCollapsedGroup > 0) depthInCollapsedGroup--;
continue;
}
let message = undefined;
if (entry.type === LogType.time) {
message = `${entry.args[0]}: ${
entry.args[1] * 1000 + entry.args[2] / 1000000
} ms`;
} else if (entry.args && entry.args.length > 0) {
message = util.format(entry.args[0], ...entry.args.slice(1));
}
/** @type {KnownStatsLoggingEntry} */
const newEntry = {
...entry,
type,
message,
trace: loggingTrace ? entry.trace : undefined,
children:
type === LogType.group || type === LogType.groupCollapsed
? []
: undefined
};
currentList.push(newEntry);
if (newEntry.children) {
groupStack.push(newEntry);
currentList = newEntry.children;
if (depthInCollapsedGroup > 0) {
depthInCollapsedGroup++;
} else if (type === LogType.groupCollapsed) {
depthInCollapsedGroup = 1;
}
}
}
let name = cachedMakePathsRelative(origin).replace(/\|/g, " ");
if (name in object.logging) {
let i = 1;
while (`${name}#${i}` in object.logging) {
i++;
}
name = `${name}#${i}`;
}
object.logging[name] = {
entries: rootList,
filteredEntries: logEntries.length - processedLogEntries,
debug: debugMode
};
}
}
},
hash: (object, compilation) => {
object.hash = compilation.hash;
@ -738,135 +876,6 @@ const SIMPLE_EXTRACTORS = {
}
}
},
logging: (object, compilation, _context, options, factory) => {
const util = require("util");
const { loggingDebug, loggingTrace, context } = options;
object.logging = {};
let acceptedTypes;
let collapsedGroups = false;
switch (options.logging) {
case "none":
acceptedTypes = new Set([]);
break;
case "error":
acceptedTypes = new Set([LogType.error]);
break;
case "warn":
acceptedTypes = new Set([LogType.error, LogType.warn]);
break;
case "info":
acceptedTypes = new Set([LogType.error, LogType.warn, LogType.info]);
break;
case "log":
acceptedTypes = new Set([
LogType.error,
LogType.warn,
LogType.info,
LogType.log,
LogType.group,
LogType.groupEnd,
LogType.groupCollapsed,
LogType.clear
]);
break;
case "verbose":
acceptedTypes = new Set([
LogType.error,
LogType.warn,
LogType.info,
LogType.log,
LogType.group,
LogType.groupEnd,
LogType.groupCollapsed,
LogType.profile,
LogType.profileEnd,
LogType.time,
LogType.status,
LogType.clear
]);
collapsedGroups = true;
break;
}
const cachedMakePathsRelative = makePathsRelative.bindContextCache(
context,
compilation.compiler.root
);
let depthInCollapsedGroup = 0;
for (const [origin, logEntries] of compilation.logging) {
const debugMode = loggingDebug.some(fn => fn(origin));
/** @type {KnownStatsLoggingEntry[]} */
const groupStack = [];
/** @type {KnownStatsLoggingEntry[]} */
const rootList = [];
let currentList = rootList;
let processedLogEntries = 0;
for (const entry of logEntries) {
let type = entry.type;
if (!debugMode && !acceptedTypes.has(type)) continue;
// Expand groups in verbose and debug modes
if (type === LogType.groupCollapsed && (debugMode || collapsedGroups))
type = LogType.group;
if (depthInCollapsedGroup === 0) {
processedLogEntries++;
}
if (type === LogType.groupEnd) {
groupStack.pop();
if (groupStack.length > 0) {
currentList = groupStack[groupStack.length - 1].children;
} else {
currentList = rootList;
}
if (depthInCollapsedGroup > 0) depthInCollapsedGroup--;
continue;
}
let message = undefined;
if (entry.type === LogType.time) {
message = `${entry.args[0]}: ${
entry.args[1] * 1000 + entry.args[2] / 1000000
} ms`;
} else if (entry.args && entry.args.length > 0) {
message = util.format(entry.args[0], ...entry.args.slice(1));
}
/** @type {KnownStatsLoggingEntry} */
const newEntry = {
...entry,
type,
message,
trace: loggingTrace ? entry.trace : undefined,
children:
type === LogType.group || type === LogType.groupCollapsed
? []
: undefined
};
currentList.push(newEntry);
if (newEntry.children) {
groupStack.push(newEntry);
currentList = newEntry.children;
if (depthInCollapsedGroup > 0) {
depthInCollapsedGroup++;
} else if (type === LogType.groupCollapsed) {
depthInCollapsedGroup = 1;
}
}
}
let name = cachedMakePathsRelative(origin).replace(/\|/g, " ");
if (name in object.logging) {
let i = 1;
while (`${name}#${i}` in object.logging) {
i++;
}
name = `${name}#${i}`;
}
object.logging[name] = {
entries: rootList,
filteredEntries: logEntries.length - processedLogEntries,
debug: debugMode
};
}
},
children: (object, compilation, context, options, factory) => {
const { type } = context;
object.children = factory.create(

View File

@ -1280,6 +1280,32 @@ asset <CLR=32,BOLD>main.js</CLR> 84 bytes <CLR=32,BOLD>[emitted]</CLR> (name: ma
webpack x.x.x compiled <CLR=32,BOLD>successfully</CLR> in X ms"
`;
exports[`StatsTestCases should print correct stats for logging-debug 1`] = `
"<i> <CLR=32,BOLD>[LogTestPlugin] Info</CLR>
asset <CLR=32,BOLD>main.js</CLR> 84 bytes <CLR=32,BOLD>[emitted]</CLR> (name: main)
<CLR=BOLD>./index.js</CLR> 1 bytes <CLR=33,BOLD>[built]</CLR> <CLR=33,BOLD>[code generated]</CLR>
<CLR=31,BOLD>DEBUG</CLR> <CLR=BOLD>LOG from ../logging/node_modules/custom-loader/index.js ../logging/node_modules/custom-loader/index.js!./index.js</CLR>
<e> <CLR=31,BOLD>An error</CLR>
<w> <CLR=33,BOLD>A warning</CLR>
<-> <CLR=36,BOLD>Unimportant</CLR>
<i> <CLR=32,BOLD>Info message</CLR>
<CLR=BOLD>Just log</CLR>
Just debug
<t> <CLR=35,BOLD>Measure: X ms</CLR>
<-> <CLR=36,BOLD>Nested</CLR>
<CLR=BOLD>Log inside collapsed group</CLR>
Trace
<t> <CLR=35,BOLD>Measure: X ms</CLR>
-------
<CLR=BOLD>After clear</CLR>
<CLR=31,BOLD>DEBUG</CLR> <CLR=BOLD>LOG from ../logging/node_modules/custom-loader/index.js Named Logger ../logging/node_modules/custom-loader/index.js!./index.js</CLR>
Message with named logger
webpack x.x.x compiled <CLR=32,BOLD>successfully</CLR> in X ms"
`;
exports[`StatsTestCases should print correct stats for max-modules 1`] = `
"asset main.js 5.43 KiB [emitted] (name: main)
31 modules

View File

View File

@ -0,0 +1,22 @@
const LogTestPlugin = require("../../helpers/LogTestPlugin");
/** @type {import("../../../").Configuration} */
module.exports = {
mode: "production",
entry: "./index",
performance: false,
module: {
rules: [
{
test: /index\.js$/,
use: require.resolve("../logging/node_modules/custom-loader")
}
]
},
plugins: [new LogTestPlugin(true)],
stats: {
colors: true,
logging: false,
loggingDebug: /custom-loader/
}
};