Files
9router/cloud/src/utils/logger.js

85 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Logger utility for worker
const LOG_LEVELS = {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3
};
const LEVEL = LOG_LEVELS.INFO;
// ANSI color codes
const COLORS = {
reset: "\x1b[0m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
cyan: "\x1b[36m"
};
function formatTime() {
return new Date().toLocaleTimeString("en-US", { hour12: false, hour: "2-digit", minute: "2-digit" });
}
function formatInline(data) {
if (!data) return "";
if (typeof data === "string") return data;
try {
return Object.entries(data).map(([k, v]) => `${k}=${v}`).join(" | ");
} catch {
return String(data);
}
}
export function debug(tag, message, data) {
if (LEVEL <= LOG_LEVELS.DEBUG) {
const extra = data ? ` | ${formatInline(data)}` : "";
console.log(`[${formatTime()}] 🔍 [${tag}] ${message}${extra}`);
}
}
export function info(tag, message, data) {
if (LEVEL <= LOG_LEVELS.INFO) {
const extra = data ? ` | ${formatInline(data)}` : "";
console.log(`[${formatTime()}] [${tag}] ${message}${extra}`);
}
}
export function warn(tag, message, data) {
if (LEVEL <= LOG_LEVELS.WARN) {
const extra = data ? ` | ${formatInline(data)}` : "";
console.warn(`${COLORS.yellow}[${formatTime()}] ⚠️ [${tag}] ${message}${extra}${COLORS.reset}`);
}
}
export function error(tag, message, data) {
if (LEVEL <= LOG_LEVELS.ERROR) {
const extra = data ? ` | ${formatInline(data)}` : "";
console.error(`${COLORS.red}[${formatTime()}] ❌ [${tag}] ${message}${extra}${COLORS.reset}`);
}
}
export function request(method, path, extra) {
const data = extra ? ` | ${formatInline(extra)}` : "";
console.log(`[${formatTime()}] 📥 ${method} ${path}${data}`);
}
export function response(status, duration, extra) {
const icon = status < 400 ? "📤" : "💥";
const data = extra ? ` | ${formatInline(extra)}` : "";
console.log(`[${formatTime()}] ${icon} ${status} (${duration}ms)${data}`);
}
export function stream(event, data) {
const extra = data ? ` | ${formatInline(data)}` : "";
console.log(`[${formatTime()}] 🌊 [STREAM] ${event}${extra}`);
}
// Mask sensitive data
export function maskKey(key) {
if (!key || key.length < 8) return "***";
return `${key.slice(0, 4)}...${key.slice(-4)}`;
}