@putkoff/abstract-logger 0.0.16 → 0.0.18

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
@@ -14488,25 +14488,68 @@ if (IS_NODE) {
14488
14488
  }
14489
14489
  /** Exported logger */
14490
14490
  const logger = nodeLogger;
14491
+ /**
14492
+ * Normalizes inputs by treating "unknown" and empty values as null.
14493
+ * Returns "unknown" ONLY if all inputs are invalid.
14494
+ */
14495
+ function resolveValue(...values) {
14496
+ let sawInput = false;
14497
+ for (const value of values) {
14498
+ if (value === undefined || value === null)
14499
+ continue;
14500
+ sawInput = true;
14501
+ if (typeof value === "string") {
14502
+ const v = value.trim();
14503
+ if (!v || v.toLowerCase() === "unknown")
14504
+ continue;
14505
+ return value;
14506
+ }
14507
+ return value;
14508
+ }
14509
+ return sawInput ? "unknown" : "unknown";
14510
+ }
14511
+ /* ------------------------------------------------------------------ */
14512
+ /* Stack inspection */
14513
+ /* ------------------------------------------------------------------ */
14491
14514
  /* ------------------------------------------------------------------ */
14492
14515
  /* Stack inspection */
14493
14516
  /* ------------------------------------------------------------------ */
14517
+ function extractFile(frame) {
14518
+ // file:///path/to/file.ts:line:col
14519
+ const esmMatch = frame.match(/file:\/\/(.+?):\d+:\d+/);
14520
+ if (esmMatch)
14521
+ return esmMatch[1];
14522
+ // (path/to/file.ts:line:col)
14523
+ const cjsMatch = frame.match(/\((.+?):\d+:\d+\)/);
14524
+ if (cjsMatch)
14525
+ return cjsMatch[1];
14526
+ // at path/to/file.ts:line:col
14527
+ const bareMatch = frame.match(/at\s+(.+?):\d+:\d+/);
14528
+ if (bareMatch)
14529
+ return bareMatch[1];
14530
+ return "unknown";
14531
+ }
14494
14532
  function getCallerInfo() {
14495
14533
  if (!IS_NODE) {
14496
14534
  return { functionName: "browser", file: "browser" };
14497
14535
  }
14498
14536
  const obj = {};
14499
- Error.captureStackTrace(obj, getCallerInfo);
14537
+ Error.captureStackTrace(obj, getLogString);
14500
14538
  const stack = obj.stack?.split("\n") ?? [];
14501
- const frame = stack.find((line) => !line.includes("getLogString") &&
14502
- !line.includes("logger.ts"));
14503
- if (!frame)
14539
+ const frame = stack
14540
+ .slice(1)
14541
+ .find((line) => !line.includes("node:internal") &&
14542
+ !line.includes("processTicksAndRejections"));
14543
+ if (!frame) {
14504
14544
  return { functionName: "unknown", file: "unknown" };
14505
- const fnMatch = frame.match(/at\s+([^\s(]+)/);
14506
- const fileMatch = frame.match(/\((.*?):\d+:\d+\)|at\s+(.*?):\d+:\d+/);
14545
+ }
14546
+ const fnMatch = frame.match(/at\s+async\s+([^\s(]+)/) ||
14547
+ frame.match(/at\s+([^\s(]+)/);
14507
14548
  return {
14508
- functionName: fnMatch?.[1] ?? "anonymous",
14509
- file: fileMatch?.[1] || fileMatch?.[2] || "unknown",
14549
+ functionName: fnMatch?.[1] && fnMatch[1] !== "Object.<anonymous>"
14550
+ ? fnMatch[1]
14551
+ : "unknown",
14552
+ file: extractFile(frame),
14510
14553
  };
14511
14554
  }
14512
14555
  /* ------------------------------------------------------------------ */
@@ -14528,24 +14571,44 @@ function resolveLogger(candidate) {
14528
14571
  /* ------------------------------------------------------------------ */
14529
14572
  /* UNIVERSAL getLogString (cannot crash) */
14530
14573
  /* ------------------------------------------------------------------ */
14531
- function getLogString(message, logType = null, details = null, function_name = null, file_location = null, consumerLogger = null) {
14532
- let { functionName, file } = getCallerInfo();
14533
- function_name = functionName || function_name;
14534
- file_location = file || file_location;
14574
+ function getLogString(messageOrOptions, logType = null, details = null, function_name = null, file_location = null, consumerLogger = null) {
14575
+ // ---------------- Normalize inputs ----------------
14576
+ let opts;
14577
+ if (typeof messageOrOptions === "object" && messageOrOptions !== null) {
14578
+ // Dict-style
14579
+ opts = messageOrOptions;
14580
+ }
14581
+ else {
14582
+ // Positional-style
14583
+ opts = {
14584
+ message: messageOrOptions,
14585
+ logType,
14586
+ details,
14587
+ function_name,
14588
+ file_location,
14589
+ consumerLogger,
14590
+ };
14591
+ }
14592
+ let { message, logType: resolvedLogType, details: resolvedDetails, function_name: resolvedFunctionName, file_location: resolvedFileLocation, consumerLogger: resolvedLoggerInput, } = opts;
14593
+ // ---------------- Resolve caller info ----------------
14594
+ const { functionName, file } = getCallerInfo();
14595
+ resolvedFunctionName = resolveValue(functionName !== "unknown" ? functionName : null, resolvedFunctionName);
14596
+ resolvedFileLocation = resolveValue(file !== "unknown" ? file : null, resolvedFileLocation);
14535
14597
  const emojiMap = {
14536
14598
  info: "ℹ️",
14537
14599
  warn: "⚠️",
14538
14600
  error: "❌",
14539
14601
  debug: "🔍",
14540
14602
  };
14541
- logType = logType || "info";
14542
- const emoji = emojiMap[logType] ?? "ℹ️";
14603
+ const finalLogType = resolvedLogType ?? "info";
14604
+ const emoji = emojiMap[finalLogType] ?? "ℹ️";
14543
14605
  const timestamp = new Date().toISOString();
14544
- let line = `${emoji} [${timestamp}] [${function_name}] ${message} | ${file_location}`;
14545
- if (details !== null && details !== undefined) {
14606
+ let line = `${emoji} [${timestamp}] [${resolvedFunctionName}] ${message}`;
14607
+ // ---------------- Details serialization ----------------
14608
+ if (resolvedDetails !== null && resolvedDetails !== undefined) {
14546
14609
  try {
14547
14610
  const seen = new WeakSet();
14548
- const safe = JSON.stringify(details, (key, value) => {
14611
+ const safe = JSON.stringify(resolvedDetails, (key, value) => {
14549
14612
  if (typeof value === "function")
14550
14613
  return undefined;
14551
14614
  if (typeof value === "bigint")
@@ -14563,21 +14626,26 @@ function getLogString(message, logType = null, details = null, function_name = n
14563
14626
  line += ` | [unserializable details]`;
14564
14627
  }
14565
14628
  }
14566
- /* ---------------- Browser ---------------- */
14629
+ line += ` | ${resolvedFileLocation}`;
14630
+ // ---------------- Browser ----------------
14567
14631
  if (IS_BROWSER) {
14568
- console[logType === "error" ? "error" :
14569
- logType === "warn" ? "warn" :
14570
- logType === "debug" ? "debug" : "log"](line);
14632
+ console[finalLogType === "error"
14633
+ ? "error"
14634
+ : finalLogType === "warn"
14635
+ ? "warn"
14636
+ : finalLogType === "debug"
14637
+ ? "debug"
14638
+ : "log"](line);
14571
14639
  return line;
14572
14640
  }
14573
- /* ---------------- Node ---------------- */
14574
- const activeLogger = resolveLogger(consumerLogger);
14641
+ // ---------------- Node ----------------
14642
+ const activeLogger = resolveLogger(resolvedLoggerInput);
14575
14643
  if (!activeLogger) {
14576
14644
  console.log(line);
14577
14645
  return line;
14578
14646
  }
14579
14647
  try {
14580
- switch (logType) {
14648
+ switch (finalLogType) {
14581
14649
  case "error":
14582
14650
  activeLogger.error(line);
14583
14651
  break;
@@ -14599,4 +14667,5 @@ function getLogString(message, logType = null, details = null, function_name = n
14599
14667
 
14600
14668
  exports.getLogString = getLogString;
14601
14669
  exports.logger = logger;
14670
+ exports.resolveValue = resolveValue;
14602
14671
  //# sourceMappingURL=index.js.map