-
-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathlog.js
More file actions
46 lines (39 loc) · 1.36 KB
/
Copy pathlog.js
File metadata and controls
46 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// @ts-expect-error -- no published types for this entry point
import logger from "webpack/lib/logging/runtime.js";
const LOGGER_NAME = "webpack-dev-middleware";
const DEFAULT_LEVEL = "info";
/** @typedef {false | true | "none" | "error" | "warn" | "info" | "log" | "verbose"} LogLevel */
/**
* @param {LogLevel} level log level (or `false` for off, `true` for default)
*/
export function setLogLevel(level) {
logger.configureDefaultLogger({ level });
}
setLogLevel(DEFAULT_LEVEL);
const rawLog = logger.getLogger(LOGGER_NAME);
/**
* Guard a logger method: under a `require-trusted-types-for 'script'`
* Content Security Policy, tapable (bundled through webpack's logging
* runtime) cannot compile its hooks — `new Function` throws an EvalError on
* the first log call. Swallowing it keeps HMR fully functional with logging
* off instead of breaking whatever listener happened to log.
* @param {string} method logger method name
* @returns {(...args: unknown[]) => void} guarded method
*/
function guarded(method) {
return (...args) => {
try {
rawLog[method](...args);
} catch {
// Logging is unavailable (e.g. Trusted Types enforcement).
}
};
}
export const log = {
error: guarded("error"),
warn: guarded("warn"),
info: guarded("info"),
log: guarded("log"),
groupCollapsed: guarded("groupCollapsed"),
groupEnd: guarded("groupEnd"),
};