fix: configure debug logging in browser (#6210)
The Node debug library uses the `DEBUG` environment variable to configure what gets logged. Our browser version just logs everything; this commit changes it to look for `window.__PUPPETEER_DEBUG` and matches the behaviour accordingly: * If the value is not set, nothing is logged. * If the value is set to `*` everything is logged. * If the value is set to a string `foo`, messages with that prefix are logged. * If the value is set to a string ending in `*`, e.g. `foo*`, messages with prefixes that start with `foo` are logged.
This commit is contained in:
parent
040f37ec5d
commit
b2f69183aa
@ -19,6 +19,8 @@ import { isNode } from '../environment.js';
|
|||||||
/**
|
/**
|
||||||
* A debug function that can be used in any environment.
|
* A debug function that can be used in any environment.
|
||||||
*
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
* If used in Node, it falls back to the
|
* If used in Node, it falls back to the
|
||||||
* {@link https://www.npmjs.com/package/debug | debug module}. In the browser it
|
* {@link https://www.npmjs.com/package/debug | debug module}. In the browser it
|
||||||
* uses `console.log`.
|
* uses `console.log`.
|
||||||
@ -26,6 +28,22 @@ import { isNode } from '../environment.js';
|
|||||||
* @param prefix - this will be prefixed to each log.
|
* @param prefix - this will be prefixed to each log.
|
||||||
* @returns a function that can be called to log to that debug channel.
|
* @returns a function that can be called to log to that debug channel.
|
||||||
*
|
*
|
||||||
|
* In Node, use the `DEBUG` environment variable to control logging:
|
||||||
|
*
|
||||||
|
* ```
|
||||||
|
* DEBUG=* // logs all channels
|
||||||
|
* DEBUG=foo // logs the `foo` channel
|
||||||
|
* DEBUG=foo* // logs any channels starting with `foo`
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* In the browser, set `window.__PUPPETEER_DEBUG` to a string:
|
||||||
|
*
|
||||||
|
* ```
|
||||||
|
* window.__PUPPETEER_DEBUG='*'; // logs all channels
|
||||||
|
* window.__PUPPETEER_DEBUG='foo'; // logs the `foo` channel
|
||||||
|
* window.__PUPPETEER_DEBUG='foo*'; // logs any channels starting with `foo`
|
||||||
|
* ```
|
||||||
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```
|
* ```
|
||||||
* const log = debug('Page');
|
* const log = debug('Page');
|
||||||
@ -40,6 +58,26 @@ export const debug = (prefix: string): ((...args: unknown[]) => void) => {
|
|||||||
return require('debug')(prefix);
|
return require('debug')(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (...logArgs: unknown[]): void => {
|
||||||
|
const debugLevel = globalThis.__PUPPETEER_DEBUG as string;
|
||||||
|
if (!debugLevel) return;
|
||||||
|
|
||||||
|
const everythingShouldBeLogged = debugLevel === '*';
|
||||||
|
|
||||||
|
const prefixMatchesDebugLevel =
|
||||||
|
everythingShouldBeLogged ||
|
||||||
|
/**
|
||||||
|
* If the debug level is `foo*`, that means we match any prefix that
|
||||||
|
* starts with `foo`. If the level is `foo`, we match only the prefix
|
||||||
|
* `foo`.
|
||||||
|
*/
|
||||||
|
(debugLevel.endsWith('*')
|
||||||
|
? prefix.startsWith(debugLevel)
|
||||||
|
: prefix === debugLevel);
|
||||||
|
|
||||||
|
if (!prefixMatchesDebugLevel) return;
|
||||||
|
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
return (...logArgs: unknown[]): void => console.log(`${prefix}:`, ...logArgs);
|
console.log(`${prefix}:`, ...logArgs);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user