70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
import crossFetch from "cross-fetch";
|
|
import { nomeVariavel } from "./variaveisComuns";
|
|
const LOKI_BASE_URL = "https://log.idz.one";
|
|
const LOKI_ENDPOINT = "/loki/api/v1/push";
|
|
const postLogger = async ({
|
|
objeto
|
|
}) => {
|
|
const response = await crossFetch(`${LOKI_BASE_URL}${LOKI_ENDPOINT}`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(objeto)
|
|
}).catch((a) => a);
|
|
if (!response.ok) {
|
|
return [objeto, `Erro ${response.status}: ${await response?.text?.()}`];
|
|
}
|
|
return [objeto];
|
|
};
|
|
let cwd = "";
|
|
const defineCwd = (novoCwd) => {
|
|
cwd = novoCwd;
|
|
};
|
|
const logger = ({ app: app_e, eProducao, parametros: parametrosAmbiente }) => ({ inquilino, usuario, parametros: parametrosSessao }) => async (level, mensagem, op_tipoLog) => {
|
|
let {
|
|
__filename,
|
|
detalhes,
|
|
local,
|
|
parametros: parametrosLog
|
|
} = op_tipoLog || {};
|
|
const app = `${eProducao ? "" : "DEV-"}${app_e}`;
|
|
if (cwd && __filename) {
|
|
__filename = __filename.replace(cwd, "");
|
|
}
|
|
if (local) {
|
|
detalhes = [`${nomeVariavel({ local })}="${local}"`, ...detalhes || []];
|
|
}
|
|
if (__filename) {
|
|
detalhes = [
|
|
`${nomeVariavel({ __filename })}="${__filename}"`,
|
|
...detalhes || []
|
|
];
|
|
}
|
|
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,
|
|
...parametrosAmbiente || {},
|
|
...parametrosSessao || {},
|
|
...parametrosLog || {}
|
|
},
|
|
values: [
|
|
[
|
|
timestamp,
|
|
mainLog
|
|
// Linha de log direta
|
|
]
|
|
]
|
|
};
|
|
const objeto = { streams: [payload] };
|
|
const response = await postLogger({ objeto });
|
|
return response;
|
|
};
|
|
export {
|
|
defineCwd,
|
|
logger,
|
|
postLogger
|
|
};
|