adicionado integração com loki
This commit is contained in:
parent
745c04819b
commit
3c8aa13a5e
14 changed files with 202 additions and 7 deletions
71
src/logger.ts
Normal file
71
src/logger.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import crossFetch from "cross-fetch"
|
||||
|
||||
const LOKI_BASE_URL = "https://log.idz.one"
|
||||
const LOKI_ENDPOINT = "/loki/api/v1/push"
|
||||
|
||||
type LogLevel = "info" | "warn" | "error"
|
||||
|
||||
interface LogOptions {
|
||||
app?: string
|
||||
conta?: string
|
||||
usuario?: string
|
||||
detalhes?: unknown[]
|
||||
}
|
||||
|
||||
interface LokiStream {
|
||||
stream: {
|
||||
app?: string
|
||||
conta?: string
|
||||
usuario?: string
|
||||
level: LogLevel
|
||||
}
|
||||
values: Array<[string, string]>
|
||||
}
|
||||
|
||||
const createLogger = (level: LogLevel) => {
|
||||
const sendToLoki = async (message: string, options: LogOptions = {}) => {
|
||||
const { app, conta, usuario, detalhes = [] } = options
|
||||
const timestamp = `${Date.now()}000000`
|
||||
|
||||
try {
|
||||
// Formata a linha de log principal
|
||||
const mainLog =
|
||||
detalhes.length > 0
|
||||
? `${message} | ${detalhes.map((d) => JSON.stringify(d)).join(" ")}`
|
||||
: message
|
||||
|
||||
const payload: LokiStream = {
|
||||
stream: { app, conta, 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()}`)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[Logger] Falha no envio:", error)
|
||||
}
|
||||
}
|
||||
|
||||
return sendToLoki
|
||||
}
|
||||
|
||||
export const logger = {
|
||||
/** 🟢 Informação geral */
|
||||
info: createLogger("info"),
|
||||
/** 🟡 Aviso/atenção necessária */
|
||||
warn: createLogger("warn"),
|
||||
/** 🔴 Erro crítico na execução */
|
||||
error: createLogger("error"),
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue