feat(runtime): add WorkerLogLevel (#19316)

This is not really used yet, but provides some infrastructure for doing
more fine grained logging in JS. I will add warn messages in a future
PR.
This commit is contained in:
David Sherret 2023-05-30 11:34:50 -04:00 committed by GitHub
parent acc6cdc0b1
commit 3b69d238cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 23 deletions

View file

@ -5,18 +5,27 @@ const {
Promise,
SafeArrayIterator,
} = primordials;
let logDebug = false;
// WARNING: Keep this in sync with Rust (search for LogLevel)
const LogLevel = {
Error: 1,
Warn: 2,
Info: 3,
Debug: 4,
};
let logLevel = 3;
let logSource = "JS";
function setLogDebug(debug, source) {
logDebug = debug;
function setLogLevel(level, source) {
logLevel = level;
if (source) {
logSource = source;
}
}
function log(...args) {
if (logDebug) {
if (logLevel >= LogLevel.Debug) {
// if we destructure `console` off `globalThis` too early, we don't bind to
// the right console, therefore we don't log anything out.
globalThis.console.error(
@ -80,6 +89,6 @@ export {
log,
nonEnumerable,
readOnly,
setLogDebug,
setLogLevel,
writable,
};