@putkoff/abstract-logger 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -14462,98 +14462,138 @@ var container = class Container {
14462
14462
  } (winston));
14463
14463
 
14464
14464
  /** ---------------------------------------------------------
14465
- * Core Logger
14466
- * --------------------------------------------------------- */
14467
- const logger = winston.createLogger({
14468
- level: "info",
14469
- format: winston.format.combine(winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), winston.format.errors({ stack: true }), winston.format.splat(), winston.format.printf(({ timestamp, level, message, stack }) => {
14470
- return `${timestamp} [${level}] ${message}${stack ? "\n" + stack : ""}`;
14471
- })),
14472
- transports: [
14473
- new winston.transports.Console(),
14474
- new winston.transports.File({ filename: "logs/error.log", level: "error" }),
14475
- new winston.transports.File({ filename: "logs/combined.log" }),
14476
- ],
14477
- });
14478
- /** Child logger factory */
14479
- function createChildLogger(label) {
14480
- return logger.child({ label });
14481
- }
14482
- /** ---------------------------------------------------------
14483
- * INTERNAL: capture calling stack frame
14465
+ * Detect Environment
14484
14466
  * --------------------------------------------------------- */
14467
+ const IS_BROWSER = typeof window !== "undefined" &&
14468
+ typeof window.document !== "undefined";
14469
+ const IS_NODE = typeof process !== "undefined" &&
14470
+ process.release?.name === "node";
14471
+
14472
+ /* ------------------------------------------------------------------ */
14473
+ /* Winston logger (Node only) */
14474
+ /* ------------------------------------------------------------------ */
14475
+ let nodeLogger = null;
14476
+ if (IS_NODE) {
14477
+ nodeLogger = winston.createLogger({
14478
+ level: "info",
14479
+ format: winston.format.combine(winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), winston.format.errors({ stack: true }), winston.format.printf(({ timestamp, level, message, stack }) => {
14480
+ return `${timestamp} [${level}] ${message}${stack ? "\n" + stack : ""}`;
14481
+ })),
14482
+ transports: [
14483
+ new winston.transports.Console(),
14484
+ new winston.transports.File({ filename: "logs/error.log", level: "error" }),
14485
+ new winston.transports.File({ filename: "logs/combined.log" }),
14486
+ ],
14487
+ });
14488
+ }
14489
+ /** Exported logger */
14490
+ const logger = nodeLogger;
14491
+ /* ------------------------------------------------------------------ */
14492
+ /* Stack inspection */
14493
+ /* ------------------------------------------------------------------ */
14485
14494
  function getCallerInfo() {
14495
+ if (!IS_NODE) {
14496
+ return { functionName: "browser", file: "browser" };
14497
+ }
14486
14498
  const obj = {};
14487
14499
  Error.captureStackTrace(obj, getCallerInfo);
14488
14500
  const stack = obj.stack?.split("\n") ?? [];
14489
- // look for the first non-logger frame
14490
- const frame = stack.find((line) => !line.includes("getCallerInfo") && !line.includes("getLogString"));
14491
- if (!frame) {
14492
- return { file: "unknown", functionName: "unknown" };
14493
- }
14494
- /** Parse things like:
14495
- * at processTxns (src/server/rabbitMq/processors/processLogs.ts:25:10)
14496
- * at src/server/.../file.ts:10:5
14497
- */
14498
- const functionMatch = frame.match(/at\s+([^\s(]+)/);
14501
+ const frame = stack.find((line) => !line.includes("getLogString") &&
14502
+ !line.includes("logger.ts"));
14503
+ if (!frame)
14504
+ return { functionName: "unknown", file: "unknown" };
14505
+ const fnMatch = frame.match(/at\s+([^\s(]+)/);
14499
14506
  const fileMatch = frame.match(/\((.*?):\d+:\d+\)|at\s+(.*?):\d+:\d+/);
14500
14507
  return {
14501
- functionName: functionMatch?.[1] ?? "anonymous",
14508
+ functionName: fnMatch?.[1] ?? "anonymous",
14502
14509
  file: fileMatch?.[1] || fileMatch?.[2] || "unknown",
14503
14510
  };
14504
14511
  }
14505
- /** ---------------------------------------------------------
14506
- * ZERO-BOILERPLATE getLogString
14507
- * --------------------------------------------------------- */
14512
+ /* ------------------------------------------------------------------ */
14513
+ /* Safe logger resolver */
14514
+ /* ------------------------------------------------------------------ */
14515
+ function resolveLogger(candidate) {
14516
+ if (candidate &&
14517
+ typeof candidate.info === "function" &&
14518
+ typeof candidate.error === "function") {
14519
+ return candidate;
14520
+ }
14521
+ if (logger &&
14522
+ typeof logger.info === "function" &&
14523
+ typeof logger.error === "function") {
14524
+ return logger;
14525
+ }
14526
+ return null;
14527
+ }
14528
+ /* ------------------------------------------------------------------ */
14529
+ /* UNIVERSAL getLogString (cannot crash) */
14530
+ /* ------------------------------------------------------------------ */
14508
14531
  function getLogString(message, details = null, logType = "info", consumerLogger = null) {
14509
- const tempLogger = consumerLogger || logger;
14510
14532
  const { functionName, file } = getCallerInfo();
14511
- // Emoji mapping
14512
14533
  const emojiMap = {
14513
- info: "",
14534
+ info: "ℹ️",
14514
14535
  warn: "⚠️",
14515
14536
  error: "❌",
14516
14537
  debug: "🔍",
14517
14538
  };
14518
- const emoji = emojiMap[logType] || "ℹ️";
14539
+ const emoji = emojiMap[logType] ?? "ℹ️";
14519
14540
  const timestamp = new Date().toISOString();
14520
- // Build initial log line
14521
- let logMessage = `${emoji} [${timestamp}] [${functionName}] ${message} | File: ${file}`;
14522
- // JSON-safe details
14523
- if (details) {
14524
- const seen = new WeakSet();
14525
- const safe = JSON.stringify(details, (key, value) => {
14526
- if (typeof value === "function")
14527
- return undefined;
14528
- if (typeof value === "bigint")
14529
- return value.toString();
14530
- if (typeof value === "object" && value !== null) {
14531
- if (seen.has(value))
14532
- return "[Circular]";
14533
- seen.add(value);
14534
- }
14535
- return value;
14536
- }, 2);
14537
- logMessage += ` | Details: ${safe}`;
14541
+ let line = `${emoji} [${timestamp}] [${functionName}] ${message} | ${file}`;
14542
+ if (details !== null && details !== undefined) {
14543
+ try {
14544
+ const seen = new WeakSet();
14545
+ const safe = JSON.stringify(details, (key, value) => {
14546
+ if (typeof value === "function")
14547
+ return undefined;
14548
+ if (typeof value === "bigint")
14549
+ return value.toString();
14550
+ if (typeof value === "object" && value !== null) {
14551
+ if (seen.has(value))
14552
+ return "[Circular]";
14553
+ seen.add(value);
14554
+ }
14555
+ return value;
14556
+ }, 2);
14557
+ line += ` | ${safe}`;
14558
+ }
14559
+ catch {
14560
+ line += ` | [unserializable details]`;
14561
+ }
14562
+ }
14563
+ /* ---------------- Browser ---------------- */
14564
+ if (IS_BROWSER) {
14565
+ console[logType === "error" ? "error" :
14566
+ logType === "warn" ? "warn" :
14567
+ logType === "debug" ? "debug" : "log"](line);
14568
+ return line;
14569
+ }
14570
+ /* ---------------- Node ---------------- */
14571
+ const activeLogger = resolveLogger(consumerLogger);
14572
+ if (!activeLogger) {
14573
+ console.log(line);
14574
+ return line;
14575
+ }
14576
+ try {
14577
+ switch (logType) {
14578
+ case "error":
14579
+ activeLogger.error(line);
14580
+ break;
14581
+ case "warn":
14582
+ activeLogger.warn(line);
14583
+ break;
14584
+ case "debug":
14585
+ activeLogger.debug(line);
14586
+ break;
14587
+ default:
14588
+ activeLogger.info(line);
14589
+ }
14538
14590
  }
14539
- // Dispatch to real logger
14540
- switch (logType) {
14541
- case "error":
14542
- tempLogger.error(logMessage);
14543
- break;
14544
- case "warn":
14545
- tempLogger.warn(logMessage);
14546
- break;
14547
- case "debug":
14548
- tempLogger.debug(logMessage);
14549
- break;
14550
- default:
14551
- tempLogger.info(logMessage);
14591
+ catch {
14592
+ console.log(line);
14552
14593
  }
14553
- return logMessage;
14594
+ return line;
14554
14595
  }
14555
14596
 
14556
- exports.createChildLogger = createChildLogger;
14557
14597
  exports.getLogString = getLogString;
14558
14598
  exports.logger = logger;
14559
14599
  //# sourceMappingURL=index.js.map