_comuns/dist/logger.js
2025-02-22 10:38:12 -03:00

48 lines
No EOL
1.8 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const variaveisComuns_1 = require("./variaveisComuns");
const LOKI_BASE_URL = "https://log.idz.one";
const LOKI_ENDPOINT = "/loki/api/v1/push";
const logger = ({ app, eProducao }) => ({ inquilino, usuario }) => async (level, mensagem, op_tipoLog) => {
let { __filename, detalhes, local } = op_tipoLog || {};
if (!eProducao) {
app = `DEV-${app}`;
}
if (__filename && typeof process != "undefined" && process.cwd()) {
__filename = __filename.replace(process.cwd(), "");
}
if (local) {
detalhes = [`${(0, variaveisComuns_1.nomeVariavel)({ local })}="${local}"`];
}
if (__filename) {
detalhes = [`${(0, variaveisComuns_1.nomeVariavel)({ __filename })}="${__filename}"`];
}
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,
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()}`);
}
};
exports.logger = logger;
//# sourceMappingURL=logger.js.map