// 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 };