Files
AutistMask/src/shared/log.js
sneak d24c10ca9c
Some checks failed
check / check (push) Has been cancelled
Debug-log every API request and response
Add debugFetch wrapper in log.js that logs method, URL, and body on
request, and status code on response. Replace all fetch() calls
across balances, transactions, tokens, background RPC proxy, and
settings validation with debugFetch.
2026-02-26 15:40:09 +07:00

41 lines
1.1 KiB
JavaScript

// Leveled logger. Outputs to console with [AutistMask] prefix.
// Level is DEBUG when the DEBUG constant is true, INFO otherwise.
const { DEBUG } = require("./constants");
const LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };
const threshold = DEBUG ? LEVELS.debug : LEVELS.info;
function emit(level, method, args) {
if (LEVELS[level] >= threshold) {
console[method]("[AutistMask]", ...args);
}
}
const log = {
debugf(...args) {
emit("debug", "log", args);
},
infof(...args) {
emit("info", "log", args);
},
warnf(...args) {
emit("warn", "warn", args);
},
errorf(...args) {
emit("error", "error", args);
},
};
// Fetch wrapper that debug-logs every request and response.
async function debugFetch(url, opts) {
const method = (opts && opts.method) || "GET";
const body = opts && opts.body;
log.debugf("fetch →", method, url, body || "");
const resp = await fetch(url, opts);
log.debugf("fetch ←", resp.status, url);
return resp;
}
module.exports = { log, debugFetch };