feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -1,66 +1,56 @@
|
||||
const util = require("util");
|
||||
const appLogger = require("../utils/logger");
|
||||
// File: server/rr/rr-logger.js
|
||||
// Console logger for RR flows with safe JSON. No socket emission by default.
|
||||
|
||||
function RRLogger(socket, baseCtx = {}) {
|
||||
const levels = new Set(["error", "warn", "info", "http", "verbose", "debug", "silly"]);
|
||||
const baseLogger = require("../utils/logger");
|
||||
|
||||
const safeString = (v) => {
|
||||
if (v instanceof Error) return v.message;
|
||||
if (typeof v === "string") return v;
|
||||
function RRLogger(_socket, defaults = {}) {
|
||||
function safeSerialize(value) {
|
||||
try {
|
||||
return JSON.stringify(v);
|
||||
} catch {
|
||||
return util.inspect(v, { depth: 2, maxArrayLength: 50 });
|
||||
}
|
||||
};
|
||||
|
||||
return function log(levelOrMsg, msgOrCtx, ctx) {
|
||||
let level = "info";
|
||||
let message = undefined;
|
||||
let meta = {};
|
||||
|
||||
if (typeof levelOrMsg === "string" && levels.has(levelOrMsg)) {
|
||||
level = levelOrMsg;
|
||||
message = msgOrCtx;
|
||||
meta = ctx || {};
|
||||
} else {
|
||||
message = levelOrMsg;
|
||||
meta = msgOrCtx || {};
|
||||
}
|
||||
|
||||
// Prepare console line + metadata
|
||||
const emitError = message instanceof Error;
|
||||
if (emitError) {
|
||||
meta.err = {
|
||||
name: message.name,
|
||||
message: message.message,
|
||||
stack: message.stack
|
||||
};
|
||||
message = message.message;
|
||||
if (level === "info") level = "error";
|
||||
}
|
||||
|
||||
const messageString = safeString(message);
|
||||
const line = `[RR] ${new Date().toISOString()} [${String(level).toUpperCase()}] ${messageString}`;
|
||||
const loggerFn = appLogger?.logger?.[level] || appLogger?.logger?.info || ((...args) => console.log(...args));
|
||||
|
||||
loggerFn(line, { ...baseCtx, ...meta });
|
||||
|
||||
// Always emit a STRING for `message` to sockets to avoid React crashes
|
||||
// If the original message was an object, include it in `details`
|
||||
const details = message && typeof message !== "string" && !emitError ? message : undefined;
|
||||
|
||||
try {
|
||||
socket?.emit?.("rr-log-event", {
|
||||
level,
|
||||
message: messageString, // <-- normalized string for UI
|
||||
ctx: { ...baseCtx, ...meta },
|
||||
...(details ? { details } : {}),
|
||||
ts: Date.now()
|
||||
const seen = new WeakSet();
|
||||
return JSON.stringify(value, (key, val) => {
|
||||
if (typeof val === "bigint") return val.toString();
|
||||
if (val instanceof Error) return { name: val.name, message: val.message, stack: val.stack };
|
||||
if (typeof val === "function") return undefined;
|
||||
if (typeof val === "object" && val !== null) {
|
||||
if (seen.has(val)) return "[Circular]";
|
||||
seen.add(val);
|
||||
if (val instanceof Date) return val.toISOString();
|
||||
if (val instanceof Map) return Object.fromEntries(val);
|
||||
if (val instanceof Set) return Array.from(val);
|
||||
if (typeof Buffer !== "undefined" && Buffer.isBuffer?.(val)) return `<Buffer len=${val.length}>`;
|
||||
}
|
||||
return val;
|
||||
});
|
||||
} catch {
|
||||
/* ignore socket emission errors */
|
||||
try {
|
||||
return String(value);
|
||||
} catch {
|
||||
return "[Unserializable]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return function log(level = "info", message = "", ctx = {}) {
|
||||
const lvl = String(level || "info").toLowerCase();
|
||||
const iso = new Date().toISOString();
|
||||
|
||||
const msgStr = typeof message === "string" ? message : safeSerialize(message);
|
||||
const mergedCtx = ctx && typeof ctx === "object" ? { ...defaults, ...ctx } : { ...defaults };
|
||||
|
||||
const ctxStr = Object.keys(mergedCtx || {}).length ? ` ${safeSerialize(mergedCtx)}` : "";
|
||||
const line = `[RR] ${iso} [${lvl.toUpperCase()}] ${msgStr}${ctxStr}`;
|
||||
|
||||
const logger = baseLogger?.logger || baseLogger || console;
|
||||
const fn = (logger[lvl] || logger.log || logger.info || console[lvl] || console.log).bind(logger);
|
||||
|
||||
try {
|
||||
fn(line);
|
||||
} catch {
|
||||
try {
|
||||
console.log(line);
|
||||
} catch {}
|
||||
}
|
||||
// INTENTIONALLY no socket emit here to avoid FE duplicates.
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user