import crossFetch from "cross-fetch" const LOKI_BASE_URL = "https://log.idz.one" const LOKI_ENDPOINT = "/loki/api/v1/push" type tipoLevel = "info" | "warn" | "error" type tipoAmb = { app: string inquilino: string usuario: string eProducao: boolean } type tipoLog = { detalhes?: unknown[] __filename?: string } export const logger = ({ inquilino, app, eProducao, usuario }: tipoAmb) => { const f = (level: tipoLevel) => async (mensagem: string, op_tipoLog?: tipoLog) => { let { __filename, detalhes } = op_tipoLog || {} if (!eProducao) { app = `DEV-${app}` } if (__filename && typeof process != "undefined" && process.cwd()) { __filename = __filename.replace(process.cwd(), "") } const timestamp = `${Date.now()}000000` const mainLog = detalhes?.length ? `${mensagem} | ${detalhes.map((d) => JSON.stringify(d)).join(" ")}` : mensagem const payload = { stream: { app, inquilino, usuario, level }, values: [ [ timestamp, mainLog, // Linha de log direta ], ], } const response = await crossFetch(`${LOKI_BASE_URL}${LOKI_ENDPOINT}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ streams: [payload] }), }) if (!response.ok) { throw new Error(`Erro ${response.status}: ${await response.text()}`) } } return { info: f("info"), warn: f("warn"), error: f("error"), } }