melhorias em logger

This commit is contained in:
Luiz Silva 2025-02-22 10:38:12 -03:00
parent 36d3cc6aa4
commit 79a83fe158
8 changed files with 79 additions and 97 deletions

View file

@ -19,56 +19,48 @@ type tipoLog = {
export const logger =
({ app, eProducao }: { app: string; eProducao: boolean }) =>
({ inquilino, usuario }: tipoOpSessao) => {
const f =
(level: tipoLevel) => async (mensagem: string, op_tipoLog?: tipoLog) => {
let { __filename, detalhes, local } = op_tipoLog || {}
({ inquilino, usuario }: tipoOpSessao) =>
async (level: tipoLevel, mensagem: string, op_tipoLog?: tipoLog) => {
let { __filename, detalhes, local } = 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 (local) {
detalhes = [`${nomeVariavel({ local })}="${local}"`]
}
if (__filename && typeof process != "undefined" && process.cwd()) {
__filename = __filename.replace(process.cwd(), "")
}
if (local) {
detalhes = [`${nomeVariavel({ local })}="${local}"`]
}
if (__filename) {
detalhes = [`${nomeVariavel({ __filename })}="${__filename}"`]
}
if (__filename) {
detalhes = [`${nomeVariavel({ __filename })}="${__filename}"`]
}
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] }),
})
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"),
if (!response.ok) {
throw new Error(`Erro ${response.status}: ${await response.text()}`)
}
}

View file

@ -2,10 +2,10 @@ import { logger } from "./logger"
const l = logger({ app: "teste", eProducao: true })
const { error, info } = l({
const lg = l({
inquilino: "conta_1",
usuario: "pedrinho",
})
error("Deu Ruim")
info("Deu Bom", { __filename })
lg("error", "Deu Ruim")
lg("info", "Deu Bom", { __filename })