This commit is contained in:
Luiz Silva 2025-02-21 17:20:13 -03:00
parent 92ffd22f79
commit 658a8abaeb
8 changed files with 96 additions and 100 deletions

View file

@ -3,45 +3,41 @@ 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"
type tipoLevel = "info" | "warn" | "error"
interface LogOptions {
app?: string
conta?: string
usuario?: string
detalhes?: unknown[]
type tipoAmb = {
app: string
inquilino: string
usuario: string
eProducao: boolean
}
interface LokiStream {
stream: {
app?: string
conta?: string
usuario?: string
level: LogLevel
}
values: Array<[string, string]>
type tipoLog = {
detalhes?: unknown[]
__filename?: string
}
const createLogger = (level: LogLevel) => {
const sendToLoki = async (mensagem: string, options: LogOptions) => {
if (!options.eProducao) {
console.log(level, mensagem, options)
return
}
export const logger = ({ inquilino, app, eProducao, usuario }: tipoAmb) => {
const f =
(level: tipoLevel) => async (mensagem: string, op_tipoLog?: tipoLog) => {
let { __filename, detalhes } = op_tipoLog || {}
const { app, conta, usuario, detalhes = [] } = options
const timestamp = `${Date.now()}000000`
if (!eProducao) {
app = `DEV-${app}`
}
try {
// Formata a linha de log principal
const mainLog =
detalhes.length > 0
? `${mensagem} | ${detalhes.map((d) => JSON.stringify(d)).join(" ")}`
: mensagem
if (__filename && typeof process != "undefined" && process.cwd()) {
__filename = __filename.replace(process.cwd(), "")
}
const payload: LokiStream = {
stream: { app, conta, usuario, level },
const timestamp = `${Date.now()}000000`
const mainLog = detalhes?.length
? `${mensagem} | ${detalhes.map((d) => JSON.stringify(d)).join(" ")}`
: mensagem
const payload = {
stream: { app, inquilino, usuario, level },
values: [
[
timestamp,
@ -59,19 +55,11 @@ const createLogger = (level: LogLevel) => {
if (!response.ok) {
throw new Error(`Erro ${response.status}: ${await response.text()}`)
}
} catch (error) {
console.error("[Logger] Falha no envio:", error)
}
return {
info: f("info"),
warn: f("warn"),
error: f("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"),
}