console-streamer
Version:
A reusable FE logger package built with React + Vite.
151 lines (150 loc) • 3.93 kB
JavaScript
import L from "axios";
import T from "moment";
let u = "", f = "development", d = "Not_Authenticated_Logs", p = "DEFAULT", E = "", y = 20, A = 1e4, m = "DEFAULT";
const g = console.log, I = console.warn, c = console.error, N = console.info, R = console.debug;
let t = [];
async function h(e) {
if (e.length !== 0) {
g(`Attempting to send ${e.length} log(s) to server...`);
try {
const n = await L.post(u, {
logs: e,
env: f,
user: d,
tenantId: p,
bucketName: E,
docType: m
});
!n.status || n.status < 200 || n.status >= 300 ? c(
`Failed to send logs to server. Status: ${n.status}`
) : g(`Successfully sent ${e.length} log(s).`);
} catch (n) {
c("Error sending logs to server:", n);
}
}
}
function _(e) {
return e.map((n) => {
if (typeof n == "object" && n !== null)
try {
return JSON.stringify(n, (o) => {
if (typeof o == "bigint")
return `${o.toString()}n`;
if (o instanceof Error)
return {
message: o.message,
stack: o.stack,
name: o.name
};
const i = [];
return JSON.stringify(o, (s, r) => {
if (console.log(s), typeof r == "object" && r !== null) {
if (i.includes(r))
return;
i.push(r);
}
return r;
});
});
} catch (o) {
return console.log(o), "Unserializable Object";
}
return String(n);
}).join(" ");
}
function a(e, n, o) {
const i = _(n), s = {
timestamp: T().utc().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
user: d,
level: e,
message: i,
userAgent: typeof navigator < "u" ? navigator.userAgent : void 0,
url: typeof window < "u" ? window.location.href : void 0,
environment: f
// Include environment from config
};
if (o instanceof Error ? s.stackTrace = o.stack || "N/A" : typeof o == "string" && (s.message += `
Details: ${o}`), t.push(s), t.length >= y) {
const r = [...t];
t = [], h(r);
}
}
console.warn = (...e) => {
I.apply(console, e), a("WARN", e);
};
console.error = (...e) => {
c.apply(console, e);
const n = e.find((o) => o instanceof Error);
a("ERROR", e, n);
};
console.info = (...e) => {
N.apply(console, e), a("INFO", e);
};
console.debug = (...e) => {
R.apply(console, e), a("DEBUG", e);
};
let l;
function F() {
l && clearInterval(l), l = window.setInterval(() => {
if (t.length > 0) {
const e = [...t];
t = [], h(e);
}
}, A);
}
function U() {
l && (clearInterval(l), l = void 0);
}
window.addEventListener(
"unhandledrejection",
(e) => {
c("Unhandled Rejection:", e.reason);
const n = e.reason instanceof Error ? e.reason : String(e.reason);
a(
"ERROR",
["Unhandled Rejection:", e.reason],
n instanceof Error ? n : void 0
);
}
);
window.onerror = (e, n, o, i, s) => {
c(
"Global Error:",
e,
`Source: ${n}`,
`Line: ${o}`,
`Col: ${i}`,
s
);
const r = typeof e == "string" ? e : "Global event error", S = `Source: ${n}, Line: ${o}, Col: ${i}`;
return a(
"ERROR",
["Global Error:", r, S],
s
), !1;
};
function C(e) {
if (u = e.logApiEndpoint, f = e?.environment ?? "development", d = e?.userEmail ?? "Not_Authenticated_Logs", p = e?.tenantId ?? "DEFAULT", E = e.bucketName, y = e?.maxBufferSize ?? 20, A = e?.flushInterval ?? 1e4, m = e?.docType ?? "DEFAULT", !E || !u) {
c("Missing required configuration for logging service.");
return;
}
g(
"Logging service initialized. API Endpoint:",
u,
"Environment:",
f,
"Tenant:",
p,
"User:",
d,
"DOC TYPE:",
m
), F();
}
function v() {
g("Cleaning up logging service."), U(), t.length > 0 && (h([...t]), t = []);
}
export {
v as cleanupLogging,
C as initLogging
};