@bymax-one/nest-logger 1.4.8 → 1.4.9

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/CHANGELOG.md CHANGED
@@ -11,6 +11,81 @@ heading here.
11
11
 
12
12
  ## [Unreleased]
13
13
 
14
+ ## [1.4.9] - 2026-11-06
15
+
16
+ ### Security
17
+
18
+ - **Error entries no longer carry what a failed driver call was given or quoted.** A driver puts the
19
+ call that failed on its error (a Redis reply error carries `command: { name, args }`, a query
20
+ error `parameters`) and a PostgreSQL error quotes the row in `detail` and the statement in `where`
21
+ and `internalQuery`. The key-name redaction cannot see a value inside such a string, so it reached
22
+ the entry. The `args` and `parameters` own fields of an error are now omitted, a `command` object
23
+ is reduced to its `name`, and an error that PostgreSQL raised loses `detail`, `where` and
24
+ `internalQuery` while its other fields stay. This holds for the root `err`, for every `cause` and
25
+ `errors` link, for an `Error` nested in the metadata, and for a driver error held as a field of
26
+ another error (a `driverError` or a `parent` field, for instance). `args` and `parameters` match
27
+ in any case. A field of those names that you attach yourself to an error is no longer logged
28
+ either; put it under a different name, or in the metadata, where name-based redaction applies.
29
+
30
+ This covers the carriers above, not every driver: a field that carries a query's text or values
31
+ under another name (a SQL string, `bindings`, an HTTP client's request body) is still written as
32
+ the error holds it. Do not put such values on an error you log, or keep them out of its own fields
33
+ before it reaches the logger.
34
+
35
+ ### Changed
36
+
37
+ - **An `Error` nested in the metadata is logged with its content.** `{ inner: new Error('x') }` was
38
+ written as `inner: {}`, because `JSON.stringify` skips an `Error`'s non-enumerable `message` and
39
+ `stack`; the same held inside arrays and inside another `Error`'s own fields, and a nested `cause` or
40
+ `AggregateError.errors` was lost with it. Outside a record-root key that has a serializer, such an
41
+ `Error` is now written as what it was written as before (its own enumerable fields, censored by name,
42
+ or the output of its `toJSON`) with `type`, `message`, `stack`, `cause` and `errors` added. A part is
43
+ added only where the node has no key of that name, so nothing written before is replaced or removed,
44
+ and a part that cannot be read is left out and the node carries `_errorFieldsIncomplete: true`. A
45
+ `cause` or `errors` value that is not an `Error` is written as data and censored by name; it is never
46
+ turned into text. The added chain and `errors` are bounded as the root `err`'s are, each ending in a
47
+ `_truncated` marker; these limits apply to the added parts only.
48
+
49
+ #### Apply to a derived backend
50
+
51
+ Nothing to change in code. What can differ at run time:
52
+
53
+ - A nested `Error` that was written as `{}` is now written with its content, so a filter or alert that
54
+ matched `inner: {}` stops matching, and a parser of nested errors gets new fields. Entries that hold
55
+ one are larger, because the stack is included.
56
+ - A string `cause` is now logged exactly as a `message` is, and the root `err` already did so. Do not
57
+ put secrets in an `Error`'s `message` or `cause`: the redaction works on field names and cannot see
58
+ a secret inside a string.
59
+ - An own enumerable field of an `Error` named `type`, `message`, `stack`, `cause` or `errors` is kept
60
+ as it was written, and the added part of that name is not written.
61
+ - The added `type` is censored when `type` or `name` is a covered name, and the added `stack` when
62
+ `stack`, `name` or `message` is: both carry that text. Without such a covered name they are written.
63
+ - A custom serializer on a record-root key receives the same value it received before, censored as
64
+ before and with any `Error` inside it still an instance. What a serializer returns is written as
65
+ the serializer returned it, so an `Error` in its output is still `{}`: the added parts are for the
66
+ metadata walk only. The one exception is the Security entry above: the arguments and quoted values
67
+ of a failed driver call are omitted from an `Error` there too.
68
+ - This applies where the name redaction runs on the record, which is the default
69
+ (`redactStrategy: 'names'` with at least one name covered). Under `'paths'`, or with no name covered,
70
+ the record is not walked and a nested `Error` is written as before.
71
+
72
+ ### Internal
73
+
74
+ - The server bundle budget (`scripts/check-size.mjs`) is raised to 32 KiB, re-derived from the built
75
+ artifact with the usual headroom; the calibration history there has the measurements. The growth
76
+ is executable code, not comment weight.
77
+ - The mutation inventory now exempts the two specs that judge no mutant, each with its reason
78
+ (`scripts/mutation-inventory.exempt.json`): one spawns a script and imports nothing from `src/`, the
79
+ other scans the text of the sources and the README.
80
+
81
+ ### Tests
82
+
83
+ - The shapes of a nested `Error` are judged against the output recorded
84
+ from 1.4.8: none may throw or fail the record where 1.4.8 did not, show a secret marker 1.4.8 did not,
85
+ or drop a value 1.4.8 emitted.
86
+ - `LOGGER_ERROR_CODES`, `RESERVED_LOG_KEYS` and `RESERVED_LOG_KEYS_NOT_EMITTED` are compared whole
87
+ against written-out lists, so an added, removed or renamed entry fails a spec.
88
+
14
89
  ## [1.4.8] - 2026-11-01
15
90
 
16
91
  ### Fixed
@@ -2338,7 +2413,8 @@ published `dist/` is identical — no runtime behaviour changes for consumers.
2338
2413
  - Professional CI suite: `ci.yml`, `bench.yml`, `codeql.yml`, `scorecard.yml`,
2339
2414
  `release.yml`, Dependabot, and issue templates
2340
2415
 
2341
- [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.8...HEAD
2416
+ [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.9...HEAD
2417
+ [1.4.9]: https://github.com/bymaxone/nest-logger/compare/v1.4.8...v1.4.9
2342
2418
  [1.4.8]: https://github.com/bymaxone/nest-logger/compare/v1.4.7...v1.4.8
2343
2419
  [1.4.7]: https://github.com/bymaxone/nest-logger/compare/v1.4.6...v1.4.7
2344
2420
  [1.4.6]: https://github.com/bymaxone/nest-logger/compare/v1.4.5...v1.4.6
package/README.md CHANGED
@@ -725,12 +725,18 @@ field the serializer derives, and they pass through the same name-based redactio
725
725
  any other serializer output.
726
726
 
727
727
  An error's own enumerable properties (`code`, `statusCode`, domain fields) are carried through, as
728
- Pino's standard serializer did.
728
+ Pino's standard serializer did, with one exception: the arguments of a failed driver call are not
729
+ written. The fields `args` and `parameters` are omitted, a `command` object is reduced to its
730
+ `name`, and a PostgreSQL error loses `detail`, `where` and `internalQuery`, which quote the row and
731
+ the statement (its other fields stay), because the redaction
732
+ matches key names and cannot see a value inside a string.
729
733
 
730
734
  When those properties cannot be read (a Proxy whose `ownKeys` throws, or a getter that throws), none of them is copied and the error node carries `_errorFieldsIncomplete: true`, so an entry without its `code` is never mistaken for a complete one.
731
735
 
732
736
  An `Error` that cannot be read at all does not cost the record its `logKey`, `userId` or metadata: the node is replaced by its sanitized form, which keeps `name` and `message` and carries the same marker unless `name` or `message` itself is unreadable, in which case it is `{ type: 'SanitizeFailed' }`. Filter on `err._errorFieldsIncomplete: true` or `SanitizeFailed` to find these entries.
733
737
 
738
+ An `Error` nested in the metadata (under a key, inside an array, or inside another `Error`) is written as its own enumerable fields, censored by name (or as its `toJSON` decides), with `type`, `message`, `stack`, `cause` and `errors` added where the node has no key of that name. The added cause chain and `errors` are bounded as the root `err`'s are, each with a `_truncated` marker. A string `cause` is logged like a `message`: do not put secrets in either. Whatever sits under a record-root key that has a serializer, `err` or a key of `serializers`, reaches that serializer censored by name, with its `Error`s still instances, and what a serializer returns is written as returned, so an `Error` in that output stays `{}` (the arguments and quoted values of a failed driver call are omitted from it, as everywhere).
739
+
734
740
  ### 7. Context propagation with `LogContextService`
735
741
 
736
742
  ```typescript
@@ -438,10 +438,28 @@ function toSanitizedError(value, seen, depth2, maxDepth) {
438
438
  return { name: "SanitizeFailed", message: "Failed to sanitize the thrown value" };
439
439
  }
440
440
  }
441
+ var ARGUMENT_FIELDS = /* @__PURE__ */ new Set(["args", "parameters"]);
442
+ var DATABASE_VALUE_FIELDS = /* @__PURE__ */ new Set(["detail", "where", "internalQuery"]);
443
+ var SQLSTATE = /^[0-9A-Z]{5}$/;
444
+ var OMIT_OWN_FIELD = /* @__PURE__ */ Symbol("omit-own-field");
445
+ function isDatabaseError(source) {
446
+ const own = (key) => Object.getOwnPropertyDescriptor(source, key)?.value;
447
+ const code = own("code");
448
+ return typeof own("severity") === "string" && typeof own("routine") === "string" && typeof code === "string" && SQLSTATE.test(code);
449
+ }
450
+ function ownFieldForLog(source, key, value) {
451
+ if (ARGUMENT_FIELDS.has(key.toLowerCase())) return OMIT_OWN_FIELD;
452
+ if (DATABASE_VALUE_FIELDS.has(key) && isDatabaseError(source)) return OMIT_OWN_FIELD;
453
+ if (key !== "command" || typeof value !== "object" || value === null) return value;
454
+ const name = Object.getOwnPropertyDescriptor(value, "name")?.value;
455
+ return typeof name === "string" ? { name } : OMIT_OWN_FIELD;
456
+ }
441
457
  function assignOwnErrorFields(target, source) {
442
458
  try {
443
459
  const own = Array.isArray(source) ? {} : source;
444
- for (const [key, value] of Object.entries(own)) {
460
+ for (const [key, field] of Object.entries(own)) {
461
+ const value = ownFieldForLog(own, key, field);
462
+ if (value === OMIT_OWN_FIELD) continue;
445
463
  if (!Object.hasOwn(target, key) && !DERIVED_ERROR_FIELDS.has(key)) {
446
464
  Object.defineProperty(target, key, { value, enumerable: true });
447
465
  }
@@ -1491,19 +1509,19 @@ var BUFFER_TO_JSON = Buffer.prototype.toJSON;
1491
1509
  function defineOwn(target, key, value) {
1492
1510
  Reflect.defineProperty(target, key, { value, enumerable: true });
1493
1511
  }
1494
- function walkArray(value, sensitive, censor, depth2, ancestors) {
1512
+ function walkArray(value, rules, depth2, ancestors, inSerialized) {
1495
1513
  const length = value.length;
1496
1514
  const copy = [];
1497
1515
  for (let index = 0; index < length; index++) {
1498
1516
  Reflect.set(
1499
1517
  copy,
1500
1518
  index,
1501
- walk(Reflect.get(value, index), sensitive, censor, depth2 + 1, ancestors)
1519
+ walk(Reflect.get(value, index), rules, depth2 + 1, ancestors, true, inSerialized)
1502
1520
  );
1503
1521
  }
1504
1522
  return copy;
1505
1523
  }
1506
- function walk(value, sensitive, censor, depth2, ancestors, honorToJson = true) {
1524
+ function walk(value, rules, depth2, ancestors, honorToJson = true, inSerialized = false) {
1507
1525
  if (value === null) {
1508
1526
  return value;
1509
1527
  }
@@ -1517,18 +1535,18 @@ function walk(value, sensitive, censor, depth2, ancestors, honorToJson = true) {
1517
1535
  return REDACT_CIRCULAR;
1518
1536
  }
1519
1537
  if (value instanceof Error) {
1520
- return walkError(value, sensitive, censor, depth2, ancestors, honorToJson);
1538
+ return walkError(value, rules, depth2, ancestors, honorToJson, inSerialized);
1521
1539
  }
1522
- return walkObject(value, sensitive, censor, depth2, ancestors, honorToJson);
1540
+ return walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized);
1523
1541
  }
1524
- function walkError(error, sensitive, censor, depth2, ancestors, honorToJson) {
1542
+ function walkError(error, rules, depth2, ancestors, honorToJson, inSerialized) {
1525
1543
  try {
1526
- return walkObject(error, sensitive, censor, depth2, ancestors, honorToJson);
1544
+ return walkObject(error, rules, depth2, ancestors, honorToJson, inSerialized);
1527
1545
  } catch {
1528
- return walk(sanitizeError(error), sensitive, censor, depth2, ancestors, honorToJson);
1546
+ return walk(sanitizeError(error), rules, depth2, ancestors, honorToJson, inSerialized);
1529
1547
  }
1530
1548
  }
1531
- function walkObject(value, sensitive, censor, depth2, ancestors, honorToJson) {
1549
+ function walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized) {
1532
1550
  const resolvedToJson = resolveToJson(value);
1533
1551
  if (!resolvedToJson.fromAccessor && resolvedToJson.method === BUFFER_TO_JSON) {
1534
1552
  return value;
@@ -1537,38 +1555,128 @@ function walkObject(value, sensitive, censor, depth2, ancestors, honorToJson) {
1537
1555
  try {
1538
1556
  const toJson = honorToJson ? resolvedToJson.method : void 0;
1539
1557
  if (toJson !== void 0) {
1540
- if (Object.keys(value).some((key) => sensitive.has(key.toLowerCase()))) {
1541
- return censor;
1558
+ if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
1559
+ return rules.censor;
1542
1560
  }
1543
- return walk(toJson.call(value), sensitive, censor, depth2 + 1, ancestors);
1561
+ return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
1544
1562
  }
1545
1563
  if (typeof value === "function") {
1546
1564
  return value;
1547
1565
  }
1548
1566
  if (Array.isArray(value)) {
1549
- return walkArray(value, sensitive, censor, depth2, ancestors);
1567
+ return walkArray(value, rules, depth2, ancestors, inSerialized);
1550
1568
  }
1551
1569
  const source = value;
1552
1570
  const keys = Object.keys(source);
1553
- const copy = value instanceof Error ? cloneError(value) : {};
1571
+ const asData = value instanceof Error && rules.errorsAsData && !inSerialized;
1572
+ const clone = value instanceof Error ? cloneError(value) : void 0;
1573
+ const copy = clone !== void 0 && !asData ? clone : {};
1574
+ const scrubbed = value instanceof Error && !inSerialized;
1554
1575
  for (const key of keys) {
1555
- const current = Reflect.get(source, key);
1576
+ const current = scrubbed ? ownFieldForLog(source, key, Reflect.get(source, key)) : Reflect.get(source, key);
1577
+ if (current === OMIT_OWN_FIELD) {
1578
+ Reflect.deleteProperty(copy, key);
1579
+ continue;
1580
+ }
1556
1581
  defineOwn(
1557
1582
  copy,
1558
1583
  key,
1559
- sensitive.has(key.toLowerCase()) ? censor : walk(current, sensitive, censor, depth2 + 1, ancestors)
1584
+ rules.sensitive.has(key.toLowerCase()) ? rules.censor : walk(
1585
+ current,
1586
+ rules,
1587
+ depth2 + 1,
1588
+ ancestors,
1589
+ true,
1590
+ inSerialized || !honorToJson && rules.serializedKeys.has(key)
1591
+ )
1560
1592
  );
1561
1593
  }
1594
+ if (asData) addErrorParts(copy, value, keys, rules, depth2, ancestors);
1562
1595
  return copy;
1563
1596
  } finally {
1564
1597
  ancestors.delete(value);
1565
1598
  }
1566
1599
  }
1567
- function createNameRedactor(fieldNames, censor) {
1568
- const sensitive = new Set(fieldNames.map((name) => name.toLowerCase()));
1600
+ var linkDepths = /* @__PURE__ */ new WeakMap();
1601
+ var INCOMPLETE_KEY = "_errorFieldsIncomplete";
1602
+ function capErrors(errors) {
1603
+ if (!Array.isArray(errors) || errors.length <= MAX_AGGREGATE_ERRORS) return errors;
1604
+ if (resolveToJson(errors).method !== void 0) return errors;
1605
+ const kept = [];
1606
+ for (let index = 0; index < MAX_AGGREGATE_ERRORS; index++) kept.push(Reflect.get(errors, index));
1607
+ kept.push(createWidthMarker(errors.length - MAX_AGGREGATE_ERRORS));
1608
+ return kept;
1609
+ }
1610
+ function linkTargets(value) {
1611
+ const targets = [];
1612
+ const take = (link) => {
1613
+ if (typeof link === "object" && link !== null) targets.push(link);
1614
+ };
1615
+ if (!Array.isArray(value)) take(value);
1616
+ else for (let index = 0; index < value.length; index++) take(Reflect.get(value, index));
1617
+ return targets;
1618
+ }
1619
+ function walkChainPart(value, level, rules, depth2, ancestors) {
1620
+ const targets = linkTargets(value);
1621
+ if (targets.length > 0 && level + 1 > DEFAULT_MAX_CAUSE_DEPTH) return createDepthMarker();
1622
+ const before = new Map(targets.map((target) => [target, linkDepths.get(target)]));
1623
+ for (const target of targets) linkDepths.set(target, level + 1);
1624
+ try {
1625
+ return walk(value, rules, depth2 + 1, ancestors);
1626
+ } finally {
1627
+ before.forEach((earlier, target) => {
1628
+ if (earlier === void 0) linkDepths.delete(target);
1629
+ else linkDepths.set(target, earlier);
1630
+ });
1631
+ }
1632
+ }
1633
+ function addErrorParts(copy, error, ownKeys, rules, depth2, ancestors) {
1634
+ const taken = new Set(ownKeys);
1635
+ const level = linkDepths.get(error) ?? 0;
1636
+ let incomplete = false;
1637
+ const add = (key, covers, read, write = (value) => walk(value, rules, depth2 + 1, ancestors)) => {
1638
+ if (taken.has(key)) return;
1639
+ try {
1640
+ const written = write(read());
1641
+ if (written === void 0) return;
1642
+ const covered = covers.some((name) => rules.sensitive.has(name));
1643
+ defineOwn(copy, key, covered ? rules.censor : written);
1644
+ } catch {
1645
+ incomplete = true;
1646
+ }
1647
+ };
1648
+ add("type", ["type", "name"], () => error.name);
1649
+ add("message", ["message"], () => error.message);
1650
+ add(
1651
+ "stack",
1652
+ ["stack", "name", "message"],
1653
+ () => error.stack,
1654
+ (stack) => typeof stack === "string" ? scrubStack(stack) : void 0
1655
+ );
1656
+ add(
1657
+ "cause",
1658
+ ["cause"],
1659
+ () => error.cause,
1660
+ (cause) => walkChainPart(cause, level, rules, depth2, ancestors)
1661
+ );
1662
+ add(
1663
+ "errors",
1664
+ ["errors"],
1665
+ () => capErrors(error.errors),
1666
+ (errors) => walkChainPart(errors, level, rules, depth2, ancestors)
1667
+ );
1668
+ if (incomplete && !taken.has(INCOMPLETE_KEY)) defineOwn(copy, INCOMPLETE_KEY, true);
1669
+ }
1670
+ function createNameRedactor(fieldNames, censor, serializedKeys) {
1671
+ const rules = {
1672
+ sensitive: new Set(fieldNames.map((name) => name.toLowerCase())),
1673
+ censor,
1674
+ serializedKeys: new Set(serializedKeys),
1675
+ errorsAsData: serializedKeys !== void 0
1676
+ };
1569
1677
  return (value, isRecordRoot = false) => {
1570
1678
  try {
1571
- return walk(value, sensitive, censor, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1679
+ return walk(value, rules, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1572
1680
  } catch {
1573
1681
  return {
1574
1682
  _redactionFailed: true,
@@ -1806,9 +1914,9 @@ function resolveNameRedactor(options) {
1806
1914
  if (options.redactStrategy !== "names") {
1807
1915
  return PASS_THROUGH;
1808
1916
  }
1809
- return buildNameRedactor(options);
1917
+ return buildNameRedactor(options, true);
1810
1918
  }
1811
- function buildNameRedactor(options) {
1919
+ function buildNameRedactor(options, nestedErrorsAsData = false) {
1812
1920
  const names = [
1813
1921
  ...options.shouldDisableDefaultRedact ? [] : REDACT_COMMON_FIELDS,
1814
1922
  ...options.redactPaths.flatMap((path) => leafNameOf(path) ?? [])
@@ -1816,7 +1924,7 @@ function buildNameRedactor(options) {
1816
1924
  if (names.length === 0) {
1817
1925
  return PASS_THROUGH;
1818
1926
  }
1819
- return createNameRedactor(names, options.redactCensor);
1927
+ return nestedErrorsAsData ? createNameRedactor(names, options.redactCensor, ["err", ...Object.keys(options.serializers)]) : createNameRedactor(names, options.redactCensor);
1820
1928
  }
1821
1929
  function resolveRedactOption(options) {
1822
1930
  const paths = options.redactStrategy === "paths" ? compileRedactPaths(options.redactPaths, options.shouldDisableDefaultRedact) : [...new Set(options.redactPaths)];
@@ -435,10 +435,28 @@ function toSanitizedError(value, seen, depth2, maxDepth) {
435
435
  return { name: "SanitizeFailed", message: "Failed to sanitize the thrown value" };
436
436
  }
437
437
  }
438
+ var ARGUMENT_FIELDS = /* @__PURE__ */ new Set(["args", "parameters"]);
439
+ var DATABASE_VALUE_FIELDS = /* @__PURE__ */ new Set(["detail", "where", "internalQuery"]);
440
+ var SQLSTATE = /^[0-9A-Z]{5}$/;
441
+ var OMIT_OWN_FIELD = /* @__PURE__ */ Symbol("omit-own-field");
442
+ function isDatabaseError(source) {
443
+ const own = (key) => Object.getOwnPropertyDescriptor(source, key)?.value;
444
+ const code = own("code");
445
+ return typeof own("severity") === "string" && typeof own("routine") === "string" && typeof code === "string" && SQLSTATE.test(code);
446
+ }
447
+ function ownFieldForLog(source, key, value) {
448
+ if (ARGUMENT_FIELDS.has(key.toLowerCase())) return OMIT_OWN_FIELD;
449
+ if (DATABASE_VALUE_FIELDS.has(key) && isDatabaseError(source)) return OMIT_OWN_FIELD;
450
+ if (key !== "command" || typeof value !== "object" || value === null) return value;
451
+ const name = Object.getOwnPropertyDescriptor(value, "name")?.value;
452
+ return typeof name === "string" ? { name } : OMIT_OWN_FIELD;
453
+ }
438
454
  function assignOwnErrorFields(target, source) {
439
455
  try {
440
456
  const own = Array.isArray(source) ? {} : source;
441
- for (const [key, value] of Object.entries(own)) {
457
+ for (const [key, field] of Object.entries(own)) {
458
+ const value = ownFieldForLog(own, key, field);
459
+ if (value === OMIT_OWN_FIELD) continue;
442
460
  if (!Object.hasOwn(target, key) && !DERIVED_ERROR_FIELDS.has(key)) {
443
461
  Object.defineProperty(target, key, { value, enumerable: true });
444
462
  }
@@ -1488,19 +1506,19 @@ var BUFFER_TO_JSON = Buffer.prototype.toJSON;
1488
1506
  function defineOwn(target, key, value) {
1489
1507
  Reflect.defineProperty(target, key, { value, enumerable: true });
1490
1508
  }
1491
- function walkArray(value, sensitive, censor, depth2, ancestors) {
1509
+ function walkArray(value, rules, depth2, ancestors, inSerialized) {
1492
1510
  const length = value.length;
1493
1511
  const copy = [];
1494
1512
  for (let index = 0; index < length; index++) {
1495
1513
  Reflect.set(
1496
1514
  copy,
1497
1515
  index,
1498
- walk(Reflect.get(value, index), sensitive, censor, depth2 + 1, ancestors)
1516
+ walk(Reflect.get(value, index), rules, depth2 + 1, ancestors, true, inSerialized)
1499
1517
  );
1500
1518
  }
1501
1519
  return copy;
1502
1520
  }
1503
- function walk(value, sensitive, censor, depth2, ancestors, honorToJson = true) {
1521
+ function walk(value, rules, depth2, ancestors, honorToJson = true, inSerialized = false) {
1504
1522
  if (value === null) {
1505
1523
  return value;
1506
1524
  }
@@ -1514,18 +1532,18 @@ function walk(value, sensitive, censor, depth2, ancestors, honorToJson = true) {
1514
1532
  return REDACT_CIRCULAR;
1515
1533
  }
1516
1534
  if (value instanceof Error) {
1517
- return walkError(value, sensitive, censor, depth2, ancestors, honorToJson);
1535
+ return walkError(value, rules, depth2, ancestors, honorToJson, inSerialized);
1518
1536
  }
1519
- return walkObject(value, sensitive, censor, depth2, ancestors, honorToJson);
1537
+ return walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized);
1520
1538
  }
1521
- function walkError(error, sensitive, censor, depth2, ancestors, honorToJson) {
1539
+ function walkError(error, rules, depth2, ancestors, honorToJson, inSerialized) {
1522
1540
  try {
1523
- return walkObject(error, sensitive, censor, depth2, ancestors, honorToJson);
1541
+ return walkObject(error, rules, depth2, ancestors, honorToJson, inSerialized);
1524
1542
  } catch {
1525
- return walk(sanitizeError(error), sensitive, censor, depth2, ancestors, honorToJson);
1543
+ return walk(sanitizeError(error), rules, depth2, ancestors, honorToJson, inSerialized);
1526
1544
  }
1527
1545
  }
1528
- function walkObject(value, sensitive, censor, depth2, ancestors, honorToJson) {
1546
+ function walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized) {
1529
1547
  const resolvedToJson = resolveToJson(value);
1530
1548
  if (!resolvedToJson.fromAccessor && resolvedToJson.method === BUFFER_TO_JSON) {
1531
1549
  return value;
@@ -1534,38 +1552,128 @@ function walkObject(value, sensitive, censor, depth2, ancestors, honorToJson) {
1534
1552
  try {
1535
1553
  const toJson = honorToJson ? resolvedToJson.method : void 0;
1536
1554
  if (toJson !== void 0) {
1537
- if (Object.keys(value).some((key) => sensitive.has(key.toLowerCase()))) {
1538
- return censor;
1555
+ if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
1556
+ return rules.censor;
1539
1557
  }
1540
- return walk(toJson.call(value), sensitive, censor, depth2 + 1, ancestors);
1558
+ return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
1541
1559
  }
1542
1560
  if (typeof value === "function") {
1543
1561
  return value;
1544
1562
  }
1545
1563
  if (Array.isArray(value)) {
1546
- return walkArray(value, sensitive, censor, depth2, ancestors);
1564
+ return walkArray(value, rules, depth2, ancestors, inSerialized);
1547
1565
  }
1548
1566
  const source = value;
1549
1567
  const keys = Object.keys(source);
1550
- const copy = value instanceof Error ? cloneError(value) : {};
1568
+ const asData = value instanceof Error && rules.errorsAsData && !inSerialized;
1569
+ const clone = value instanceof Error ? cloneError(value) : void 0;
1570
+ const copy = clone !== void 0 && !asData ? clone : {};
1571
+ const scrubbed = value instanceof Error && !inSerialized;
1551
1572
  for (const key of keys) {
1552
- const current = Reflect.get(source, key);
1573
+ const current = scrubbed ? ownFieldForLog(source, key, Reflect.get(source, key)) : Reflect.get(source, key);
1574
+ if (current === OMIT_OWN_FIELD) {
1575
+ Reflect.deleteProperty(copy, key);
1576
+ continue;
1577
+ }
1553
1578
  defineOwn(
1554
1579
  copy,
1555
1580
  key,
1556
- sensitive.has(key.toLowerCase()) ? censor : walk(current, sensitive, censor, depth2 + 1, ancestors)
1581
+ rules.sensitive.has(key.toLowerCase()) ? rules.censor : walk(
1582
+ current,
1583
+ rules,
1584
+ depth2 + 1,
1585
+ ancestors,
1586
+ true,
1587
+ inSerialized || !honorToJson && rules.serializedKeys.has(key)
1588
+ )
1557
1589
  );
1558
1590
  }
1591
+ if (asData) addErrorParts(copy, value, keys, rules, depth2, ancestors);
1559
1592
  return copy;
1560
1593
  } finally {
1561
1594
  ancestors.delete(value);
1562
1595
  }
1563
1596
  }
1564
- function createNameRedactor(fieldNames, censor) {
1565
- const sensitive = new Set(fieldNames.map((name) => name.toLowerCase()));
1597
+ var linkDepths = /* @__PURE__ */ new WeakMap();
1598
+ var INCOMPLETE_KEY = "_errorFieldsIncomplete";
1599
+ function capErrors(errors) {
1600
+ if (!Array.isArray(errors) || errors.length <= MAX_AGGREGATE_ERRORS) return errors;
1601
+ if (resolveToJson(errors).method !== void 0) return errors;
1602
+ const kept = [];
1603
+ for (let index = 0; index < MAX_AGGREGATE_ERRORS; index++) kept.push(Reflect.get(errors, index));
1604
+ kept.push(createWidthMarker(errors.length - MAX_AGGREGATE_ERRORS));
1605
+ return kept;
1606
+ }
1607
+ function linkTargets(value) {
1608
+ const targets = [];
1609
+ const take = (link) => {
1610
+ if (typeof link === "object" && link !== null) targets.push(link);
1611
+ };
1612
+ if (!Array.isArray(value)) take(value);
1613
+ else for (let index = 0; index < value.length; index++) take(Reflect.get(value, index));
1614
+ return targets;
1615
+ }
1616
+ function walkChainPart(value, level, rules, depth2, ancestors) {
1617
+ const targets = linkTargets(value);
1618
+ if (targets.length > 0 && level + 1 > DEFAULT_MAX_CAUSE_DEPTH) return createDepthMarker();
1619
+ const before = new Map(targets.map((target) => [target, linkDepths.get(target)]));
1620
+ for (const target of targets) linkDepths.set(target, level + 1);
1621
+ try {
1622
+ return walk(value, rules, depth2 + 1, ancestors);
1623
+ } finally {
1624
+ before.forEach((earlier, target) => {
1625
+ if (earlier === void 0) linkDepths.delete(target);
1626
+ else linkDepths.set(target, earlier);
1627
+ });
1628
+ }
1629
+ }
1630
+ function addErrorParts(copy, error, ownKeys, rules, depth2, ancestors) {
1631
+ const taken = new Set(ownKeys);
1632
+ const level = linkDepths.get(error) ?? 0;
1633
+ let incomplete = false;
1634
+ const add = (key, covers, read, write = (value) => walk(value, rules, depth2 + 1, ancestors)) => {
1635
+ if (taken.has(key)) return;
1636
+ try {
1637
+ const written = write(read());
1638
+ if (written === void 0) return;
1639
+ const covered = covers.some((name) => rules.sensitive.has(name));
1640
+ defineOwn(copy, key, covered ? rules.censor : written);
1641
+ } catch {
1642
+ incomplete = true;
1643
+ }
1644
+ };
1645
+ add("type", ["type", "name"], () => error.name);
1646
+ add("message", ["message"], () => error.message);
1647
+ add(
1648
+ "stack",
1649
+ ["stack", "name", "message"],
1650
+ () => error.stack,
1651
+ (stack) => typeof stack === "string" ? scrubStack(stack) : void 0
1652
+ );
1653
+ add(
1654
+ "cause",
1655
+ ["cause"],
1656
+ () => error.cause,
1657
+ (cause) => walkChainPart(cause, level, rules, depth2, ancestors)
1658
+ );
1659
+ add(
1660
+ "errors",
1661
+ ["errors"],
1662
+ () => capErrors(error.errors),
1663
+ (errors) => walkChainPart(errors, level, rules, depth2, ancestors)
1664
+ );
1665
+ if (incomplete && !taken.has(INCOMPLETE_KEY)) defineOwn(copy, INCOMPLETE_KEY, true);
1666
+ }
1667
+ function createNameRedactor(fieldNames, censor, serializedKeys) {
1668
+ const rules = {
1669
+ sensitive: new Set(fieldNames.map((name) => name.toLowerCase())),
1670
+ censor,
1671
+ serializedKeys: new Set(serializedKeys),
1672
+ errorsAsData: serializedKeys !== void 0
1673
+ };
1566
1674
  return (value, isRecordRoot = false) => {
1567
1675
  try {
1568
- return walk(value, sensitive, censor, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1676
+ return walk(value, rules, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1569
1677
  } catch {
1570
1678
  return {
1571
1679
  _redactionFailed: true,
@@ -1803,9 +1911,9 @@ function resolveNameRedactor(options) {
1803
1911
  if (options.redactStrategy !== "names") {
1804
1912
  return PASS_THROUGH;
1805
1913
  }
1806
- return buildNameRedactor(options);
1914
+ return buildNameRedactor(options, true);
1807
1915
  }
1808
- function buildNameRedactor(options) {
1916
+ function buildNameRedactor(options, nestedErrorsAsData = false) {
1809
1917
  const names = [
1810
1918
  ...options.shouldDisableDefaultRedact ? [] : REDACT_COMMON_FIELDS,
1811
1919
  ...options.redactPaths.flatMap((path2) => leafNameOf(path2) ?? [])
@@ -1813,7 +1921,7 @@ function buildNameRedactor(options) {
1813
1921
  if (names.length === 0) {
1814
1922
  return PASS_THROUGH;
1815
1923
  }
1816
- return createNameRedactor(names, options.redactCensor);
1924
+ return nestedErrorsAsData ? createNameRedactor(names, options.redactCensor, ["err", ...Object.keys(options.serializers)]) : createNameRedactor(names, options.redactCensor);
1817
1925
  }
1818
1926
  function resolveRedactOption(options) {
1819
1927
  const paths = options.redactStrategy === "paths" ? compileRedactPaths(options.redactPaths, options.shouldDisableDefaultRedact) : [...new Set(options.redactPaths)];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bymax-one/nest-logger",
3
- "version": "1.4.8",
3
+ "version": "1.4.9",
4
4
  "description": "Structured JSON logging for NestJS based on Pino 10, with optional OpenTelemetry correlation.",
5
5
  "author": "Bymax One <support@bymax.one>",
6
6
  "license": "MIT",