import { fetchMutation } from "convex/nextjs"; import { api } from "@/convex/_generated/api"; import type { Id } from "@/convex/_generated/dataModel"; type LogLevel = "debug" | "info" | "warn" | "error"; type LogParams = { level: LogLevel; message: string; labels: string[]; payload?: unknown; source?: string; requestId?: string; projectId?: Id<"projects">; token?: string; }; function writeConsole(level: LogLevel, message: string, payload?: unknown) { const tag = `[${level}]`; if (payload === undefined) { if (level === "error") { console.error(tag, message); return; } if (level === "warn") { console.warn(tag, message); return; } console.log(tag, message); return; } if (level === "error") { console.error(tag, message, payload); return; } if (level === "warn") { console.warn(tag, message, payload); return; } console.log(tag, message, payload); } export async function logServer({ token, ...args }: LogParams): Promise { writeConsole(args.level, args.message, args.payload); try { if (token) { await fetchMutation(api.logs.createLog, args, { token }); return; } await fetchMutation(api.logs.createLog, args); } catch (error) { console.error("[logger] Failed to write log", error); } }