This commit is contained in:
Luiz Silva 2025-02-21 17:26:52 -03:00
parent 658a8abaeb
commit decb819207
8 changed files with 51 additions and 51 deletions

View file

@ -6,10 +6,8 @@ const LOKI_ENDPOINT = "/loki/api/v1/push"
type tipoLevel = "info" | "warn" | "error"
type tipoAmb = {
app: string
inquilino: string
usuario: string
eProducao: boolean
}
type tipoLog = {
@ -17,49 +15,51 @@ type tipoLog = {
__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 || {}
export const logger =
({ app, eProducao }: { app: string; eProducao: boolean }) =>
({ inquilino, usuario }: tipoAmb) => {
const f =
(level: tipoLevel) => async (mensagem: string, op_tipoLog?: tipoLog) => {
let { __filename, detalhes } = op_tipoLog || {}
if (!eProducao) {
app = `DEV-${app}`
}
if (!eProducao) {
app = `DEV-${app}`
}
if (__filename && typeof process != "undefined" && process.cwd()) {
__filename = __filename.replace(process.cwd(), "")
}
if (__filename && typeof process != "undefined" && process.cwd()) {
__filename = __filename.replace(process.cwd(), "")
}
const timestamp = `${Date.now()}000000`
const timestamp = `${Date.now()}000000`
const mainLog = detalhes?.length
? `${mensagem} | ${detalhes.map((d) => JSON.stringify(d)).join(" ")}`
: mensagem
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 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()}`)
}
}
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"),
}
return {
info: f("info"),
warn: f("warn"),
error: f("error"),
}
}