@putkoff/abstract-logger 0.0.13 → 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.
@@ -0,0 +1 @@
1
+ export * from './utils';
@@ -0,0 +1,5 @@
1
+ /** ---------------------------------------------------------
2
+ * Detect Environment
3
+ * --------------------------------------------------------- */
4
+ export declare const IS_BROWSER: boolean;
5
+ export declare const IS_NODE: boolean;
@@ -1,10 +1,3 @@
1
- /** ---------------------------------------------------------
2
- * Core Logger
3
- * --------------------------------------------------------- */
4
- export declare const logger: import("winston").Logger;
5
- /** Child logger factory */
6
- export declare function createChildLogger(label: string): import("winston").Logger;
7
- /** ---------------------------------------------------------
8
- * ZERO-BOILERPLATE getLogString
9
- * --------------------------------------------------------- */
1
+ /** Exported logger */
2
+ export declare const logger: any;
10
3
  export declare function getLogString(message: string, details?: any, logType?: "info" | "warn" | "error" | "debug", consumerLogger?: any): string;
package/dist/esm/index.js CHANGED
@@ -14460,96 +14460,137 @@ var container = class Container {
14460
14460
  } (winston));
14461
14461
 
14462
14462
  /** ---------------------------------------------------------
14463
- * Core Logger
14464
- * --------------------------------------------------------- */
14465
- const logger = winston.createLogger({
14466
- level: "info",
14467
- 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 }) => {
14468
- return `${timestamp} [${level}] ${message}${stack ? "\n" + stack : ""}`;
14469
- })),
14470
- transports: [
14471
- new winston.transports.Console(),
14472
- new winston.transports.File({ filename: "logs/error.log", level: "error" }),
14473
- new winston.transports.File({ filename: "logs/combined.log" }),
14474
- ],
14475
- });
14476
- /** Child logger factory */
14477
- function createChildLogger(label) {
14478
- return logger.child({ label });
14479
- }
14480
- /** ---------------------------------------------------------
14481
- * INTERNAL: capture calling stack frame
14463
+ * Detect Environment
14482
14464
  * --------------------------------------------------------- */
14465
+ const IS_BROWSER = typeof window !== "undefined" &&
14466
+ typeof window.document !== "undefined";
14467
+ const IS_NODE = typeof process !== "undefined" &&
14468
+ process.release?.name === "node";
14469
+
14470
+ /* ------------------------------------------------------------------ */
14471
+ /* Winston logger (Node only) */
14472
+ /* ------------------------------------------------------------------ */
14473
+ let nodeLogger = null;
14474
+ if (IS_NODE) {
14475
+ nodeLogger = winston.createLogger({
14476
+ level: "info",
14477
+ 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 }) => {
14478
+ return `${timestamp} [${level}] ${message}${stack ? "\n" + stack : ""}`;
14479
+ })),
14480
+ transports: [
14481
+ new winston.transports.Console(),
14482
+ new winston.transports.File({ filename: "logs/error.log", level: "error" }),
14483
+ new winston.transports.File({ filename: "logs/combined.log" }),
14484
+ ],
14485
+ });
14486
+ }
14487
+ /** Exported logger */
14488
+ const logger = nodeLogger;
14489
+ /* ------------------------------------------------------------------ */
14490
+ /* Stack inspection */
14491
+ /* ------------------------------------------------------------------ */
14483
14492
  function getCallerInfo() {
14493
+ if (!IS_NODE) {
14494
+ return { functionName: "browser", file: "browser" };
14495
+ }
14484
14496
  const obj = {};
14485
14497
  Error.captureStackTrace(obj, getCallerInfo);
14486
14498
  const stack = obj.stack?.split("\n") ?? [];
14487
- // look for the first non-logger frame
14488
- const frame = stack.find((line) => !line.includes("getCallerInfo") && !line.includes("getLogString"));
14489
- if (!frame) {
14490
- return { file: "unknown", functionName: "unknown" };
14491
- }
14492
- /** Parse things like:
14493
- * at processTxns (src/server/rabbitMq/processors/processLogs.ts:25:10)
14494
- * at src/server/.../file.ts:10:5
14495
- */
14496
- const functionMatch = frame.match(/at\s+([^\s(]+)/);
14499
+ const frame = stack.find((line) => !line.includes("getLogString") &&
14500
+ !line.includes("logger.ts"));
14501
+ if (!frame)
14502
+ return { functionName: "unknown", file: "unknown" };
14503
+ const fnMatch = frame.match(/at\s+([^\s(]+)/);
14497
14504
  const fileMatch = frame.match(/\((.*?):\d+:\d+\)|at\s+(.*?):\d+:\d+/);
14498
14505
  return {
14499
- functionName: functionMatch?.[1] ?? "anonymous",
14506
+ functionName: fnMatch?.[1] ?? "anonymous",
14500
14507
  file: fileMatch?.[1] || fileMatch?.[2] || "unknown",
14501
14508
  };
14502
14509
  }
14503
- /** ---------------------------------------------------------
14504
- * ZERO-BOILERPLATE getLogString
14505
- * --------------------------------------------------------- */
14510
+ /* ------------------------------------------------------------------ */
14511
+ /* Safe logger resolver */
14512
+ /* ------------------------------------------------------------------ */
14513
+ function resolveLogger(candidate) {
14514
+ if (candidate &&
14515
+ typeof candidate.info === "function" &&
14516
+ typeof candidate.error === "function") {
14517
+ return candidate;
14518
+ }
14519
+ if (logger &&
14520
+ typeof logger.info === "function" &&
14521
+ typeof logger.error === "function") {
14522
+ return logger;
14523
+ }
14524
+ return null;
14525
+ }
14526
+ /* ------------------------------------------------------------------ */
14527
+ /* UNIVERSAL getLogString (cannot crash) */
14528
+ /* ------------------------------------------------------------------ */
14506
14529
  function getLogString(message, details = null, logType = "info", consumerLogger = null) {
14507
- const tempLogger = consumerLogger || logger;
14508
14530
  const { functionName, file } = getCallerInfo();
14509
- // Emoji mapping
14510
14531
  const emojiMap = {
14511
- info: "",
14532
+ info: "ℹ️",
14512
14533
  warn: "⚠️",
14513
14534
  error: "❌",
14514
14535
  debug: "🔍",
14515
14536
  };
14516
- const emoji = emojiMap[logType] || "ℹ️";
14537
+ const emoji = emojiMap[logType] ?? "ℹ️";
14517
14538
  const timestamp = new Date().toISOString();
14518
- // Build initial log line
14519
- let logMessage = `${emoji} [${timestamp}] [${functionName}] ${message} | File: ${file}`;
14520
- // JSON-safe details
14521
- if (details) {
14522
- const seen = new WeakSet();
14523
- const safe = JSON.stringify(details, (key, value) => {
14524
- if (typeof value === "function")
14525
- return undefined;
14526
- if (typeof value === "bigint")
14527
- return value.toString();
14528
- if (typeof value === "object" && value !== null) {
14529
- if (seen.has(value))
14530
- return "[Circular]";
14531
- seen.add(value);
14532
- }
14533
- return value;
14534
- }, 2);
14535
- logMessage += ` | Details: ${safe}`;
14539
+ let line = `${emoji} [${timestamp}] [${functionName}] ${message} | ${file}`;
14540
+ if (details !== null && details !== undefined) {
14541
+ try {
14542
+ const seen = new WeakSet();
14543
+ const safe = JSON.stringify(details, (key, value) => {
14544
+ if (typeof value === "function")
14545
+ return undefined;
14546
+ if (typeof value === "bigint")
14547
+ return value.toString();
14548
+ if (typeof value === "object" && value !== null) {
14549
+ if (seen.has(value))
14550
+ return "[Circular]";
14551
+ seen.add(value);
14552
+ }
14553
+ return value;
14554
+ }, 2);
14555
+ line += ` | ${safe}`;
14556
+ }
14557
+ catch {
14558
+ line += ` | [unserializable details]`;
14559
+ }
14560
+ }
14561
+ /* ---------------- Browser ---------------- */
14562
+ if (IS_BROWSER) {
14563
+ console[logType === "error" ? "error" :
14564
+ logType === "warn" ? "warn" :
14565
+ logType === "debug" ? "debug" : "log"](line);
14566
+ return line;
14567
+ }
14568
+ /* ---------------- Node ---------------- */
14569
+ const activeLogger = resolveLogger(consumerLogger);
14570
+ if (!activeLogger) {
14571
+ console.log(line);
14572
+ return line;
14573
+ }
14574
+ try {
14575
+ switch (logType) {
14576
+ case "error":
14577
+ activeLogger.error(line);
14578
+ break;
14579
+ case "warn":
14580
+ activeLogger.warn(line);
14581
+ break;
14582
+ case "debug":
14583
+ activeLogger.debug(line);
14584
+ break;
14585
+ default:
14586
+ activeLogger.info(line);
14587
+ }
14536
14588
  }
14537
- // Dispatch to real logger
14538
- switch (logType) {
14539
- case "error":
14540
- tempLogger.error(logMessage);
14541
- break;
14542
- case "warn":
14543
- tempLogger.warn(logMessage);
14544
- break;
14545
- case "debug":
14546
- tempLogger.debug(logMessage);
14547
- break;
14548
- default:
14549
- tempLogger.info(logMessage);
14589
+ catch {
14590
+ console.log(line);
14550
14591
  }
14551
- return logMessage;
14592
+ return line;
14552
14593
  }
14553
14594
 
14554
- export { createChildLogger, getLogString, logger };
14595
+ export { getLogString, logger };
14555
14596
  //# sourceMappingURL=index.js.map