Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate to consume webpack built-in logger #361

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ class BundleAnalyzerPlugin {
};

this.server = null;
this.logger = new Logger(this.opts.logLevel);
}

apply(compiler) {
this.compiler = compiler;

// Respect default logging level from webpack
const defaultLoggerLevel = compiler.options.infrastructureLogging && compiler.options.infrastructureLogging.level ?
compiler.options.infrastructureLogging.level : this.opts.logLevel;
this.logger = compiler.getInfrastructureLogger ?
compiler.getInfrastructureLogger('webpack-bundle-analyzer') : new Logger(defaultLoggerLevel);

const done = (stats, callback) => {
callback = callback || (() => {});

Expand Down
22 changes: 20 additions & 2 deletions src/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ const LEVELS = [
'silent'
];

const loggerMethods = [
'log',
'trace',
'group',
'groupEnd',
'groupEndCollapsed',
'status',
'clear',
'profile'
];

const LEVEL_TO_CONSOLE_METHOD = new Map([
['debug', 'log'],
['info', 'log'],
['warn', 'log']
]);

class Logger {
const webpackLogger = require('webpack/lib/logging/runtime');

if (webpackLogger.getLogger) LEVELS.push(...loggerMethods);

class Logger {
static levels = LEVELS;
static defaultLevel = 'info';

Expand All @@ -35,7 +49,11 @@ class Logger {
}

_log(level, ...args) {
console[LEVEL_TO_CONSOLE_METHOD.get(level) || level](...args);
if (webpackLogger.getLogger) {
webpackLogger.getLogger('webpack-bundle-analyzer')[level](...args);
} else {
console[LEVEL_TO_CONSOLE_METHOD.get(level) || level](...args);
}
}

};
Expand Down