@putkoff/abstract-logger 0.0.14 → 0.0.16
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 +115 -72
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/detect/index.d.ts +1 -0
- package/dist/cjs/types/detect/utils.d.ts +5 -0
- package/dist/cjs/types/logger/logger.d.ts +5 -10
- package/dist/esm/index.js +116 -72
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/detect/index.d.ts +1 -0
- package/dist/esm/types/detect/utils.d.ts +5 -0
- package/dist/esm/types/logger/logger.d.ts +5 -10
- package/dist/types/detect/index.d.ts +1 -0
- package/dist/types/detect/utils.d.ts +5 -0
- package/dist/types/index.d.ts +5 -13
- package/dist/types/logger/logger.d.ts +5 -10
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -14462,98 +14462,141 @@ var container = class Container {
|
|
|
14462
14462
|
} (winston));
|
|
14463
14463
|
|
|
14464
14464
|
/** ---------------------------------------------------------
|
|
14465
|
-
*
|
|
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
|
-
|
|
14490
|
-
|
|
14491
|
-
if (!frame)
|
|
14492
|
-
return {
|
|
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:
|
|
14508
|
+
functionName: fnMatch?.[1] ?? "anonymous",
|
|
14502
14509
|
file: fileMatch?.[1] || fileMatch?.[2] || "unknown",
|
|
14503
14510
|
};
|
|
14504
14511
|
}
|
|
14505
|
-
|
|
14506
|
-
|
|
14507
|
-
|
|
14508
|
-
function
|
|
14509
|
-
|
|
14510
|
-
|
|
14511
|
-
|
|
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
|
+
/* ------------------------------------------------------------------ */
|
|
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;
|
|
14512
14535
|
const emojiMap = {
|
|
14513
|
-
info: "
|
|
14536
|
+
info: "ℹ️",
|
|
14514
14537
|
warn: "⚠️",
|
|
14515
14538
|
error: "❌",
|
|
14516
14539
|
debug: "🔍",
|
|
14517
14540
|
};
|
|
14518
|
-
|
|
14541
|
+
logType = logType || "info";
|
|
14542
|
+
const emoji = emojiMap[logType] ?? "ℹ️";
|
|
14519
14543
|
const timestamp = new Date().toISOString();
|
|
14520
|
-
|
|
14521
|
-
|
|
14522
|
-
|
|
14523
|
-
|
|
14524
|
-
|
|
14525
|
-
|
|
14526
|
-
|
|
14527
|
-
|
|
14528
|
-
|
|
14529
|
-
|
|
14530
|
-
|
|
14531
|
-
|
|
14532
|
-
|
|
14533
|
-
|
|
14534
|
-
|
|
14535
|
-
|
|
14536
|
-
|
|
14537
|
-
|
|
14544
|
+
let line = `${emoji} [${timestamp}] [${function_name}] ${message} | ${file_location}`;
|
|
14545
|
+
if (details !== null && details !== undefined) {
|
|
14546
|
+
try {
|
|
14547
|
+
const seen = new WeakSet();
|
|
14548
|
+
const safe = JSON.stringify(details, (key, value) => {
|
|
14549
|
+
if (typeof value === "function")
|
|
14550
|
+
return undefined;
|
|
14551
|
+
if (typeof value === "bigint")
|
|
14552
|
+
return value.toString();
|
|
14553
|
+
if (typeof value === "object" && value !== null) {
|
|
14554
|
+
if (seen.has(value))
|
|
14555
|
+
return "[Circular]";
|
|
14556
|
+
seen.add(value);
|
|
14557
|
+
}
|
|
14558
|
+
return value;
|
|
14559
|
+
}, 2);
|
|
14560
|
+
line += ` | ${safe}`;
|
|
14561
|
+
}
|
|
14562
|
+
catch {
|
|
14563
|
+
line += ` | [unserializable details]`;
|
|
14564
|
+
}
|
|
14565
|
+
}
|
|
14566
|
+
/* ---------------- Browser ---------------- */
|
|
14567
|
+
if (IS_BROWSER) {
|
|
14568
|
+
console[logType === "error" ? "error" :
|
|
14569
|
+
logType === "warn" ? "warn" :
|
|
14570
|
+
logType === "debug" ? "debug" : "log"](line);
|
|
14571
|
+
return line;
|
|
14572
|
+
}
|
|
14573
|
+
/* ---------------- Node ---------------- */
|
|
14574
|
+
const activeLogger = resolveLogger(consumerLogger);
|
|
14575
|
+
if (!activeLogger) {
|
|
14576
|
+
console.log(line);
|
|
14577
|
+
return line;
|
|
14578
|
+
}
|
|
14579
|
+
try {
|
|
14580
|
+
switch (logType) {
|
|
14581
|
+
case "error":
|
|
14582
|
+
activeLogger.error(line);
|
|
14583
|
+
break;
|
|
14584
|
+
case "warn":
|
|
14585
|
+
activeLogger.warn(line);
|
|
14586
|
+
break;
|
|
14587
|
+
case "debug":
|
|
14588
|
+
activeLogger.debug(line);
|
|
14589
|
+
break;
|
|
14590
|
+
default:
|
|
14591
|
+
activeLogger.info(line);
|
|
14592
|
+
}
|
|
14538
14593
|
}
|
|
14539
|
-
|
|
14540
|
-
|
|
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);
|
|
14594
|
+
catch {
|
|
14595
|
+
console.log(line);
|
|
14552
14596
|
}
|
|
14553
|
-
return
|
|
14597
|
+
return line;
|
|
14554
14598
|
}
|
|
14555
14599
|
|
|
14556
|
-
exports.createChildLogger = createChildLogger;
|
|
14557
14600
|
exports.getLogString = getLogString;
|
|
14558
14601
|
exports.logger = logger;
|
|
14559
14602
|
//# sourceMappingURL=index.js.map
|