melhorias em logger
This commit is contained in:
parent
36d3cc6aa4
commit
79a83fe158
8 changed files with 79 additions and 97 deletions
|
|
@ -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()}`)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue