@webpieces/bunyan 0.4.400 → 0.4.401

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webpieces/bunyan",
3
- "version": "0.4.400",
3
+ "version": "0.4.401",
4
4
  "description": "Node-only bunyan LoggerFactory backends for webpieces: Console (local pretty) + GCP (@google-cloud/logging-bunyan), auto-enriched with HeaderRegistry context keys",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -23,8 +23,8 @@
23
23
  "access": "public"
24
24
  },
25
25
  "dependencies": {
26
- "@webpieces/core-util": "0.4.400",
27
- "@webpieces/core-context": "0.4.400",
26
+ "@webpieces/core-util": "0.4.401",
27
+ "@webpieces/core-context": "0.4.401",
28
28
  "bunyan": "1.8.15",
29
29
  "@google-cloud/logging-bunyan": "5.1.0",
30
30
  "@types/bunyan": "1.8.11"
@@ -14,7 +14,6 @@ import type { Logger as WpLogger } from '@webpieces/core-util';
14
14
  */
15
15
  export declare class BunyanLogger implements WpLogger {
16
16
  private readonly bunyan;
17
- private reportedMissingContext;
18
17
  constructor(bunyan: Logger);
19
18
  trace(message: string, err?: Error): void;
20
19
  debug(message: string, err?: Error): void;
@@ -22,5 +21,4 @@ export declare class BunyanLogger implements WpLogger {
22
21
  warn(message: string, err?: Error): void;
23
22
  error(message: string, err?: Error): void;
24
23
  private buildFields;
25
- private reportMissingContextOnce;
26
24
  }
@@ -3,11 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BunyanLogger = void 0;
4
4
  const core_context_1 = require("@webpieces/core-context");
5
5
  const LoggedError_1 = require("./LoggedError");
6
- // A log line with no active RequestContext = a missing request-wrapping server
7
- // filter. We report that once (below), never on every line.
8
- const MISSING_CONTEXT_MESSAGE = 'Log emitted OUTSIDE RequestContext.run(...) — every request must be wrapped in ' +
9
- 'RequestContext.run() by a server filter. That filter appears to be missing: correlation ' +
10
- 'fields (requestId, tenant, ...) will be absent from logs until it is added. Reported once.';
11
6
  /**
12
7
  * The error, whole. There is deliberately NO size guard here any more.
13
8
  *
@@ -35,9 +30,6 @@ function normalizeError(err) {
35
30
  */
36
31
  class BunyanLogger {
37
32
  bunyan;
38
- // One-shot latch (see MISSING_CONTEXT_MESSAGE) — flips true after the first
39
- // out-of-context line so we report the missing filter exactly once, not per line.
40
- reportedMissingContext = false;
41
33
  constructor(bunyan) {
42
34
  this.bunyan = bunyan;
43
35
  }
@@ -58,8 +50,11 @@ class BunyanLogger {
58
50
  }
59
51
  buildFields(err) {
60
52
  const fields = {};
53
+ // No active RequestContext (startup, a background job, or an in-process call the caller did
54
+ // not wrap) simply logs with no context fields — an empty context is normal, not an error.
55
+ // The in-process client's ApiClientFactory.requireActiveContext() throws precisely when a
56
+ // request-path wrap is genuinely missing.
61
57
  if (!core_context_1.RequestContext.isActive()) {
62
- this.reportMissingContextOnce();
63
58
  return fields;
64
59
  }
65
60
  // ONE loop, in HeaderRegistry.buildStructuredLogFields — the registry owns the keys and each
@@ -74,15 +69,6 @@ class BunyanLogger {
74
69
  }
75
70
  return fields;
76
71
  }
77
- // Report the missing request-wrapping filter once. Uses the RAW bunyan logger
78
- // (not buildFields()), so there is no re-entrancy — just a single extra line.
79
- reportMissingContextOnce() {
80
- if (this.reportedMissingContext) {
81
- return;
82
- }
83
- this.reportedMissingContext = true;
84
- this.bunyan.error(MISSING_CONTEXT_MESSAGE);
85
- }
86
72
  }
87
73
  exports.BunyanLogger = BunyanLogger;
88
74
  //# sourceMappingURL=BunyanLogger.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"BunyanLogger.js","sourceRoot":"","sources":["../../../../../packages/logging/bunyan/src/BunyanLogger.ts"],"names":[],"mappings":";;;AAEA,0DAAyD;AACzD,+CAA4C;AAE5C,+EAA+E;AAC/E,4DAA4D;AAC5D,MAAM,uBAAuB,GACzB,iFAAiF;IACjF,0FAA0F;IAC1F,4FAA4F,C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C7B,IAAI,CAAC,wBAAwB,EAAE,CAAC;YAChC,OAAO,MAAM,CAAC;QAClB,CAAC;QAED,6FAA6F;QAC7F,iGAAiG;QACjG,8FAA8F;QAC9F,6CAA6C;QAC7C,6BAAc,CAAC,wBAAwB,EAAE,CAAC,OAAO,CAAC,CAAC,KAAsB,EAAE,IAAY,EAAE,EAAE;YACvF,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QACzB,CAAC,CAAC,CAAC;QACH,IAAI,GAAG,EAAE,CAAC;YACN,MAAM,CAAC,KAAK,CAAC,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,8EAA8E;IAC9E,8EAA8E;IACtE,wBAAwB;QAC5B,IAAI,IAAI,CAAC,sBAAsB,EAAE,CAAC;YAC9B,OAAO;QACX,CAAC;QACD,IAAI,CAAC,sBAAsB,GAAG,IAAI,CAAC;QACnC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC/C,CAAC;CACJ;AAxDD,oCAwDC","sourcesContent":["import type Logger from 'bunyan';\nimport type { Logger as WpLogger } from '@webpieces/core-util';\nimport { RequestContext } from '@webpieces/core-context';\nimport { LoggedError } from './LoggedError';\n\n// A log line with no active RequestContext = a missing request-wrapping server\n// filter. We report that once (below), never on every line.\nconst MISSING_CONTEXT_MESSAGE =\n 'Log emitted OUTSIDE RequestContext.run(...) — every request must be wrapped in ' +\n 'RequestContext.run() by a server filter. That filter appears to be missing: correlation ' +\n 'fields (requestId, tenant, ...) will be absent from logs until it is added. Reported once.';\n\n/**\n * The error, whole. There is deliberately NO size guard here any more.\n *\n * This used to truncate an error over 100KB down to 5 stack frames and a 100-character message, to\n * keep one giant stack from blowing past Cloud Logging's per-entry limit. That traded the wrong\n * thing away: a stack trace big enough to trip the limit is precisely the one worth reading, and it\n * arrived pre-shredded. The GCP stream now SPLITS an oversized record across several complete\n * records instead (see ChunkingRawStream), so the whole stack survives and nothing is lost — which\n * makes truncating here strictly harmful.\n */\nfunction normalizeError(err: Error): LoggedError {\n return new LoggedError(err.name, err.message, err.stack);\n}\n\n/**\n * BunyanLogger - a webpieces {@link WpLogger} backed by a bunyan logger (one per\n * name, created as a bunyan child carrying `loggerName`). On every call it reads\n * the logged HeaderRegistry keys DIRECTLY from the active {@link RequestContext}\n * (secured values masked) into the bunyan fields object, and normalizes an optional\n * Error into `err: { name, message, stack }` — matching the tested trytami AppLogger.\n * The GCP stream then serializes those fields into the structured log payload.\n *\n * This context-reading is INLINED here (and duplicated in the winston logger) on\n * purpose: it must run ONLY when a bunyan/winston backend is installed, never for\n * the plain ConsoleLogger — so it does not belong on RequestContext.\n */\nexport class BunyanLogger implements WpLogger {\n // One-shot latch (see MISSING_CONTEXT_MESSAGE) — flips true after the first\n // out-of-context line so we report the missing filter exactly once, not per line.\n private reportedMissingContext = false;\n\n constructor(private readonly bunyan: Logger) {}\n\n trace(message: string, err?: Error): void {\n this.bunyan.trace(this.buildFields(err), message);\n }\n\n debug(message: string, err?: Error): void {\n this.bunyan.debug(this.buildFields(err), message);\n }\n\n info(message: string, err?: Error): void {\n this.bunyan.info(this.buildFields(err), message);\n }\n\n warn(message: string, err?: Error): void {\n this.bunyan.warn(this.buildFields(err), message);\n }\n\n error(message: string, err?: Error): void {\n this.bunyan.error(this.buildFields(err), message);\n }\n\n private buildFields(err?: Error): Record<string, string | object | LoggedError> {\n const fields: Record<string, string | object | LoggedError> = {};\n if (!RequestContext.isActive()) {\n this.reportMissingContextOnce();\n return fields;\n }\n\n // ONE loop, in HeaderRegistry.buildStructuredLogFields — the registry owns the keys and each\n // ContextKey masks its own value. Values may be OBJECTS (the `api` tag), so an object-valued key\n // nests into the structured payload (bunyan's GCP stream serializes fields) rather than being\n // dropped by the string-only buildLogFields.\n RequestContext.buildStructuredLogFields().forEach((value: string | object, name: string) => {\n fields[name] = value;\n });\n if (err) {\n fields['err'] = normalizeError(err);\n }\n return fields;\n }\n\n // Report the missing request-wrapping filter once. Uses the RAW bunyan logger\n // (not buildFields()), so there is no re-entrancy — just a single extra line.\n private reportMissingContextOnce(): void {\n if (this.reportedMissingContext) {\n return;\n }\n this.reportedMissingContext = true;\n this.bunyan.error(MISSING_CONTEXT_MESSAGE);\n }\n}\n"]}
1
+ {"version":3,"file":"BunyanLogger.js","sourceRoot":"","sources":["../../../../../packages/logging/bunyan/src/BunyanLogger.ts"],"names":[],"mappings":";;;AAEA,0DAAyD;AACzD,+CAA4C;AAE5C;;;;;;;;;GASG;AACH,SAAS,cAAc,CAAC,GAAU;IAC9B,OAAO,IAAI,yBAAW,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;AAC7D,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAa,YAAY;IACQ;IAA7B,YAA6B,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;IAAG,CAAC;IAE/C,KAAK,CAAC,OAAe,EAAE,GAAW;QAC9B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IACtD,CAAC;IAED,KAAK,CAAC,OAAe,EAAE,GAAW;QAC9B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IACtD,CAAC;IAED,IAAI,CAAC,OAAe,EAAE,GAAW;QAC7B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IACrD,CAAC;IAED,IAAI,CAAC,OAAe,EAAE,GAAW;QAC7B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IACrD,CAAC;IAED,KAAK,CAAC,OAAe,EAAE,GAAW;QAC9B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IACtD,CAAC;IAEO,WAAW,CAAC,GAAW;QAC3B,MAAM,MAAM,GAAkD,EAAE,CAAC;QACjE,4FAA4F;QAC5F,2FAA2F;QAC3F,0FAA0F;QAC1F,0CAA0C;QAC1C,IAAI,CAAC,6BAAc,CAAC,QAAQ,EAAE,EAAE,CAAC;YAC7B,OAAO,MAAM,CAAC;QAClB,CAAC;QAED,6FAA6F;QAC7F,iGAAiG;QACjG,8FAA8F;QAC9F,6CAA6C;QAC7C,6BAAc,CAAC,wBAAwB,EAAE,CAAC,OAAO,CAAC,CAAC,KAAsB,EAAE,IAAY,EAAE,EAAE;YACvF,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QACzB,CAAC,CAAC,CAAC;QACH,IAAI,GAAG,EAAE,CAAC;YACN,MAAM,CAAC,KAAK,CAAC,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,MAAM,CAAC;IAClB,CAAC;CACJ;AA7CD,oCA6CC","sourcesContent":["import type Logger from 'bunyan';\nimport type { Logger as WpLogger } from '@webpieces/core-util';\nimport { RequestContext } from '@webpieces/core-context';\nimport { LoggedError } from './LoggedError';\n\n/**\n * The error, whole. There is deliberately NO size guard here any more.\n *\n * This used to truncate an error over 100KB down to 5 stack frames and a 100-character message, to\n * keep one giant stack from blowing past Cloud Logging's per-entry limit. That traded the wrong\n * thing away: a stack trace big enough to trip the limit is precisely the one worth reading, and it\n * arrived pre-shredded. The GCP stream now SPLITS an oversized record across several complete\n * records instead (see ChunkingRawStream), so the whole stack survives and nothing is lost — which\n * makes truncating here strictly harmful.\n */\nfunction normalizeError(err: Error): LoggedError {\n return new LoggedError(err.name, err.message, err.stack);\n}\n\n/**\n * BunyanLogger - a webpieces {@link WpLogger} backed by a bunyan logger (one per\n * name, created as a bunyan child carrying `loggerName`). On every call it reads\n * the logged HeaderRegistry keys DIRECTLY from the active {@link RequestContext}\n * (secured values masked) into the bunyan fields object, and normalizes an optional\n * Error into `err: { name, message, stack }` — matching the tested trytami AppLogger.\n * The GCP stream then serializes those fields into the structured log payload.\n *\n * This context-reading is INLINED here (and duplicated in the winston logger) on\n * purpose: it must run ONLY when a bunyan/winston backend is installed, never for\n * the plain ConsoleLogger — so it does not belong on RequestContext.\n */\nexport class BunyanLogger implements WpLogger {\n constructor(private readonly bunyan: Logger) {}\n\n trace(message: string, err?: Error): void {\n this.bunyan.trace(this.buildFields(err), message);\n }\n\n debug(message: string, err?: Error): void {\n this.bunyan.debug(this.buildFields(err), message);\n }\n\n info(message: string, err?: Error): void {\n this.bunyan.info(this.buildFields(err), message);\n }\n\n warn(message: string, err?: Error): void {\n this.bunyan.warn(this.buildFields(err), message);\n }\n\n error(message: string, err?: Error): void {\n this.bunyan.error(this.buildFields(err), message);\n }\n\n private buildFields(err?: Error): Record<string, string | object | LoggedError> {\n const fields: Record<string, string | object | LoggedError> = {};\n // No active RequestContext (startup, a background job, or an in-process call the caller did\n // not wrap) simply logs with no context fields — an empty context is normal, not an error.\n // The in-process client's ApiClientFactory.requireActiveContext() throws precisely when a\n // request-path wrap is genuinely missing.\n if (!RequestContext.isActive()) {\n return fields;\n }\n\n // ONE loop, in HeaderRegistry.buildStructuredLogFields — the registry owns the keys and each\n // ContextKey masks its own value. Values may be OBJECTS (the `api` tag), so an object-valued key\n // nests into the structured payload (bunyan's GCP stream serializes fields) rather than being\n // dropped by the string-only buildLogFields.\n RequestContext.buildStructuredLogFields().forEach((value: string | object, name: string) => {\n fields[name] = value;\n });\n if (err) {\n fields['err'] = normalizeError(err);\n }\n return fields;\n }\n}\n"]}