import { logToDb } from "../db"; function safeStringify(obj: unknown) { const seen = new WeakSet(); return JSON.stringify(obj, (key, value) => { if (typeof value === "object" && value !== null) { if (seen.has(value)) { return "[Circular]"; } seen.add(value); } if (typeof value === "function") { return `[Function: ${value.name || "anonymous"}]`; } return value; }); } function formatMessage(message: string, ...args: unknown[]): string { const prefix = '[TimeSafari]'; const argsString = args.length > 0 ? " - " + safeStringify(args) : ""; return `${prefix} ${message}${argsString}`; } export const logger = { log: (message: string, ...args: unknown[]) => { if (process.env.NODE_ENV !== "production") { const formattedMessage = formatMessage(message, ...args); console.log(formattedMessage); logToDb(message + (args.length > 0 ? " - " + safeStringify(args) : "")); } }, warn: (message: string, ...args: unknown[]) => { if (process.env.NODE_ENV !== "production") { const formattedMessage = formatMessage(message, ...args); console.warn(formattedMessage); logToDb(message + (args.length > 0 ? " - " + safeStringify(args) : "")); } }, error: (message: string, ...args: unknown[]) => { const formattedMessage = formatMessage(message, ...args); console.error(formattedMessage); logToDb(message + (args.length > 0 ? " - " + safeStringify(args) : "")); }, }; export function log(...args: any[]) { const message = formatMessage(args[0], ...args.slice(1)); console.log(message); } export function error(...args: any[]) { const message = formatMessage(args[0], ...args.slice(1)); console.error(message); } export function warn(...args: any[]) { const message = formatMessage(args[0], ...args.slice(1)); console.warn(message); } export function info(...args: any[]) { const message = formatMessage(args[0], ...args.slice(1)); console.info(message); } export function debug(...args: any[]) { const message = formatMessage(args[0], ...args.slice(1)); console.debug(message); }