44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
// File: server/rr/rr-logger-event.js
|
|
// Fortellis-style log helper for RR flows.
|
|
// Usage: CreateRRLogEvent(socket, "DEBUG"|"INFO"|"WARN"|"ERROR", message, details?)
|
|
|
|
const RRLogger = require("../rr/rr-logger");
|
|
|
|
// Normalize level to upper + provide console + socket event with coherent payload
|
|
function CreateRRLogEvent(socket, level = "DEBUG", message = "", details = {}) {
|
|
const lvl = String(level || "DEBUG").toUpperCase();
|
|
const ts = Date.now();
|
|
|
|
// Console (uses existing RRLogger, which also emits "RR:LOG" to sockets for live tail)
|
|
try {
|
|
const log = RRLogger(socket);
|
|
const fn = lvl === "ERROR" ? "error" : lvl === "WARN" ? "warn" : lvl === "INFO" ? "info" : "debug";
|
|
log(fn, message, { ts, ...safeJson(details) });
|
|
} catch {
|
|
/* ignore console/log failures */
|
|
}
|
|
|
|
// Structured RR event for FE debug panel (parity with Fortellis' CreateFortellisLogEvent)
|
|
try {
|
|
socket?.emit?.("rr-log-event", {
|
|
level: lvl,
|
|
message,
|
|
ts,
|
|
...safeJson(details)
|
|
});
|
|
} catch {
|
|
/* ignore socket emit failures */
|
|
}
|
|
}
|
|
|
|
// Best-effort ensure details are JSON-safe (avoid circular / BigInt)
|
|
function safeJson(obj) {
|
|
try {
|
|
return JSON.parse(JSON.stringify(obj ?? {}));
|
|
} catch {
|
|
return { _unsafe: true };
|
|
}
|
|
}
|
|
|
|
module.exports = CreateRRLogEvent;
|