Logging output is an additional way to display messages to the end users.
Webpack logger is available to loaders and plugins. Emitting as part of the Stats and configured by the user in webpack configuration.
Benefits of custom logging API in webpack:
stats.json
By introducing webpack logging API we hope to unify the way webpack plugins and loaders emit logs and allow better ways to inspect build problems. Integrated logging solution supports plugins and loaders developers by improving their development experience. Paves the way for non-CLI webpack solutions like dashboards or other UIs.
my-webpack-plugin.js
const PLUGIN_NAME = 'my-webpack-plugin';
export class MyWebpackPlugin {
apply(compiler) {
// you can access Logger from compiler
const logger = compiler.getInfrastructureLogger(PLUGIN_NAME);
logger.log('log from compiler');
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
// you can also access Logger from compilation
const logger = compilation.getLogger(PLUGIN_NAME);
logger.info('log from compilation');
});
}
}
my-webpack-loader.js
module.exports = function (source) {
// you can get Logger with `this.getLogger` in your webpack loaders
const logger = this.getLogger('my-webpack-loader');
logger.info('hello Logger');
return source;
};
As you can see from the above my-webpack-plugin.js
example, there're two types of logging methods,
compilation.getLogger
compiler.getInfrastructureLogger
It's advised to use compilation.getLogger
when plugin/logging is related to the compilation, and they will be stored within the stats. For logging that happens outside the compilation cycle, use compiler.getInfrastructureLogger
instead.
logger.error(...)
: for error messageslogger.warn(...)
: for warningslogger.info(...)
: for important information messages. These messages are displayed by default. Only use this for messages that the user really needs to seelogger.log(...)
: for unimportant information messages. These messages are displayed only when user had opted-in to see themlogger.debug(...)
: for debugging information. These messages are displayed only when user had opted-in to see debug logging for specific moduleslogger.trace()
: to display a stack trace. Displayed like logger.debug
logger.group(...)
: to group messages. Displayed collapsed like logger.log
logger.groupEnd()
: to end a logging grouplogger.groupCollapsed(...)
: to group messages together. Displayed collapsed like logger.log
. Displayed expanded when logging level is set to 'verbose'
or 'debug'
.logger.status
: writes a temporary message, setting a new status, overrides the previous onelogger.clear()
: to print a horizontal line. Displayed like logger.log
logger.profile(...)
, logger.profileEnd(...)
: to capture a profile. Delegated to console.profile
when supportedRuntime logger API is only intended to be used as a development tool, it is not intended to be included in production mode.
const logging = require('webpack/lib/logging/runtime')
: to use the logger in runtime, require it directly from webpacklogging.getLogger('name')
: to get individual logger by namelogging.configureDefaultLogger(...)
: to override the default logger.const logging = require('webpack/lib/logging/runtime');
logging.configureDefaultLogger({
level: 'log',
debug: /something/,
});
logging.hooks.log
: to apply Plugins to the runtime logger