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