Files
SanatiLeads/convex/logs.ts
2026-02-04 12:51:41 +00:00

33 lines
935 B
TypeScript

import { mutation } from "./_generated/server";
import { v } from "convex/values";
import { getAuthUserId } from "@convex-dev/auth/server";
export const createLog = mutation({
args: {
level: v.union(
v.literal("debug"),
v.literal("info"),
v.literal("warn"),
v.literal("error")
),
message: v.string(),
labels: v.array(v.string()),
payload: v.optional(v.any()),
source: v.optional(v.string()),
requestId: v.optional(v.string()),
projectId: v.optional(v.id("projects")),
},
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
const base = {
...args,
createdAt: Date.now(),
};
if (userId) {
await ctx.db.insert("logs", { ...base, userId });
return;
}
await ctx.db.insert("logs", base);
},
});