.
This commit is contained in:
parent
92ffd22f79
commit
658a8abaeb
8 changed files with 96 additions and 100 deletions
71
dist/logger.js
vendored
71
dist/logger.js
vendored
|
|
@ -7,49 +7,42 @@ exports.logger = void 0;
|
|||
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
||||
const LOKI_BASE_URL = "https://log.idz.one";
|
||||
const LOKI_ENDPOINT = "/loki/api/v1/push";
|
||||
const createLogger = (level) => {
|
||||
const sendToLoki = async (mensagem, options) => {
|
||||
if (!options.eProducao) {
|
||||
console.log(level, mensagem, options);
|
||||
return;
|
||||
const logger = ({ inquilino, app, eProducao, usuario }) => {
|
||||
const f = (level) => async (mensagem, op_tipoLog) => {
|
||||
let { __filename, detalhes } = op_tipoLog || {};
|
||||
if (!eProducao) {
|
||||
app = `DEV-${app}`;
|
||||
}
|
||||
if (__filename && typeof process != "undefined" && process.cwd()) {
|
||||
__filename = __filename.replace(process.cwd(), "");
|
||||
}
|
||||
const { app, conta, usuario, detalhes = [] } = options;
|
||||
const timestamp = `${Date.now()}000000`;
|
||||
try {
|
||||
// Formata a linha de log principal
|
||||
const mainLog = detalhes.length > 0
|
||||
? `${mensagem} | ${detalhes.map((d) => JSON.stringify(d)).join(" ")}`
|
||||
: mensagem;
|
||||
const payload = {
|
||||
stream: { app, conta, usuario, level },
|
||||
values: [
|
||||
[
|
||||
timestamp,
|
||||
mainLog, // Linha de log direta
|
||||
],
|
||||
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 response = await (0, cross_fetch_1.default)(`${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);
|
||||
],
|
||||
};
|
||||
const response = await (0, cross_fetch_1.default)(`${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 sendToLoki;
|
||||
};
|
||||
exports.logger = {
|
||||
/** 🟢 Informação geral */
|
||||
info: createLogger("info"),
|
||||
/** 🟡 Aviso/atenção necessária */
|
||||
warn: createLogger("warn"),
|
||||
/** 🔴 Erro crítico na execução */
|
||||
error: createLogger("error"),
|
||||
return {
|
||||
info: f("info"),
|
||||
warn: f("warn"),
|
||||
error: f("error"),
|
||||
};
|
||||
};
|
||||
exports.logger = logger;
|
||||
//# sourceMappingURL=logger.js.map
|
||||
Loading…
Add table
Add a link
Reference in a new issue