@bymax-one/nest-logger 1.4.7 → 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,108 @@ 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
+
89
+ ## [1.4.8] - 2026-11-01
90
+
91
+ ### Fixed
92
+
93
+ - **A logging call no longer throws because of what it was handed.** `error`, `warn`, `log`, `fatal`
94
+ and the other NestJS variadic methods threw out of the call when an `Error` had a `name` or `message`
95
+ getter that throws, and when the message had no text form (a null-prototype object, a throwing
96
+ `toString`). In `catch (e) { logger.error(e) }` that second throw replaced the original error and
97
+ nothing was logged. The call now returns and the entry is written: an unreadable `Error` arrives as
98
+ the same `err` node `errorStructured` produces, with `err.type: 'SanitizeFailed'`, and a message with
99
+ no text form is logged as `Unreadable log message`.
100
+
101
+ #### Apply to a derived backend
102
+
103
+ Nothing to change in code. What can differ at run time:
104
+
105
+ - Entries that were never written now appear. A host that handled the throw (a `try` around the logging
106
+ call, or an error handler that reacted to it) no longer sees it; one that relied on the throw to detect
107
+ a hostile input should look for `err.type: 'SanitizeFailed'` (an `Error`) or the message
108
+ `Unreadable log message` (a message) instead.
109
+
110
+ ### Tests
111
+
112
+ - Each variadic position with an unreadable `name` or `message`, and each message with no text form on
113
+ `error`, `warn` and `log`, has a failing-first spec through the real Pino pipeline, with a control that
114
+ a readable error is logged as before.
115
+
14
116
  ## [1.4.7] - 2026-11-01
15
117
 
16
118
  ### Fixed
@@ -2311,7 +2413,9 @@ published `dist/` is identical — no runtime behaviour changes for consumers.
2311
2413
  - Professional CI suite: `ci.yml`, `bench.yml`, `codeql.yml`, `scorecard.yml`,
2312
2414
  `release.yml`, Dependabot, and issue templates
2313
2415
 
2314
- [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.7...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
2418
+ [1.4.8]: https://github.com/bymaxone/nest-logger/compare/v1.4.7...v1.4.8
2315
2419
  [1.4.7]: https://github.com/bymaxone/nest-logger/compare/v1.4.6...v1.4.7
2316
2420
  [1.4.6]: https://github.com/bymaxone/nest-logger/compare/v1.4.5...v1.4.6
2317
2421
  [1.4.5]: https://github.com/bymaxone/nest-logger/compare/v1.4.4...v1.4.5
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
@@ -386,15 +386,29 @@ function sanitizeError(err, options) {
386
386
  return toSanitizedError(err, /* @__PURE__ */ new WeakSet(), 0, maxCauseDepth);
387
387
  }
388
388
  function isErrorLike(value) {
389
+ try {
390
+ return readsAsError(value);
391
+ } catch {
392
+ return isErrorInstance(value);
393
+ }
394
+ }
395
+ function readsAsError(value) {
389
396
  if (typeof value !== "object" || value === null) {
390
397
  return false;
391
398
  }
392
399
  const candidate = value;
393
400
  return typeof candidate.name === "string" && typeof candidate.message === "string";
394
401
  }
402
+ function isErrorInstance(value) {
403
+ try {
404
+ return value instanceof Error;
405
+ } catch {
406
+ return false;
407
+ }
408
+ }
395
409
  function toSanitizedError(value, seen, depth2, maxDepth) {
396
410
  try {
397
- if (!isErrorLike(value)) {
411
+ if (!readsAsError(value)) {
398
412
  return { name: "UnknownError", message: String(value) };
399
413
  }
400
414
  seen.add(value);
@@ -424,10 +438,28 @@ function toSanitizedError(value, seen, depth2, maxDepth) {
424
438
  return { name: "SanitizeFailed", message: "Failed to sanitize the thrown value" };
425
439
  }
426
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
+ }
427
457
  function assignOwnErrorFields(target, source) {
428
458
  try {
429
459
  const own = Array.isArray(source) ? {} : source;
430
- 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;
431
463
  if (!Object.hasOwn(target, key) && !DERIVED_ERROR_FIELDS.has(key)) {
432
464
  Object.defineProperty(target, key, { value, enumerable: true });
433
465
  }
@@ -500,6 +532,13 @@ function safeErrorMessage(error) {
500
532
  return "Unreadable error message";
501
533
  }
502
534
  }
535
+ function safeLogMessage(message) {
536
+ try {
537
+ return toSingleLineMessage(typeof message === "string" ? message : String(message));
538
+ } catch {
539
+ return "Unreadable log message";
540
+ }
541
+ }
503
542
  function findErrorParam(optionalParams) {
504
543
  return optionalParams.find((param) => isErrorLike(param));
505
544
  }
@@ -571,10 +610,7 @@ exports.PinoLoggerService = class PinoLoggerService {
571
610
  assignIfDefined(payload, "context", context);
572
611
  assignIfDefined(payload, "stack", stack);
573
612
  assignIfDefined(payload, "err", findErrorParam(optionalParams));
574
- this.pino.error(
575
- payload,
576
- toSingleLineMessage(typeof message === "string" ? message : String(message))
577
- );
613
+ this.pino.error(payload, safeLogMessage(message));
578
614
  }
579
615
  /**
580
616
  * NestJS `warn` (warn level).
@@ -731,7 +767,7 @@ exports.PinoLoggerService = class PinoLoggerService {
731
767
  assignIfDefined(payload, "context", this.resolveContext(optionalParams));
732
768
  const leading = isErrorLike(message) ? message : void 0;
733
769
  assignIfDefined(payload, "err", leading ?? findErrorParam(optionalParams));
734
- const msg = leading === void 0 ? toSingleLineMessage(typeof message === "string" ? message : String(message)) : safeErrorMessage(leading);
770
+ const msg = leading === void 0 ? safeLogMessage(message) : safeErrorMessage(leading);
735
771
  switch (level) {
736
772
  case "info":
737
773
  this.pino.info(payload, msg);
@@ -1473,19 +1509,19 @@ var BUFFER_TO_JSON = Buffer.prototype.toJSON;
1473
1509
  function defineOwn(target, key, value) {
1474
1510
  Reflect.defineProperty(target, key, { value, enumerable: true });
1475
1511
  }
1476
- function walkArray(value, sensitive, censor, depth2, ancestors) {
1512
+ function walkArray(value, rules, depth2, ancestors, inSerialized) {
1477
1513
  const length = value.length;
1478
1514
  const copy = [];
1479
1515
  for (let index = 0; index < length; index++) {
1480
1516
  Reflect.set(
1481
1517
  copy,
1482
1518
  index,
1483
- walk(Reflect.get(value, index), sensitive, censor, depth2 + 1, ancestors)
1519
+ walk(Reflect.get(value, index), rules, depth2 + 1, ancestors, true, inSerialized)
1484
1520
  );
1485
1521
  }
1486
1522
  return copy;
1487
1523
  }
1488
- function walk(value, sensitive, censor, depth2, ancestors, honorToJson = true) {
1524
+ function walk(value, rules, depth2, ancestors, honorToJson = true, inSerialized = false) {
1489
1525
  if (value === null) {
1490
1526
  return value;
1491
1527
  }
@@ -1499,18 +1535,18 @@ function walk(value, sensitive, censor, depth2, ancestors, honorToJson = true) {
1499
1535
  return REDACT_CIRCULAR;
1500
1536
  }
1501
1537
  if (value instanceof Error) {
1502
- return walkError(value, sensitive, censor, depth2, ancestors, honorToJson);
1538
+ return walkError(value, rules, depth2, ancestors, honorToJson, inSerialized);
1503
1539
  }
1504
- return walkObject(value, sensitive, censor, depth2, ancestors, honorToJson);
1540
+ return walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized);
1505
1541
  }
1506
- function walkError(error, sensitive, censor, depth2, ancestors, honorToJson) {
1542
+ function walkError(error, rules, depth2, ancestors, honorToJson, inSerialized) {
1507
1543
  try {
1508
- return walkObject(error, sensitive, censor, depth2, ancestors, honorToJson);
1544
+ return walkObject(error, rules, depth2, ancestors, honorToJson, inSerialized);
1509
1545
  } catch {
1510
- return walk(sanitizeError(error), sensitive, censor, depth2, ancestors, honorToJson);
1546
+ return walk(sanitizeError(error), rules, depth2, ancestors, honorToJson, inSerialized);
1511
1547
  }
1512
1548
  }
1513
- function walkObject(value, sensitive, censor, depth2, ancestors, honorToJson) {
1549
+ function walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized) {
1514
1550
  const resolvedToJson = resolveToJson(value);
1515
1551
  if (!resolvedToJson.fromAccessor && resolvedToJson.method === BUFFER_TO_JSON) {
1516
1552
  return value;
@@ -1519,38 +1555,128 @@ function walkObject(value, sensitive, censor, depth2, ancestors, honorToJson) {
1519
1555
  try {
1520
1556
  const toJson = honorToJson ? resolvedToJson.method : void 0;
1521
1557
  if (toJson !== void 0) {
1522
- if (Object.keys(value).some((key) => sensitive.has(key.toLowerCase()))) {
1523
- return censor;
1558
+ if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
1559
+ return rules.censor;
1524
1560
  }
1525
- return walk(toJson.call(value), sensitive, censor, depth2 + 1, ancestors);
1561
+ return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
1526
1562
  }
1527
1563
  if (typeof value === "function") {
1528
1564
  return value;
1529
1565
  }
1530
1566
  if (Array.isArray(value)) {
1531
- return walkArray(value, sensitive, censor, depth2, ancestors);
1567
+ return walkArray(value, rules, depth2, ancestors, inSerialized);
1532
1568
  }
1533
1569
  const source = value;
1534
1570
  const keys = Object.keys(source);
1535
- 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;
1536
1575
  for (const key of keys) {
1537
- 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
+ }
1538
1581
  defineOwn(
1539
1582
  copy,
1540
1583
  key,
1541
- 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
+ )
1542
1592
  );
1543
1593
  }
1594
+ if (asData) addErrorParts(copy, value, keys, rules, depth2, ancestors);
1544
1595
  return copy;
1545
1596
  } finally {
1546
1597
  ancestors.delete(value);
1547
1598
  }
1548
1599
  }
1549
- function createNameRedactor(fieldNames, censor) {
1550
- 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
+ };
1551
1677
  return (value, isRecordRoot = false) => {
1552
1678
  try {
1553
- return walk(value, sensitive, censor, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1679
+ return walk(value, rules, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1554
1680
  } catch {
1555
1681
  return {
1556
1682
  _redactionFailed: true,
@@ -1788,9 +1914,9 @@ function resolveNameRedactor(options) {
1788
1914
  if (options.redactStrategy !== "names") {
1789
1915
  return PASS_THROUGH;
1790
1916
  }
1791
- return buildNameRedactor(options);
1917
+ return buildNameRedactor(options, true);
1792
1918
  }
1793
- function buildNameRedactor(options) {
1919
+ function buildNameRedactor(options, nestedErrorsAsData = false) {
1794
1920
  const names = [
1795
1921
  ...options.shouldDisableDefaultRedact ? [] : REDACT_COMMON_FIELDS,
1796
1922
  ...options.redactPaths.flatMap((path) => leafNameOf(path) ?? [])
@@ -1798,7 +1924,7 @@ function buildNameRedactor(options) {
1798
1924
  if (names.length === 0) {
1799
1925
  return PASS_THROUGH;
1800
1926
  }
1801
- return createNameRedactor(names, options.redactCensor);
1927
+ return nestedErrorsAsData ? createNameRedactor(names, options.redactCensor, ["err", ...Object.keys(options.serializers)]) : createNameRedactor(names, options.redactCensor);
1802
1928
  }
1803
1929
  function resolveRedactOption(options) {
1804
1930
  const paths = options.redactStrategy === "paths" ? compileRedactPaths(options.redactPaths, options.shouldDisableDefaultRedact) : [...new Set(options.redactPaths)];
@@ -3218,11 +3344,7 @@ exports.PinoUnexpectedErrorSink = __decorateClass([
3218
3344
  __decorateParam(0, common.Inject(exports.PinoLoggerService))
3219
3345
  ], exports.PinoUnexpectedErrorSink);
3220
3346
  function toLoggableError(value) {
3221
- try {
3222
- return isErrorLike(value) ? value : sanitizeError(value);
3223
- } catch {
3224
- return sanitizeError(value);
3225
- }
3347
+ return isErrorLike(value) ? value : sanitizeError(value);
3226
3348
  }
3227
3349
  var INJECT_LOGGER_CONTEXT_METADATA_KEY = "bymax_logger:context";
3228
3350
  function InjectLogger(context) {
@@ -383,15 +383,29 @@ function sanitizeError(err, options) {
383
383
  return toSanitizedError(err, /* @__PURE__ */ new WeakSet(), 0, maxCauseDepth);
384
384
  }
385
385
  function isErrorLike(value) {
386
+ try {
387
+ return readsAsError(value);
388
+ } catch {
389
+ return isErrorInstance(value);
390
+ }
391
+ }
392
+ function readsAsError(value) {
386
393
  if (typeof value !== "object" || value === null) {
387
394
  return false;
388
395
  }
389
396
  const candidate = value;
390
397
  return typeof candidate.name === "string" && typeof candidate.message === "string";
391
398
  }
399
+ function isErrorInstance(value) {
400
+ try {
401
+ return value instanceof Error;
402
+ } catch {
403
+ return false;
404
+ }
405
+ }
392
406
  function toSanitizedError(value, seen, depth2, maxDepth) {
393
407
  try {
394
- if (!isErrorLike(value)) {
408
+ if (!readsAsError(value)) {
395
409
  return { name: "UnknownError", message: String(value) };
396
410
  }
397
411
  seen.add(value);
@@ -421,10 +435,28 @@ function toSanitizedError(value, seen, depth2, maxDepth) {
421
435
  return { name: "SanitizeFailed", message: "Failed to sanitize the thrown value" };
422
436
  }
423
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
+ }
424
454
  function assignOwnErrorFields(target, source) {
425
455
  try {
426
456
  const own = Array.isArray(source) ? {} : source;
427
- 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;
428
460
  if (!Object.hasOwn(target, key) && !DERIVED_ERROR_FIELDS.has(key)) {
429
461
  Object.defineProperty(target, key, { value, enumerable: true });
430
462
  }
@@ -497,6 +529,13 @@ function safeErrorMessage(error) {
497
529
  return "Unreadable error message";
498
530
  }
499
531
  }
532
+ function safeLogMessage(message) {
533
+ try {
534
+ return toSingleLineMessage(typeof message === "string" ? message : String(message));
535
+ } catch {
536
+ return "Unreadable log message";
537
+ }
538
+ }
500
539
  function findErrorParam(optionalParams) {
501
540
  return optionalParams.find((param) => isErrorLike(param));
502
541
  }
@@ -568,10 +607,7 @@ var PinoLoggerService = class {
568
607
  assignIfDefined(payload, "context", context);
569
608
  assignIfDefined(payload, "stack", stack);
570
609
  assignIfDefined(payload, "err", findErrorParam(optionalParams));
571
- this.pino.error(
572
- payload,
573
- toSingleLineMessage(typeof message === "string" ? message : String(message))
574
- );
610
+ this.pino.error(payload, safeLogMessage(message));
575
611
  }
576
612
  /**
577
613
  * NestJS `warn` (warn level).
@@ -728,7 +764,7 @@ var PinoLoggerService = class {
728
764
  assignIfDefined(payload, "context", this.resolveContext(optionalParams));
729
765
  const leading = isErrorLike(message) ? message : void 0;
730
766
  assignIfDefined(payload, "err", leading ?? findErrorParam(optionalParams));
731
- const msg = leading === void 0 ? toSingleLineMessage(typeof message === "string" ? message : String(message)) : safeErrorMessage(leading);
767
+ const msg = leading === void 0 ? safeLogMessage(message) : safeErrorMessage(leading);
732
768
  switch (level) {
733
769
  case "info":
734
770
  this.pino.info(payload, msg);
@@ -1470,19 +1506,19 @@ var BUFFER_TO_JSON = Buffer.prototype.toJSON;
1470
1506
  function defineOwn(target, key, value) {
1471
1507
  Reflect.defineProperty(target, key, { value, enumerable: true });
1472
1508
  }
1473
- function walkArray(value, sensitive, censor, depth2, ancestors) {
1509
+ function walkArray(value, rules, depth2, ancestors, inSerialized) {
1474
1510
  const length = value.length;
1475
1511
  const copy = [];
1476
1512
  for (let index = 0; index < length; index++) {
1477
1513
  Reflect.set(
1478
1514
  copy,
1479
1515
  index,
1480
- walk(Reflect.get(value, index), sensitive, censor, depth2 + 1, ancestors)
1516
+ walk(Reflect.get(value, index), rules, depth2 + 1, ancestors, true, inSerialized)
1481
1517
  );
1482
1518
  }
1483
1519
  return copy;
1484
1520
  }
1485
- function walk(value, sensitive, censor, depth2, ancestors, honorToJson = true) {
1521
+ function walk(value, rules, depth2, ancestors, honorToJson = true, inSerialized = false) {
1486
1522
  if (value === null) {
1487
1523
  return value;
1488
1524
  }
@@ -1496,18 +1532,18 @@ function walk(value, sensitive, censor, depth2, ancestors, honorToJson = true) {
1496
1532
  return REDACT_CIRCULAR;
1497
1533
  }
1498
1534
  if (value instanceof Error) {
1499
- return walkError(value, sensitive, censor, depth2, ancestors, honorToJson);
1535
+ return walkError(value, rules, depth2, ancestors, honorToJson, inSerialized);
1500
1536
  }
1501
- return walkObject(value, sensitive, censor, depth2, ancestors, honorToJson);
1537
+ return walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized);
1502
1538
  }
1503
- function walkError(error, sensitive, censor, depth2, ancestors, honorToJson) {
1539
+ function walkError(error, rules, depth2, ancestors, honorToJson, inSerialized) {
1504
1540
  try {
1505
- return walkObject(error, sensitive, censor, depth2, ancestors, honorToJson);
1541
+ return walkObject(error, rules, depth2, ancestors, honorToJson, inSerialized);
1506
1542
  } catch {
1507
- return walk(sanitizeError(error), sensitive, censor, depth2, ancestors, honorToJson);
1543
+ return walk(sanitizeError(error), rules, depth2, ancestors, honorToJson, inSerialized);
1508
1544
  }
1509
1545
  }
1510
- function walkObject(value, sensitive, censor, depth2, ancestors, honorToJson) {
1546
+ function walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized) {
1511
1547
  const resolvedToJson = resolveToJson(value);
1512
1548
  if (!resolvedToJson.fromAccessor && resolvedToJson.method === BUFFER_TO_JSON) {
1513
1549
  return value;
@@ -1516,38 +1552,128 @@ function walkObject(value, sensitive, censor, depth2, ancestors, honorToJson) {
1516
1552
  try {
1517
1553
  const toJson = honorToJson ? resolvedToJson.method : void 0;
1518
1554
  if (toJson !== void 0) {
1519
- if (Object.keys(value).some((key) => sensitive.has(key.toLowerCase()))) {
1520
- return censor;
1555
+ if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
1556
+ return rules.censor;
1521
1557
  }
1522
- return walk(toJson.call(value), sensitive, censor, depth2 + 1, ancestors);
1558
+ return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
1523
1559
  }
1524
1560
  if (typeof value === "function") {
1525
1561
  return value;
1526
1562
  }
1527
1563
  if (Array.isArray(value)) {
1528
- return walkArray(value, sensitive, censor, depth2, ancestors);
1564
+ return walkArray(value, rules, depth2, ancestors, inSerialized);
1529
1565
  }
1530
1566
  const source = value;
1531
1567
  const keys = Object.keys(source);
1532
- 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;
1533
1572
  for (const key of keys) {
1534
- 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
+ }
1535
1578
  defineOwn(
1536
1579
  copy,
1537
1580
  key,
1538
- 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
+ )
1539
1589
  );
1540
1590
  }
1591
+ if (asData) addErrorParts(copy, value, keys, rules, depth2, ancestors);
1541
1592
  return copy;
1542
1593
  } finally {
1543
1594
  ancestors.delete(value);
1544
1595
  }
1545
1596
  }
1546
- function createNameRedactor(fieldNames, censor) {
1547
- 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
+ };
1548
1674
  return (value, isRecordRoot = false) => {
1549
1675
  try {
1550
- return walk(value, sensitive, censor, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1676
+ return walk(value, rules, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1551
1677
  } catch {
1552
1678
  return {
1553
1679
  _redactionFailed: true,
@@ -1785,9 +1911,9 @@ function resolveNameRedactor(options) {
1785
1911
  if (options.redactStrategy !== "names") {
1786
1912
  return PASS_THROUGH;
1787
1913
  }
1788
- return buildNameRedactor(options);
1914
+ return buildNameRedactor(options, true);
1789
1915
  }
1790
- function buildNameRedactor(options) {
1916
+ function buildNameRedactor(options, nestedErrorsAsData = false) {
1791
1917
  const names = [
1792
1918
  ...options.shouldDisableDefaultRedact ? [] : REDACT_COMMON_FIELDS,
1793
1919
  ...options.redactPaths.flatMap((path2) => leafNameOf(path2) ?? [])
@@ -1795,7 +1921,7 @@ function buildNameRedactor(options) {
1795
1921
  if (names.length === 0) {
1796
1922
  return PASS_THROUGH;
1797
1923
  }
1798
- return createNameRedactor(names, options.redactCensor);
1924
+ return nestedErrorsAsData ? createNameRedactor(names, options.redactCensor, ["err", ...Object.keys(options.serializers)]) : createNameRedactor(names, options.redactCensor);
1799
1925
  }
1800
1926
  function resolveRedactOption(options) {
1801
1927
  const paths = options.redactStrategy === "paths" ? compileRedactPaths(options.redactPaths, options.shouldDisableDefaultRedact) : [...new Set(options.redactPaths)];
@@ -3215,11 +3341,7 @@ PinoUnexpectedErrorSink = __decorateClass([
3215
3341
  __decorateParam(0, Inject(PinoLoggerService))
3216
3342
  ], PinoUnexpectedErrorSink);
3217
3343
  function toLoggableError(value) {
3218
- try {
3219
- return isErrorLike(value) ? value : sanitizeError(value);
3220
- } catch {
3221
- return sanitizeError(value);
3222
- }
3344
+ return isErrorLike(value) ? value : sanitizeError(value);
3223
3345
  }
3224
3346
  var INJECT_LOGGER_CONTEXT_METADATA_KEY = "bymax_logger:context";
3225
3347
  function InjectLogger(context) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bymax-one/nest-logger",
3
- "version": "1.4.7",
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",