This commit is contained in:
2026-02-04 12:51:41 +00:00
parent 4fdbfb0fb3
commit f1e13f87f6
19 changed files with 722 additions and 67 deletions

57
lib/server-logger.ts Normal file
View File

@@ -0,0 +1,57 @@
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<void> {
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);
}
}