From d532c4f24f6ca6b04335096095bc02e531e8a068 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 22 Jul 2019 08:23:40 +0200 Subject: [PATCH] allow to configure the infrastructure logger rename includeDebugLogging to loggingDebug rename logToConsole to createConsoleLogger --- declarations/WebpackOptions.d.ts | 41 +++++++++++------ lib/Compiler.js | 12 +++-- lib/Stats.js | 6 +-- lib/WebpackOptionsDefaulter.js | 6 +++ ...logToConsole.js => createConsoleLogger.js} | 20 +++++---- lib/logging/runtime.js | 10 ++--- lib/node/NodeEnvironmentPlugin.js | 14 ++++++ lib/webpack.js | 4 +- schemas/WebpackOptions.json | 45 +++++++++++++++---- test/statsCases/logging/webpack.config.js | 2 +- 10 files changed, 112 insertions(+), 48 deletions(-) rename lib/logging/{logToConsole.js => createConsoleLogger.js} (90%) diff --git a/declarations/WebpackOptions.d.ts b/declarations/WebpackOptions.d.ts index e89c2422ed4..43a86cc4753 100644 --- a/declarations/WebpackOptions.d.ts +++ b/declarations/WebpackOptions.d.ts @@ -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 */ diff --git a/lib/Compiler.js b/lib/Compiler.js index 8d5caf6e934..b22a4033de2 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -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); + } } }); } diff --git a/lib/Stats.js b/lib/Stats.js index f023f511cfa..473602d52c7 100644 --- a/lib/Stats.js +++ b/lib/Stats.js @@ -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) { diff --git a/lib/WebpackOptionsDefaulter.js b/lib/WebpackOptionsDefaulter.js index 53adbf3058b..c11529cb30e 100644 --- a/lib/WebpackOptionsDefaulter.js +++ b/lib/WebpackOptionsDefaulter.js @@ -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); } } diff --git a/lib/logging/logToConsole.js b/lib/logging/createConsoleLogger.js similarity index 90% rename from lib/logging/logToConsole.js rename to lib/logging/createConsoleLogger.js index 439326ee76d..82ac0173a2a 100644 --- a/lib/logging/logToConsole.js +++ b/lib/logging/createConsoleLogger.js @@ -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; diff --git a/lib/logging/runtime.js b/lib/logging/runtime.js index f4ea3119c44..7b4bbf7b22f 100644 --- a/lib/logging/runtime.js +++ b/lib/logging/runtime.js @@ -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 = { diff --git a/lib/node/NodeEnvironmentPlugin.js b/lib/node/NodeEnvironmentPlugin.js index 3f76bffa533..8a236ea85a5 100644 --- a/lib/node/NodeEnvironmentPlugin.js +++ b/lib/node/NodeEnvironmentPlugin.js @@ -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 diff --git a/lib/webpack.js b/lib/webpack.js index 3e121f41fa9..80f810b35f2 100644 --- a/lib/webpack.js +++ b/lib/webpack.js @@ -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") { diff --git a/schemas/WebpackOptions.json b/schemas/WebpackOptions.json index 4de118a5559..9107d4eba8c 100644 --- a/schemas/WebpackOptions.json +++ b/schemas/WebpackOptions.json @@ -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" diff --git a/test/statsCases/logging/webpack.config.js b/test/statsCases/logging/webpack.config.js index ed10f829a2f..ce49b7dc908 100644 --- a/test/statsCases/logging/webpack.config.js +++ b/test/statsCases/logging/webpack.config.js @@ -30,7 +30,7 @@ module.exports = { stats: { colors: true, logging: true, - includeDebugLogging: "custom-loader", + loggingDebug: "custom-loader", loggingTrace: true } };