.
This commit is contained in:
parent
658a8abaeb
commit
decb819207
8 changed files with 51 additions and 51 deletions
|
|
@ -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"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { logger } from "./logger"
|
||||
|
||||
const { error, info } = logger({
|
||||
const l = logger({ app: "teste", eProducao: true })
|
||||
|
||||
const { error, info } = l({
|
||||
inquilino: "conta_1",
|
||||
app: "teste",
|
||||
eProducao: true,
|
||||
usuario: "pedrinho",
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue