allow to configure the infrastructure logger

rename includeDebugLogging to loggingDebug
rename logToConsole to createConsoleLogger
This commit is contained in:
Tobias Koppers 2019-07-22 08:23:40 +02:00
parent 183ddad98e
commit d532c4f24f
10 changed files with 112 additions and 48 deletions

View File

@ -75,6 +75,16 @@ export type ExternalItem =
* via the `definition` "ArrayOfStringValues".
*/
export type ArrayOfStringValues = string[];
/**
* This interface was referenced by `WebpackOptions`'s JSON-Schema
* via the `definition` "FilterTypes".
*/
export type FilterTypes = FilterItemTypes | FilterItemTypes[];
/**
* This interface was referenced by `WebpackOptions`'s JSON-Schema
* via the `definition` "FilterItemTypes".
*/
export type FilterItemTypes = RegExp | string | ((value: string) => boolean);
/**
* One or multiple rule conditions
*
@ -235,16 +245,6 @@ export type WebpackPluginFunction = (
* via the `definition` "RuleSetRules".
*/
export type RuleSetRules = RuleSetRule[];
/**
* This interface was referenced by `WebpackOptions`'s JSON-Schema
* via the `definition` "FilterTypes".
*/
export type FilterTypes = FilterItemTypes | FilterItemTypes[];
/**
* This interface was referenced by `WebpackOptions`'s JSON-Schema
* via the `definition` "FilterItemTypes".
*/
export type FilterItemTypes = RegExp | string | Function;
export interface WebpackOptions {
/**
@ -293,6 +293,19 @@ export interface WebpackOptions {
* Specify dependencies that shouldn't be resolved by webpack, but should become dependencies of the resulting bundle. The kind of the dependency depends on `output.libraryTarget`.
*/
externals?: Externals;
/**
* Options for infrastructure level logging
*/
infrastructureLogging?: {
/**
* Enable debug logging for specific loggers
*/
debug?: FilterTypes | boolean;
/**
* Log level
*/
level?: "error" | "warn" | "info" | "log" | "verbose";
};
/**
* Custom values available in the loader context.
*/
@ -1343,14 +1356,14 @@ export interface StatsOptions {
* add the hash of the compilation
*/
hash?: boolean;
/**
* Include debug logging of specified modules (plugins/loaders). Filters can be Strings, RegExps or Functions
*/
includeDebugLogging?: FilterTypes;
/**
* add logging output
*/
logging?: boolean | ("error" | "warn" | "info" | "log" | "verbose");
/**
* Include debug logging of specified loggers (i. e. for plugins or loaders). Filters can be Strings, RegExps or Functions
*/
loggingDebug?: FilterTypes | boolean;
/**
* add stack traces to logging output
*/

View File

@ -28,16 +28,10 @@ const RequestShortener = require("./RequestShortener");
const { makePathsRelative } = require("./util/identifier");
const ConcurrentCompilationError = require("./ConcurrentCompilationError");
const { Logger } = require("./logging/Logger");
const logToConsole = require("./logging/logToConsole");
/** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
const infrastructureLogger = logToConsole({
level: "log",
debug: false
});
/**
* @typedef {Object} CompilationParams
* @property {NormalModuleFactory} normalModuleFactory
@ -147,6 +141,8 @@ class Compiler extends Tapable {
/** @type {ResolverFactory} */
this.resolverFactory = new ResolverFactory();
this.infrastructureLogger = undefined;
// TODO remove in webpack 5
this.resolvers = {
normal: {
@ -226,7 +222,9 @@ class Compiler extends Tapable {
}
}
if (this.hooks.log.call(name, type, args) === undefined) {
infrastructureLogger(name, type, args);
if (this.infrastructureLogger !== undefined) {
this.infrastructureLogger(name, type, args);
}
}
});
}

View File

@ -207,8 +207,8 @@ class Stats {
options.loggingTrace,
!forToString
);
const includeDebugLogging = []
.concat(optionsOrFallback(options.includeDebugLogging, []))
const loggingDebug = []
.concat(optionsOrFallback(options.loggingDebug, []))
.map(testAgainstGivenOption);
const excludeModules = []
@ -758,7 +758,7 @@ class Stats {
break;
}
for (const [origin, logEntries] of compilation.logging) {
const debugMode = includeDebugLogging.some(fn => fn(origin));
const debugMode = loggingDebug.some(fn => fn(origin));
let collapseCounter = 0;
let processedLogEntries = logEntries;
if (!debugMode) {

View File

@ -363,6 +363,12 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
options.resolveLoader.plugins.length > 0
);
});
this.set("infrastructureLogging", "call", value =>
Object.assign({}, value)
);
this.set("infrastructureLogging.level", "info");
this.set("infrastructureLogging.debug", false);
}
}

View File

@ -8,19 +8,20 @@
const { LogType } = require("./Logger");
/** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
/** @typedef {import("../../declarations/WebpackOptions").FilterTypes} FilterTypes */
/** @typedef {import("../../declarations/WebpackOptions").FilterItemTypes} FilterItemTypes */
/** @typedef {function(string): boolean} FilterFunction */
/** @typedef {RegExp|FilterFunction|string|boolean} FilterInputItem */
/** @typedef {FilterInputItem[]|FilterInputItem} FilterInput */
/**
* @typedef {Object} LoggerOptions
* @property {false|true|"error"|"warn"|"info"|"log"|"verbose"} options.level loglevel
* @property {FilterInput} options.debug filter for debug logging
* @property {FilterTypes|boolean} options.debug filter for debug logging
*/
/**
* @param {FilterInputItem} item an input item
* @returns {function(string): boolean} filter funtion
* @param {FilterItemTypes} item an input item
* @returns {FilterFunction} filter funtion
*/
const filterToFunction = item => {
if (typeof item === "string") {
@ -61,9 +62,12 @@ const LogLevel = {
* @returns {function(string, LogTypeEnum, any[]): void} logging function
*/
module.exports = ({ level = "info", debug = false }) => {
const debugFilters = /** @type {FilterInputItem[]} */ ([])
.concat(debug)
.map(filterToFunction);
const debugFilters =
typeof debug === "boolean"
? [() => debug]
: /** @type {FilterItemTypes[]} */ ([])
.concat(debug)
.map(filterToFunction);
/** @type {number} */
const loglevel = LogLevel[`${level}`] || 0;

View File

@ -1,13 +1,13 @@
const SyncBailHook = require("tapable/lib/SyncBailHook");
const { Logger } = require("./Logger");
const logToConsole = require("./logToConsole");
const createConsoleLogger = require("./createConsoleLogger");
/** @type {logToConsole.LoggerOptions} */
/** @type {createConsoleLogger.LoggerOptions} */
let currentDefaultLoggerOptions = {
level: "info",
debug: false
};
let currentDefaultLogger = logToConsole(currentDefaultLoggerOptions);
let currentDefaultLogger = createConsoleLogger(currentDefaultLoggerOptions);
/**
* @param {string} name name of the logger
@ -22,12 +22,12 @@ exports.getLogger = name => {
};
/**
* @param {logToConsole.LoggerOptions} options new options, merge with old options
* @param {createConsoleLogger.LoggerOptions} options new options, merge with old options
* @returns {void}
*/
exports.configureDefaultLogger = options => {
Object.assign(currentDefaultLoggerOptions, options);
currentDefaultLogger = logToConsole(currentDefaultLoggerOptions);
currentDefaultLogger = createConsoleLogger(currentDefaultLoggerOptions);
};
exports.hooks = {

View File

@ -8,9 +8,23 @@ const NodeWatchFileSystem = require("./NodeWatchFileSystem");
const NodeOutputFileSystem = require("./NodeOutputFileSystem");
const NodeJsInputFileSystem = require("enhanced-resolve/lib/NodeJsInputFileSystem");
const CachedInputFileSystem = require("enhanced-resolve/lib/CachedInputFileSystem");
const createConsoleLogger = require("../logging/createConsoleLogger");
class NodeEnvironmentPlugin {
constructor(options) {
this.options = options || {};
}
apply(compiler) {
compiler.infrastructureLogger = createConsoleLogger(
Object.assign(
{
level: "info",
debug: false
},
this.options.infrastructureLogging
)
);
compiler.inputFileSystem = new CachedInputFileSystem(
new NodeJsInputFileSystem(),
60000

View File

@ -40,7 +40,9 @@ const webpack = (options, callback) => {
compiler = new Compiler(options.context);
compiler.options = options;
new NodeEnvironmentPlugin().apply(compiler);
new NodeEnvironmentPlugin({
infrastructureLogging: options.infrastructureLogging
}).apply(compiler);
if (options.plugins && Array.isArray(options.plugins)) {
for (const plugin of options.plugins) {
if (typeof plugin === "function") {

View File

@ -166,7 +166,7 @@
},
{
"instanceof": "Function",
"tsType": "Function"
"tsType": "((value: string) => boolean)"
}
]
},
@ -1822,14 +1822,6 @@
"description": "add the hash of the compilation",
"type": "boolean"
},
"includeDebugLogging": {
"description": "Include debug logging of specified modules (plugins/loaders). Filters can be Strings, RegExps or Functions",
"anyOf": [
{
"$ref": "#/definitions/FilterTypes"
}
]
},
"logging": {
"description": "add logging output",
"anyOf": [
@ -1843,6 +1835,18 @@
}
]
},
"loggingDebug": {
"description": "Include debug logging of specified loggers (i. e. for plugins or loaders). Filters can be Strings, RegExps or Functions",
"anyOf": [
{
"$ref": "#/definitions/FilterTypes"
},
{
"description": "Enable/Disable debug logging for all loggers",
"type": "boolean"
}
]
},
"loggingTrace": {
"description": "add stack traces to logging output",
"type": "boolean"
@ -2021,6 +2025,29 @@
}
]
},
"infrastructureLogging": {
"description": "Options for infrastructure level logging",
"type": "object",
"additionalProperties": false,
"properties": {
"debug": {
"description": "Enable debug logging for specific loggers",
"anyOf": [
{
"$ref": "#/definitions/FilterTypes"
},
{
"description": "Enable/Disable debug logging for all loggers",
"type": "boolean"
}
]
},
"level": {
"description": "Log level",
"enum": ["error", "warn", "info", "log", "verbose"]
}
}
},
"loader": {
"description": "Custom values available in the loader context.",
"type": "object"

View File

@ -30,7 +30,7 @@ module.exports = {
stats: {
colors: true,
logging: true,
includeDebugLogging: "custom-loader",
loggingDebug: "custom-loader",
loggingTrace: true
}
};