@bian-womp/spark-graph 0.3.55 → 0.3.57
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/lib/cjs/index.cjs +131 -6
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/cjs/src/misc/utils/LevelLogger.d.ts +14 -0
- package/lib/cjs/src/misc/utils/LevelLogger.d.ts.map +1 -1
- package/lib/cjs/src/misc/utils/LevelLogger.test.d.ts +2 -0
- package/lib/cjs/src/misc/utils/LevelLogger.test.d.ts.map +1 -0
- package/lib/cjs/src/misc/utils/test-logger-output.d.ts +7 -0
- package/lib/cjs/src/misc/utils/test-logger-output.d.ts.map +1 -0
- package/lib/esm/index.js +131 -6
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/src/misc/utils/LevelLogger.d.ts +14 -0
- package/lib/esm/src/misc/utils/LevelLogger.d.ts.map +1 -1
- package/lib/esm/src/misc/utils/LevelLogger.test.d.ts +2 -0
- package/lib/esm/src/misc/utils/LevelLogger.test.d.ts.map +1 -0
- package/lib/esm/src/misc/utils/test-logger-output.d.ts +7 -0
- package/lib/esm/src/misc/utils/test-logger-output.d.ts.map +1 -0
- package/package.json +6 -3
|
@@ -44,6 +44,20 @@ export declare class LevelLogger {
|
|
|
44
44
|
* Logs an error message
|
|
45
45
|
*/
|
|
46
46
|
error(message: string, context?: Record<string, unknown>, overrideLevel?: LogLevel): void;
|
|
47
|
+
/**
|
|
48
|
+
* Tries to parse a string as JSON and format it nicely if it's fully JSON
|
|
49
|
+
*/
|
|
50
|
+
private parseJsonStringIfFull;
|
|
51
|
+
/**
|
|
52
|
+
* Tries to parse a string as JSON and return the parsed object/array if it's fully JSON
|
|
53
|
+
* Used for parsing JSON strings inside arrays so they can be formatted by stringifySceneAndOps
|
|
54
|
+
*/
|
|
55
|
+
private parseJsonStringToObject;
|
|
56
|
+
/**
|
|
57
|
+
* Parses JSON strings in context values if they are fully JSON
|
|
58
|
+
* Also handles arrays containing JSON strings
|
|
59
|
+
*/
|
|
60
|
+
private parseJsonStringsInContext;
|
|
47
61
|
/**
|
|
48
62
|
* Core logging method that respects the log level and applies prefix
|
|
49
63
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LevelLogger.d.ts","sourceRoot":"","sources":["../../../../../src/misc/utils/LevelLogger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAI1D;;;GAGG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,YAAY,CAAW;IAC/B,OAAO,CAAC,MAAM,CAAS;IAEvB;;OAEG;IACH,gBAAuB,WAAW,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CACzC;gBAEP,YAAY,GAAE,QAAiB,EAAE,MAAM,GAAE,MAAW;IAKhE;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAI/B;;OAEG;IACH,SAAS,IAAI,MAAM;IAInB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IAI/B;;OAEG;IACH,QAAQ,IAAI,QAAQ;IAIpB,aAAa,IAAI,MAAM;IAIvB;;OAEG;IACH,KAAK,CACH,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,aAAa,CAAC,EAAE,QAAQ,GACvB,IAAI;IAIP;;OAEG;IACH,IAAI,CACF,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,aAAa,CAAC,EAAE,QAAQ,GACvB,IAAI;IAIP;;OAEG;IACH,IAAI,CACF,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,aAAa,CAAC,EAAE,QAAQ,GACvB,IAAI;IAIP;;OAEG;IACH,KAAK,CACH,OAAO,EAA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H,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,aAAa,CAAC,EAAE,QAAQ,GACvB,IAAI;IAIP;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAkB7B;;;OAGG;IACH,OAAO,CAAC,uBAAuB;IAiB/B;;;OAGG;IACH,OAAO,CAAC,yBAAyB;IAuBjC;;OAEG;IACH,OAAO,CAAC,GAAG;CAgHZ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LevelLogger.test.d.ts","sourceRoot":"","sources":["../../../../../src/misc/utils/LevelLogger.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-logger-output.d.ts","sourceRoot":"","sources":["../../../../../src/misc/utils/test-logger-output.ts"],"names":[],"mappings":";AACA;;;GAGG"}
|
package/lib/esm/index.js
CHANGED
|
@@ -1544,6 +1544,70 @@ class LevelLogger {
|
|
|
1544
1544
|
error(message, context, overrideLevel) {
|
|
1545
1545
|
this.log("error", message, context, overrideLevel);
|
|
1546
1546
|
}
|
|
1547
|
+
/**
|
|
1548
|
+
* Tries to parse a string as JSON and format it nicely if it's fully JSON
|
|
1549
|
+
*/
|
|
1550
|
+
parseJsonStringIfFull(str) {
|
|
1551
|
+
const trimmed = str.trim();
|
|
1552
|
+
// Check if the string starts with { or [ and ends with } or ]
|
|
1553
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
1554
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
1555
|
+
try {
|
|
1556
|
+
const parsed = JSON.parse(trimmed);
|
|
1557
|
+
return JSON.stringify(parsed, null, 2);
|
|
1558
|
+
}
|
|
1559
|
+
catch {
|
|
1560
|
+
// If parsing fails, return original string
|
|
1561
|
+
return str;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
return str;
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1567
|
+
* Tries to parse a string as JSON and return the parsed object/array if it's fully JSON
|
|
1568
|
+
* Used for parsing JSON strings inside arrays so they can be formatted by stringifySceneAndOps
|
|
1569
|
+
*/
|
|
1570
|
+
parseJsonStringToObject(str) {
|
|
1571
|
+
const trimmed = str.trim();
|
|
1572
|
+
// Check if the string starts with { or [ and ends with } or ]
|
|
1573
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
1574
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
1575
|
+
try {
|
|
1576
|
+
return JSON.parse(trimmed);
|
|
1577
|
+
}
|
|
1578
|
+
catch {
|
|
1579
|
+
// If parsing fails, return original string
|
|
1580
|
+
return str;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
return str;
|
|
1584
|
+
}
|
|
1585
|
+
/**
|
|
1586
|
+
* Parses JSON strings in context values if they are fully JSON
|
|
1587
|
+
* Also handles arrays containing JSON strings
|
|
1588
|
+
*/
|
|
1589
|
+
parseJsonStringsInContext(context) {
|
|
1590
|
+
const parsed = {};
|
|
1591
|
+
for (const [key, value] of Object.entries(context)) {
|
|
1592
|
+
if (typeof value === "string") {
|
|
1593
|
+
parsed[key] = this.parseJsonStringIfFull(value);
|
|
1594
|
+
}
|
|
1595
|
+
else if (Array.isArray(value)) {
|
|
1596
|
+
// Process arrays: parse JSON strings within the array to objects/arrays
|
|
1597
|
+
// so they can be formatted correctly by stringifySceneAndOps
|
|
1598
|
+
parsed[key] = value.map((item) => {
|
|
1599
|
+
if (typeof item === "string") {
|
|
1600
|
+
return this.parseJsonStringToObject(item);
|
|
1601
|
+
}
|
|
1602
|
+
return item;
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
else {
|
|
1606
|
+
parsed[key] = value;
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
return parsed;
|
|
1610
|
+
}
|
|
1547
1611
|
/**
|
|
1548
1612
|
* Core logging method that respects the log level and applies prefix
|
|
1549
1613
|
*/
|
|
@@ -1557,12 +1621,73 @@ class LevelLogger {
|
|
|
1557
1621
|
const effectiveValue = LevelLogger.levelValues[effectiveLevel] ?? 1;
|
|
1558
1622
|
// Only log if the requested level is >= effective level
|
|
1559
1623
|
if (requestedValue >= effectiveValue) {
|
|
1560
|
-
const {
|
|
1561
|
-
const
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1624
|
+
const { logFormat = "plain", ...rest } = context ?? {};
|
|
1625
|
+
const formatJson = logFormat === "json" || logFormat === "json-deep";
|
|
1626
|
+
const parseJsonString = logFormat === "json-deep";
|
|
1627
|
+
// Parse JSON strings in context values if requested
|
|
1628
|
+
let processedContext = rest;
|
|
1629
|
+
const parsedKeys = new Set(); // Track keys that had JSON strings parsed
|
|
1630
|
+
if (parseJsonString && rest && Object.keys(rest).length > 0) {
|
|
1631
|
+
// Check which keys had JSON strings before parsing
|
|
1632
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
1633
|
+
if (typeof value === "string") {
|
|
1634
|
+
const trimmed = value.trim();
|
|
1635
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
1636
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
1637
|
+
try {
|
|
1638
|
+
JSON.parse(trimmed);
|
|
1639
|
+
parsedKeys.add(key);
|
|
1640
|
+
}
|
|
1641
|
+
catch {
|
|
1642
|
+
// Not valid JSON, skip
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
else if (Array.isArray(value)) {
|
|
1647
|
+
// Check if array contains JSON strings
|
|
1648
|
+
for (const item of value) {
|
|
1649
|
+
if (typeof item === "string") {
|
|
1650
|
+
const trimmed = item.trim();
|
|
1651
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
1652
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
1653
|
+
try {
|
|
1654
|
+
JSON.parse(trimmed);
|
|
1655
|
+
parsedKeys.add(key);
|
|
1656
|
+
break; // Found at least one JSON string in array
|
|
1657
|
+
}
|
|
1658
|
+
catch {
|
|
1659
|
+
// Not valid JSON, continue
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
processedContext = this.parseJsonStringsInContext(rest);
|
|
1667
|
+
}
|
|
1668
|
+
const contextStr = processedContext && Object.keys(processedContext).length > 0
|
|
1669
|
+
? `${formatJson ? "\n" : " "}${Object.entries(processedContext)
|
|
1670
|
+
.map(([k, v]) => {
|
|
1671
|
+
// If parseJsonString is enabled and value is a formatted JSON string
|
|
1672
|
+
// (starts with { or [ and contains newlines indicating it was formatted),
|
|
1673
|
+
// output it directly without stringifySceneAndOps to preserve formatting
|
|
1674
|
+
// Wrap it with json`...` to distinguish it as formatted JSON
|
|
1675
|
+
if (parseJsonString &&
|
|
1676
|
+
typeof v === "string" &&
|
|
1677
|
+
v.trim().match(/^[{\[]/) &&
|
|
1678
|
+
v.includes("\n")) {
|
|
1679
|
+
return `${k}=json\`${v}\``;
|
|
1680
|
+
}
|
|
1681
|
+
// If this key had a JSON string parsed and we're formatting JSON,
|
|
1682
|
+
// wrap the formatted output with json`...`
|
|
1683
|
+
if (formatJson && parseJsonString && parsedKeys.has(k)) {
|
|
1684
|
+
return `${k}=json\`${stringifySceneAndOps(v)}\``;
|
|
1685
|
+
}
|
|
1686
|
+
if (formatJson) {
|
|
1687
|
+
return `${k}=${stringifySceneAndOps(v)}`;
|
|
1688
|
+
}
|
|
1689
|
+
return `${k}=${JSON.stringify(v)}`;
|
|
1690
|
+
})
|
|
1566
1691
|
.join(formatJson ? "\n" : " ")}`
|
|
1567
1692
|
: "";
|
|
1568
1693
|
const prefixedMessage = this.prefix
|