@geekmidas/logger 10.0.0-alpha.69 → 10.0.0-alpha.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/otel.cjs +113 -0
- package/dist/otel.cjs.map +1 -0
- package/dist/otel.d.cts +31 -0
- package/dist/otel.d.cts.map +1 -0
- package/dist/otel.d.mts +31 -0
- package/dist/otel.d.mts.map +1 -0
- package/dist/otel.mjs +110 -0
- package/dist/otel.mjs.map +1 -0
- package/dist/pino.cjs +17 -4
- package/dist/pino.cjs.map +1 -1
- package/dist/pino.d.cts.map +1 -1
- package/dist/pino.d.mts.map +1 -1
- package/dist/pino.mjs +18 -5
- package/dist/pino.mjs.map +1 -1
- package/package.json +19 -3
package/dist/otel.cjs
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _opentelemetry_api_logs = require("@opentelemetry/api-logs");
|
|
3
|
+
//#region src/otel.ts
|
|
4
|
+
/**
|
|
5
|
+
* The bridge from pino to OpenTelemetry logs.
|
|
6
|
+
*
|
|
7
|
+
* A pino record is also emitted through `@opentelemetry/api-logs` when a
|
|
8
|
+
* `LoggerProvider` is registered globally — as the generated telemetry setup
|
|
9
|
+
* of a production server does. Nothing hooks module loading, so it works the
|
|
10
|
+
* same in a single bundled file as from `node_modules`.
|
|
11
|
+
*
|
|
12
|
+
* `@opentelemetry/api-logs` keeps its provider on `globalThis` under a
|
|
13
|
+
* `Symbol.for` key, so the copy imported here and the copy the SDK registered
|
|
14
|
+
* through share one provider even when a bundle holds both. That is why the
|
|
15
|
+
* provider is looked up per record rather than once: a logger obtained before
|
|
16
|
+
* the SDK registered — or through another copy's proxy — would stay a no-op.
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
/** The instrumentation scope every bridged record carries. */
|
|
21
|
+
const OTEL_SCOPE = "@geekmidas/logger";
|
|
22
|
+
/** Pino's level labels, upper-cased as the logger prints them. */
|
|
23
|
+
const SEVERITY = {
|
|
24
|
+
TRACE: _opentelemetry_api_logs.SeverityNumber.TRACE,
|
|
25
|
+
DEBUG: _opentelemetry_api_logs.SeverityNumber.DEBUG,
|
|
26
|
+
INFO: _opentelemetry_api_logs.SeverityNumber.INFO,
|
|
27
|
+
WARN: _opentelemetry_api_logs.SeverityNumber.WARN,
|
|
28
|
+
ERROR: _opentelemetry_api_logs.SeverityNumber.ERROR,
|
|
29
|
+
FATAL: _opentelemetry_api_logs.SeverityNumber.FATAL
|
|
30
|
+
};
|
|
31
|
+
/** Pino's numeric levels, for a record whose level was not formatted. */
|
|
32
|
+
function severityFromNumber(level) {
|
|
33
|
+
if (level >= 60) return _opentelemetry_api_logs.SeverityNumber.FATAL;
|
|
34
|
+
if (level >= 50) return _opentelemetry_api_logs.SeverityNumber.ERROR;
|
|
35
|
+
if (level >= 40) return _opentelemetry_api_logs.SeverityNumber.WARN;
|
|
36
|
+
if (level >= 30) return _opentelemetry_api_logs.SeverityNumber.INFO;
|
|
37
|
+
if (level >= 20) return _opentelemetry_api_logs.SeverityNumber.DEBUG;
|
|
38
|
+
return _opentelemetry_api_logs.SeverityNumber.TRACE;
|
|
39
|
+
}
|
|
40
|
+
const SEVERITY_TEXT = {
|
|
41
|
+
[_opentelemetry_api_logs.SeverityNumber.TRACE]: "TRACE",
|
|
42
|
+
[_opentelemetry_api_logs.SeverityNumber.DEBUG]: "DEBUG",
|
|
43
|
+
[_opentelemetry_api_logs.SeverityNumber.INFO]: "INFO",
|
|
44
|
+
[_opentelemetry_api_logs.SeverityNumber.WARN]: "WARN",
|
|
45
|
+
[_opentelemetry_api_logs.SeverityNumber.ERROR]: "ERROR",
|
|
46
|
+
[_opentelemetry_api_logs.SeverityNumber.FATAL]: "FATAL"
|
|
47
|
+
};
|
|
48
|
+
let cached;
|
|
49
|
+
/** The bridge's logger from whichever provider is registered now. */
|
|
50
|
+
function otelLogger() {
|
|
51
|
+
const provider = _opentelemetry_api_logs.logs.getLoggerProvider();
|
|
52
|
+
if (cached?.provider !== provider) cached = {
|
|
53
|
+
provider,
|
|
54
|
+
logger: provider.getLogger(OTEL_SCOPE)
|
|
55
|
+
};
|
|
56
|
+
return cached.logger;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Emit one serialized pino line through OpenTelemetry, when a provider is
|
|
60
|
+
* registered that wants it.
|
|
61
|
+
*
|
|
62
|
+
* The line is what pino is about to write — after path redaction and the
|
|
63
|
+
* URL-credential redaction — so the exported copy is exactly as redacted as
|
|
64
|
+
* stdout. It runs synchronously inside the log call, so the active context —
|
|
65
|
+
* and with it the trace and span ids — is the caller's.
|
|
66
|
+
*
|
|
67
|
+
* Without a registered provider the logger is a no-op whose `enabled()` is
|
|
68
|
+
* false, and the line is not parsed.
|
|
69
|
+
*/
|
|
70
|
+
function emitToOtel(line) {
|
|
71
|
+
const logger = otelLogger();
|
|
72
|
+
if (!logger.enabled()) return;
|
|
73
|
+
let record;
|
|
74
|
+
try {
|
|
75
|
+
record = JSON.parse(line);
|
|
76
|
+
} catch {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const { level, time, msg, ...attributes } = record;
|
|
80
|
+
const severityNumber = typeof level === "number" ? severityFromNumber(level) : SEVERITY[String(level).toUpperCase()] ?? _opentelemetry_api_logs.SeverityNumber.UNSPECIFIED;
|
|
81
|
+
if (!logger.enabled({ severityNumber })) return;
|
|
82
|
+
logger.emit({
|
|
83
|
+
severityNumber,
|
|
84
|
+
severityText: SEVERITY_TEXT[severityNumber] ?? (level === void 0 ? void 0 : String(level)),
|
|
85
|
+
...msg !== void 0 && { body: String(msg) },
|
|
86
|
+
...typeof time === "number" && { timestamp: time },
|
|
87
|
+
attributes
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The bridge as a pino `streamWrite` hook, for a logger made with `pino()`
|
|
92
|
+
* directly rather than `createLogger` — which installs it already.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* import pino from 'pino';
|
|
97
|
+
* import { otelStreamWrite } from '@geekmidas/logger/otel';
|
|
98
|
+
*
|
|
99
|
+
* const logger = pino({ hooks: { streamWrite: otelStreamWrite } });
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
function otelStreamWrite(line) {
|
|
103
|
+
try {
|
|
104
|
+
emitToOtel(line);
|
|
105
|
+
} catch {}
|
|
106
|
+
return line;
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
exports.OTEL_SCOPE = OTEL_SCOPE;
|
|
110
|
+
exports.emitToOtel = emitToOtel;
|
|
111
|
+
exports.otelStreamWrite = otelStreamWrite;
|
|
112
|
+
|
|
113
|
+
//# sourceMappingURL=otel.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otel.cjs","names":["SeverityNumber","logs"],"sources":["../src/otel.ts"],"sourcesContent":["/**\n * The bridge from pino to OpenTelemetry logs.\n *\n * A pino record is also emitted through `@opentelemetry/api-logs` when a\n * `LoggerProvider` is registered globally — as the generated telemetry setup\n * of a production server does. Nothing hooks module loading, so it works the\n * same in a single bundled file as from `node_modules`.\n *\n * `@opentelemetry/api-logs` keeps its provider on `globalThis` under a\n * `Symbol.for` key, so the copy imported here and the copy the SDK registered\n * through share one provider even when a bundle holds both. That is why the\n * provider is looked up per record rather than once: a logger obtained before\n * the SDK registered — or through another copy's proxy — would stay a no-op.\n *\n * @module\n */\nimport {\n\ttype AnyValueMap,\n\ttype Logger,\n\ttype LoggerProvider,\n\tlogs,\n\tSeverityNumber,\n} from '@opentelemetry/api-logs';\n\n/** The instrumentation scope every bridged record carries. */\nexport const OTEL_SCOPE = '@geekmidas/logger';\n\n/** Pino's level labels, upper-cased as the logger prints them. */\nconst SEVERITY: Record<string, SeverityNumber> = {\n\tTRACE: SeverityNumber.TRACE,\n\tDEBUG: SeverityNumber.DEBUG,\n\tINFO: SeverityNumber.INFO,\n\tWARN: SeverityNumber.WARN,\n\tERROR: SeverityNumber.ERROR,\n\tFATAL: SeverityNumber.FATAL,\n};\n\n/** Pino's numeric levels, for a record whose level was not formatted. */\nfunction severityFromNumber(level: number): SeverityNumber {\n\tif (level >= 60) return SeverityNumber.FATAL;\n\tif (level >= 50) return SeverityNumber.ERROR;\n\tif (level >= 40) return SeverityNumber.WARN;\n\tif (level >= 30) return SeverityNumber.INFO;\n\tif (level >= 20) return SeverityNumber.DEBUG;\n\treturn SeverityNumber.TRACE;\n}\n\nconst SEVERITY_TEXT: Partial<Record<SeverityNumber, string>> = {\n\t[SeverityNumber.TRACE]: 'TRACE',\n\t[SeverityNumber.DEBUG]: 'DEBUG',\n\t[SeverityNumber.INFO]: 'INFO',\n\t[SeverityNumber.WARN]: 'WARN',\n\t[SeverityNumber.ERROR]: 'ERROR',\n\t[SeverityNumber.FATAL]: 'FATAL',\n};\n\nlet cached: { provider: LoggerProvider; logger: Logger } | undefined;\n\n/** The bridge's logger from whichever provider is registered now. */\nfunction otelLogger(): Logger {\n\tconst provider = logs.getLoggerProvider();\n\tif (cached?.provider !== provider) {\n\t\tcached = { provider, logger: provider.getLogger(OTEL_SCOPE) };\n\t}\n\treturn cached.logger;\n}\n\n/**\n * Emit one serialized pino line through OpenTelemetry, when a provider is\n * registered that wants it.\n *\n * The line is what pino is about to write — after path redaction and the\n * URL-credential redaction — so the exported copy is exactly as redacted as\n * stdout. It runs synchronously inside the log call, so the active context —\n * and with it the trace and span ids — is the caller's.\n *\n * Without a registered provider the logger is a no-op whose `enabled()` is\n * false, and the line is not parsed.\n */\nexport function emitToOtel(line: string): void {\n\tconst logger = otelLogger();\n\tif (!logger.enabled()) return;\n\n\tlet record: Record<string, unknown>;\n\ttry {\n\t\trecord = JSON.parse(line);\n\t} catch {\n\t\treturn;\n\t}\n\n\tconst { level, time, msg, ...attributes } = record;\n\tconst severityNumber =\n\t\ttypeof level === 'number'\n\t\t\t? severityFromNumber(level)\n\t\t\t: (SEVERITY[String(level).toUpperCase()] ?? SeverityNumber.UNSPECIFIED);\n\n\tif (!logger.enabled({ severityNumber })) return;\n\n\tlogger.emit({\n\t\tseverityNumber,\n\t\tseverityText:\n\t\t\tSEVERITY_TEXT[severityNumber] ??\n\t\t\t(level === undefined ? undefined : String(level)),\n\t\t...(msg !== undefined && { body: String(msg) }),\n\t\t...(typeof time === 'number' && { timestamp: time }),\n\t\tattributes: attributes as AnyValueMap,\n\t});\n}\n\n/**\n * The bridge as a pino `streamWrite` hook, for a logger made with `pino()`\n * directly rather than `createLogger` — which installs it already.\n *\n * @example\n * ```typescript\n * import pino from 'pino';\n * import { otelStreamWrite } from '@geekmidas/logger/otel';\n *\n * const logger = pino({ hooks: { streamWrite: otelStreamWrite } });\n * ```\n */\nexport function otelStreamWrite(line: string): string {\n\ttry {\n\t\temitToOtel(line);\n\t} catch {\n\t\t// Exporting a log is never a reason to lose it from stdout.\n\t}\n\treturn line;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAyBA,MAAa,aAAa;;AAG1B,MAAM,WAA2C;CAChD,OAAOA,wBAAAA,eAAe;CACtB,OAAOA,wBAAAA,eAAe;CACtB,MAAMA,wBAAAA,eAAe;CACrB,MAAMA,wBAAAA,eAAe;CACrB,OAAOA,wBAAAA,eAAe;CACtB,OAAOA,wBAAAA,eAAe;AACvB;;AAGA,SAAS,mBAAmB,OAA+B;CAC1D,IAAI,SAAS,IAAI,OAAOA,wBAAAA,eAAe;CACvC,IAAI,SAAS,IAAI,OAAOA,wBAAAA,eAAe;CACvC,IAAI,SAAS,IAAI,OAAOA,wBAAAA,eAAe;CACvC,IAAI,SAAS,IAAI,OAAOA,wBAAAA,eAAe;CACvC,IAAI,SAAS,IAAI,OAAOA,wBAAAA,eAAe;CACvC,OAAOA,wBAAAA,eAAe;AACvB;AAEA,MAAM,gBAAyD;EAC7DA,wBAAAA,eAAe,QAAQ;EACvBA,wBAAAA,eAAe,QAAQ;EACvBA,wBAAAA,eAAe,OAAO;EACtBA,wBAAAA,eAAe,OAAO;EACtBA,wBAAAA,eAAe,QAAQ;EACvBA,wBAAAA,eAAe,QAAQ;AACzB;AAEA,IAAI;;AAGJ,SAAS,aAAqB;CAC7B,MAAM,WAAWC,wBAAAA,KAAK,kBAAkB;CACxC,IAAI,QAAQ,aAAa,UACxB,SAAS;EAAE;EAAU,QAAQ,SAAS,UAAU,UAAU;CAAE;CAE7D,OAAO,OAAO;AACf;;;;;;;;;;;;;AAcA,SAAgB,WAAW,MAAoB;CAC9C,MAAM,SAAS,WAAW;CAC1B,IAAI,CAAC,OAAO,QAAQ,GAAG;CAEvB,IAAI;CACJ,IAAI;EACH,SAAS,KAAK,MAAM,IAAI;CACzB,QAAQ;EACP;CACD;CAEA,MAAM,EAAE,OAAO,MAAM,KAAK,GAAG,eAAe;CAC5C,MAAM,iBACL,OAAO,UAAU,WACd,mBAAmB,KAAK,IACvB,SAAS,OAAO,KAAK,CAAC,CAAC,YAAY,MAAMD,wBAAAA,eAAe;CAE7D,IAAI,CAAC,OAAO,QAAQ,EAAE,eAAe,CAAC,GAAG;CAEzC,OAAO,KAAK;EACX;EACA,cACC,cAAc,oBACb,UAAU,KAAA,IAAY,KAAA,IAAY,OAAO,KAAK;EAChD,GAAI,QAAQ,KAAA,KAAa,EAAE,MAAM,OAAO,GAAG,EAAE;EAC7C,GAAI,OAAO,SAAS,YAAY,EAAE,WAAW,KAAK;EACtC;CACb,CAAC;AACF;;;;;;;;;;;;;AAcA,SAAgB,gBAAgB,MAAsB;CACrD,IAAI;EACH,WAAW,IAAI;CAChB,QAAQ,CAER;CACA,OAAO;AACR"}
|
package/dist/otel.d.cts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/otel.d.ts
|
|
2
|
+
/** The instrumentation scope every bridged record carries. */
|
|
3
|
+
export declare const OTEL_SCOPE = "@geekmidas/logger";
|
|
4
|
+
/**
|
|
5
|
+
* Emit one serialized pino line through OpenTelemetry, when a provider is
|
|
6
|
+
* registered that wants it.
|
|
7
|
+
*
|
|
8
|
+
* The line is what pino is about to write — after path redaction and the
|
|
9
|
+
* URL-credential redaction — so the exported copy is exactly as redacted as
|
|
10
|
+
* stdout. It runs synchronously inside the log call, so the active context —
|
|
11
|
+
* and with it the trace and span ids — is the caller's.
|
|
12
|
+
*
|
|
13
|
+
* Without a registered provider the logger is a no-op whose `enabled()` is
|
|
14
|
+
* false, and the line is not parsed.
|
|
15
|
+
*/
|
|
16
|
+
export declare function emitToOtel(line: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* The bridge as a pino `streamWrite` hook, for a logger made with `pino()`
|
|
19
|
+
* directly rather than `createLogger` — which installs it already.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import pino from 'pino';
|
|
24
|
+
* import { otelStreamWrite } from '@geekmidas/logger/otel';
|
|
25
|
+
*
|
|
26
|
+
* const logger = pino({ hooks: { streamWrite: otelStreamWrite } });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function otelStreamWrite(line: string): string;
|
|
30
|
+
//#endregion
|
|
31
|
+
//# sourceMappingURL=otel.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otel.d.cts","names":[],"sources":["../src/otel.ts"],"mappings":";;qBAyBa;;;;;;;;;;;;;wBAsDG,WAAW;;;;;;;;;;;;;wBA0CX,gBAAgB"}
|
package/dist/otel.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/otel.d.ts
|
|
2
|
+
/** The instrumentation scope every bridged record carries. */
|
|
3
|
+
export declare const OTEL_SCOPE = "@geekmidas/logger";
|
|
4
|
+
/**
|
|
5
|
+
* Emit one serialized pino line through OpenTelemetry, when a provider is
|
|
6
|
+
* registered that wants it.
|
|
7
|
+
*
|
|
8
|
+
* The line is what pino is about to write — after path redaction and the
|
|
9
|
+
* URL-credential redaction — so the exported copy is exactly as redacted as
|
|
10
|
+
* stdout. It runs synchronously inside the log call, so the active context —
|
|
11
|
+
* and with it the trace and span ids — is the caller's.
|
|
12
|
+
*
|
|
13
|
+
* Without a registered provider the logger is a no-op whose `enabled()` is
|
|
14
|
+
* false, and the line is not parsed.
|
|
15
|
+
*/
|
|
16
|
+
export declare function emitToOtel(line: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* The bridge as a pino `streamWrite` hook, for a logger made with `pino()`
|
|
19
|
+
* directly rather than `createLogger` — which installs it already.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import pino from 'pino';
|
|
24
|
+
* import { otelStreamWrite } from '@geekmidas/logger/otel';
|
|
25
|
+
*
|
|
26
|
+
* const logger = pino({ hooks: { streamWrite: otelStreamWrite } });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function otelStreamWrite(line: string): string;
|
|
30
|
+
//#endregion
|
|
31
|
+
//# sourceMappingURL=otel.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otel.d.mts","names":[],"sources":["../src/otel.ts"],"mappings":";;qBAyBa;;;;;;;;;;;;;wBAsDG,WAAW;;;;;;;;;;;;;wBA0CX,gBAAgB"}
|
package/dist/otel.mjs
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { SeverityNumber, logs } from "@opentelemetry/api-logs";
|
|
2
|
+
//#region src/otel.ts
|
|
3
|
+
/**
|
|
4
|
+
* The bridge from pino to OpenTelemetry logs.
|
|
5
|
+
*
|
|
6
|
+
* A pino record is also emitted through `@opentelemetry/api-logs` when a
|
|
7
|
+
* `LoggerProvider` is registered globally — as the generated telemetry setup
|
|
8
|
+
* of a production server does. Nothing hooks module loading, so it works the
|
|
9
|
+
* same in a single bundled file as from `node_modules`.
|
|
10
|
+
*
|
|
11
|
+
* `@opentelemetry/api-logs` keeps its provider on `globalThis` under a
|
|
12
|
+
* `Symbol.for` key, so the copy imported here and the copy the SDK registered
|
|
13
|
+
* through share one provider even when a bundle holds both. That is why the
|
|
14
|
+
* provider is looked up per record rather than once: a logger obtained before
|
|
15
|
+
* the SDK registered — or through another copy's proxy — would stay a no-op.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
/** The instrumentation scope every bridged record carries. */
|
|
20
|
+
const OTEL_SCOPE = "@geekmidas/logger";
|
|
21
|
+
/** Pino's level labels, upper-cased as the logger prints them. */
|
|
22
|
+
const SEVERITY = {
|
|
23
|
+
TRACE: SeverityNumber.TRACE,
|
|
24
|
+
DEBUG: SeverityNumber.DEBUG,
|
|
25
|
+
INFO: SeverityNumber.INFO,
|
|
26
|
+
WARN: SeverityNumber.WARN,
|
|
27
|
+
ERROR: SeverityNumber.ERROR,
|
|
28
|
+
FATAL: SeverityNumber.FATAL
|
|
29
|
+
};
|
|
30
|
+
/** Pino's numeric levels, for a record whose level was not formatted. */
|
|
31
|
+
function severityFromNumber(level) {
|
|
32
|
+
if (level >= 60) return SeverityNumber.FATAL;
|
|
33
|
+
if (level >= 50) return SeverityNumber.ERROR;
|
|
34
|
+
if (level >= 40) return SeverityNumber.WARN;
|
|
35
|
+
if (level >= 30) return SeverityNumber.INFO;
|
|
36
|
+
if (level >= 20) return SeverityNumber.DEBUG;
|
|
37
|
+
return SeverityNumber.TRACE;
|
|
38
|
+
}
|
|
39
|
+
const SEVERITY_TEXT = {
|
|
40
|
+
[SeverityNumber.TRACE]: "TRACE",
|
|
41
|
+
[SeverityNumber.DEBUG]: "DEBUG",
|
|
42
|
+
[SeverityNumber.INFO]: "INFO",
|
|
43
|
+
[SeverityNumber.WARN]: "WARN",
|
|
44
|
+
[SeverityNumber.ERROR]: "ERROR",
|
|
45
|
+
[SeverityNumber.FATAL]: "FATAL"
|
|
46
|
+
};
|
|
47
|
+
let cached;
|
|
48
|
+
/** The bridge's logger from whichever provider is registered now. */
|
|
49
|
+
function otelLogger() {
|
|
50
|
+
const provider = logs.getLoggerProvider();
|
|
51
|
+
if (cached?.provider !== provider) cached = {
|
|
52
|
+
provider,
|
|
53
|
+
logger: provider.getLogger(OTEL_SCOPE)
|
|
54
|
+
};
|
|
55
|
+
return cached.logger;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Emit one serialized pino line through OpenTelemetry, when a provider is
|
|
59
|
+
* registered that wants it.
|
|
60
|
+
*
|
|
61
|
+
* The line is what pino is about to write — after path redaction and the
|
|
62
|
+
* URL-credential redaction — so the exported copy is exactly as redacted as
|
|
63
|
+
* stdout. It runs synchronously inside the log call, so the active context —
|
|
64
|
+
* and with it the trace and span ids — is the caller's.
|
|
65
|
+
*
|
|
66
|
+
* Without a registered provider the logger is a no-op whose `enabled()` is
|
|
67
|
+
* false, and the line is not parsed.
|
|
68
|
+
*/
|
|
69
|
+
function emitToOtel(line) {
|
|
70
|
+
const logger = otelLogger();
|
|
71
|
+
if (!logger.enabled()) return;
|
|
72
|
+
let record;
|
|
73
|
+
try {
|
|
74
|
+
record = JSON.parse(line);
|
|
75
|
+
} catch {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const { level, time, msg, ...attributes } = record;
|
|
79
|
+
const severityNumber = typeof level === "number" ? severityFromNumber(level) : SEVERITY[String(level).toUpperCase()] ?? SeverityNumber.UNSPECIFIED;
|
|
80
|
+
if (!logger.enabled({ severityNumber })) return;
|
|
81
|
+
logger.emit({
|
|
82
|
+
severityNumber,
|
|
83
|
+
severityText: SEVERITY_TEXT[severityNumber] ?? (level === void 0 ? void 0 : String(level)),
|
|
84
|
+
...msg !== void 0 && { body: String(msg) },
|
|
85
|
+
...typeof time === "number" && { timestamp: time },
|
|
86
|
+
attributes
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The bridge as a pino `streamWrite` hook, for a logger made with `pino()`
|
|
91
|
+
* directly rather than `createLogger` — which installs it already.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* import pino from 'pino';
|
|
96
|
+
* import { otelStreamWrite } from '@geekmidas/logger/otel';
|
|
97
|
+
*
|
|
98
|
+
* const logger = pino({ hooks: { streamWrite: otelStreamWrite } });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function otelStreamWrite(line) {
|
|
102
|
+
try {
|
|
103
|
+
emitToOtel(line);
|
|
104
|
+
} catch {}
|
|
105
|
+
return line;
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
export { OTEL_SCOPE, emitToOtel, otelStreamWrite };
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=otel.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otel.mjs","names":[],"sources":["../src/otel.ts"],"sourcesContent":["/**\n * The bridge from pino to OpenTelemetry logs.\n *\n * A pino record is also emitted through `@opentelemetry/api-logs` when a\n * `LoggerProvider` is registered globally — as the generated telemetry setup\n * of a production server does. Nothing hooks module loading, so it works the\n * same in a single bundled file as from `node_modules`.\n *\n * `@opentelemetry/api-logs` keeps its provider on `globalThis` under a\n * `Symbol.for` key, so the copy imported here and the copy the SDK registered\n * through share one provider even when a bundle holds both. That is why the\n * provider is looked up per record rather than once: a logger obtained before\n * the SDK registered — or through another copy's proxy — would stay a no-op.\n *\n * @module\n */\nimport {\n\ttype AnyValueMap,\n\ttype Logger,\n\ttype LoggerProvider,\n\tlogs,\n\tSeverityNumber,\n} from '@opentelemetry/api-logs';\n\n/** The instrumentation scope every bridged record carries. */\nexport const OTEL_SCOPE = '@geekmidas/logger';\n\n/** Pino's level labels, upper-cased as the logger prints them. */\nconst SEVERITY: Record<string, SeverityNumber> = {\n\tTRACE: SeverityNumber.TRACE,\n\tDEBUG: SeverityNumber.DEBUG,\n\tINFO: SeverityNumber.INFO,\n\tWARN: SeverityNumber.WARN,\n\tERROR: SeverityNumber.ERROR,\n\tFATAL: SeverityNumber.FATAL,\n};\n\n/** Pino's numeric levels, for a record whose level was not formatted. */\nfunction severityFromNumber(level: number): SeverityNumber {\n\tif (level >= 60) return SeverityNumber.FATAL;\n\tif (level >= 50) return SeverityNumber.ERROR;\n\tif (level >= 40) return SeverityNumber.WARN;\n\tif (level >= 30) return SeverityNumber.INFO;\n\tif (level >= 20) return SeverityNumber.DEBUG;\n\treturn SeverityNumber.TRACE;\n}\n\nconst SEVERITY_TEXT: Partial<Record<SeverityNumber, string>> = {\n\t[SeverityNumber.TRACE]: 'TRACE',\n\t[SeverityNumber.DEBUG]: 'DEBUG',\n\t[SeverityNumber.INFO]: 'INFO',\n\t[SeverityNumber.WARN]: 'WARN',\n\t[SeverityNumber.ERROR]: 'ERROR',\n\t[SeverityNumber.FATAL]: 'FATAL',\n};\n\nlet cached: { provider: LoggerProvider; logger: Logger } | undefined;\n\n/** The bridge's logger from whichever provider is registered now. */\nfunction otelLogger(): Logger {\n\tconst provider = logs.getLoggerProvider();\n\tif (cached?.provider !== provider) {\n\t\tcached = { provider, logger: provider.getLogger(OTEL_SCOPE) };\n\t}\n\treturn cached.logger;\n}\n\n/**\n * Emit one serialized pino line through OpenTelemetry, when a provider is\n * registered that wants it.\n *\n * The line is what pino is about to write — after path redaction and the\n * URL-credential redaction — so the exported copy is exactly as redacted as\n * stdout. It runs synchronously inside the log call, so the active context —\n * and with it the trace and span ids — is the caller's.\n *\n * Without a registered provider the logger is a no-op whose `enabled()` is\n * false, and the line is not parsed.\n */\nexport function emitToOtel(line: string): void {\n\tconst logger = otelLogger();\n\tif (!logger.enabled()) return;\n\n\tlet record: Record<string, unknown>;\n\ttry {\n\t\trecord = JSON.parse(line);\n\t} catch {\n\t\treturn;\n\t}\n\n\tconst { level, time, msg, ...attributes } = record;\n\tconst severityNumber =\n\t\ttypeof level === 'number'\n\t\t\t? severityFromNumber(level)\n\t\t\t: (SEVERITY[String(level).toUpperCase()] ?? SeverityNumber.UNSPECIFIED);\n\n\tif (!logger.enabled({ severityNumber })) return;\n\n\tlogger.emit({\n\t\tseverityNumber,\n\t\tseverityText:\n\t\t\tSEVERITY_TEXT[severityNumber] ??\n\t\t\t(level === undefined ? undefined : String(level)),\n\t\t...(msg !== undefined && { body: String(msg) }),\n\t\t...(typeof time === 'number' && { timestamp: time }),\n\t\tattributes: attributes as AnyValueMap,\n\t});\n}\n\n/**\n * The bridge as a pino `streamWrite` hook, for a logger made with `pino()`\n * directly rather than `createLogger` — which installs it already.\n *\n * @example\n * ```typescript\n * import pino from 'pino';\n * import { otelStreamWrite } from '@geekmidas/logger/otel';\n *\n * const logger = pino({ hooks: { streamWrite: otelStreamWrite } });\n * ```\n */\nexport function otelStreamWrite(line: string): string {\n\ttry {\n\t\temitToOtel(line);\n\t} catch {\n\t\t// Exporting a log is never a reason to lose it from stdout.\n\t}\n\treturn line;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAyBA,MAAa,aAAa;;AAG1B,MAAM,WAA2C;CAChD,OAAO,eAAe;CACtB,OAAO,eAAe;CACtB,MAAM,eAAe;CACrB,MAAM,eAAe;CACrB,OAAO,eAAe;CACtB,OAAO,eAAe;AACvB;;AAGA,SAAS,mBAAmB,OAA+B;CAC1D,IAAI,SAAS,IAAI,OAAO,eAAe;CACvC,IAAI,SAAS,IAAI,OAAO,eAAe;CACvC,IAAI,SAAS,IAAI,OAAO,eAAe;CACvC,IAAI,SAAS,IAAI,OAAO,eAAe;CACvC,IAAI,SAAS,IAAI,OAAO,eAAe;CACvC,OAAO,eAAe;AACvB;AAEA,MAAM,gBAAyD;EAC7D,eAAe,QAAQ;EACvB,eAAe,QAAQ;EACvB,eAAe,OAAO;EACtB,eAAe,OAAO;EACtB,eAAe,QAAQ;EACvB,eAAe,QAAQ;AACzB;AAEA,IAAI;;AAGJ,SAAS,aAAqB;CAC7B,MAAM,WAAW,KAAK,kBAAkB;CACxC,IAAI,QAAQ,aAAa,UACxB,SAAS;EAAE;EAAU,QAAQ,SAAS,UAAU,UAAU;CAAE;CAE7D,OAAO,OAAO;AACf;;;;;;;;;;;;;AAcA,SAAgB,WAAW,MAAoB;CAC9C,MAAM,SAAS,WAAW;CAC1B,IAAI,CAAC,OAAO,QAAQ,GAAG;CAEvB,IAAI;CACJ,IAAI;EACH,SAAS,KAAK,MAAM,IAAI;CACzB,QAAQ;EACP;CACD;CAEA,MAAM,EAAE,OAAO,MAAM,KAAK,GAAG,eAAe;CAC5C,MAAM,iBACL,OAAO,UAAU,WACd,mBAAmB,KAAK,IACvB,SAAS,OAAO,KAAK,CAAC,CAAC,YAAY,MAAM,eAAe;CAE7D,IAAI,CAAC,OAAO,QAAQ,EAAE,eAAe,CAAC,GAAG;CAEzC,OAAO,KAAK;EACX;EACA,cACC,cAAc,oBACb,UAAU,KAAA,IAAY,KAAA,IAAY,OAAO,KAAK;EAChD,GAAI,QAAQ,KAAA,KAAa,EAAE,MAAM,OAAO,GAAG,EAAE;EAC7C,GAAI,OAAO,SAAS,YAAY,EAAE,WAAW,KAAK;EACtC;CACb,CAAC;AACF;;;;;;;;;;;;;AAcA,SAAgB,gBAAgB,MAAsB;CACrD,IAAI;EACH,WAAW,IAAI;CAChB,QAAQ,CAER;CACA,OAAO;AACR"}
|
package/dist/pino.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_otel = require("./otel.cjs");
|
|
2
3
|
const require_redact_paths = require("./redact-paths.cjs");
|
|
3
4
|
let pino = require("pino");
|
|
4
5
|
//#region src/pino.ts
|
|
@@ -77,14 +78,26 @@ function createLogger(options = {}) {
|
|
|
77
78
|
options: { colorize: true }
|
|
78
79
|
} } : {};
|
|
79
80
|
const redact = resolveRedactConfig(options.redact);
|
|
81
|
+
const serializeError = (value) => {
|
|
82
|
+
const serialized = pino.stdSerializers.err(value);
|
|
83
|
+
if (!redact || serialized === value) return serialized;
|
|
84
|
+
return require_redact_paths.redactUrlCredentialsIn({ ...serialized });
|
|
85
|
+
};
|
|
80
86
|
const pinoOptions = {
|
|
81
87
|
...baseOptions,
|
|
82
88
|
...options.level && { level: options.level },
|
|
83
89
|
...redact && { redact },
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
90
|
+
serializers: {
|
|
91
|
+
err: serializeError,
|
|
92
|
+
error: serializeError
|
|
93
|
+
},
|
|
94
|
+
hooks: {
|
|
95
|
+
...redact && { logMethod(args, method) {
|
|
96
|
+
const redacted = args.map((arg) => typeof arg === "string" ? require_redact_paths.redactUrlCredentials(arg) : arg);
|
|
97
|
+
return method.apply(this, redacted);
|
|
98
|
+
} },
|
|
99
|
+
streamWrite: require_otel.otelStreamWrite
|
|
100
|
+
},
|
|
88
101
|
formatters: {
|
|
89
102
|
...redact && { log: (object) => require_redact_paths.redactUrlCredentialsIn(object) },
|
|
90
103
|
bindings() {
|
package/dist/pino.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pino.cjs","names":["DEFAULT_REDACT_PATHS","
|
|
1
|
+
{"version":3,"file":"pino.cjs","names":["DEFAULT_REDACT_PATHS","stdSerializers","redactUrlCredentialsIn","redactUrlCredentials","otelStreamWrite"],"sources":["../src/pino.ts"],"sourcesContent":["/**\n * Pino logger with built-in redaction support for sensitive data.\n *\n * Redaction is on by default: a logger that was never told about passwords\n * and tokens still masks them. `redact: false` is the explicit opt-out.\n *\n * @example\n * ```typescript\n * import { createLogger, DEFAULT_REDACT_PATHS } from '@geekmidas/logger/pino';\n *\n * // Redaction with sensible defaults — the same as `redact: true`\n * const logger = createLogger();\n *\n * // Sensitive data is automatically masked\n * logger.info({ password: 'secret123', user: 'john' }, 'Login');\n * // Output: { password: '[Redacted]', user: 'john' } Login\n *\n * // Add custom paths (merged with defaults)\n * const logger2 = createLogger({ redact: ['user.ssn'] });\n *\n * // Override defaults for full control\n * const logger3 = createLogger({\n * redact: {\n * paths: ['onlyThis'],\n * resolution: 'override',\n * }\n * });\n * ```\n *\n * @module\n */\nimport { type LogFn, type Logger, pino, stdSerializers } from 'pino';\nimport { otelStreamWrite } from './otel';\nimport {\n\tDEFAULT_REDACT_PATHS,\n\tredactUrlCredentials,\n\tredactUrlCredentialsIn,\n} from './redact-paths';\nimport type { CreateLoggerOptions, RedactOptions } from './types';\n\n// Re-export for backwards compatibility\nexport { DEFAULT_REDACT_PATHS } from './redact-paths';\n\n/**\n * Type for the resolved pino redact config (without our custom resolution field).\n */\ntype PinoRedactConfig =\n\t| string[]\n\t| {\n\t\t\tpaths: string[];\n\t\t\tcensor?: string | ((value: unknown, path: string[]) => unknown);\n\t\t\tremove?: boolean;\n\t };\n\n/**\n * Resolves redaction configuration from options.\n * Returns undefined only when redaction is turned off with `false`; leaving it\n * unset is the same as `true`, so sensitive paths are masked unless someone\n * decides they should not be.\n *\n * By default (resolution: 'merge'), custom paths are merged with DEFAULT_REDACT_PATHS.\n * With resolution: 'override', only the custom paths are used.\n */\nfunction resolveRedactConfig(\n\tredact: boolean | RedactOptions | undefined,\n): PinoRedactConfig | undefined {\n\tif (redact === false) {\n\t\treturn undefined;\n\t}\n\n\tif (redact === undefined || redact === true) {\n\t\treturn DEFAULT_REDACT_PATHS;\n\t}\n\n\t// Array syntax - merge with defaults\n\tif (Array.isArray(redact)) {\n\t\treturn [...DEFAULT_REDACT_PATHS, ...redact];\n\t}\n\n\t// Object syntax - check resolution mode\n\tconst { resolution = 'merge', paths, censor, remove } = redact;\n\n\tconst resolvedPaths =\n\t\tresolution === 'override' ? paths : [...DEFAULT_REDACT_PATHS, ...paths];\n\n\t// Return clean pino config without our resolution field\n\tconst config: PinoRedactConfig = { paths: resolvedPaths };\n\tif (censor !== undefined) config.censor = censor;\n\tif (remove !== undefined) config.remove = remove;\n\n\treturn config;\n}\n\n/**\n * Creates a pino logger instance with optional redaction support.\n *\n * @param options - Logger configuration options\n * @returns A configured pino logger instance\n *\n * @example\n * ```typescript\n * // Basic logger — sensitive paths are redacted by default\n * const logger = createLogger({ level: 'debug' });\n *\n * // Opt out of redaction\n * const rawLogger = createLogger({ redact: false });\n *\n * // Pretty printing in development\n * const devLogger = createLogger({ pretty: true });\n * ```\n */\nexport function createLogger(options: CreateLoggerOptions = {}) {\n\t// Pretty printing spawns a transport worker and writes for humans; in\n\t// production the output is for a log pipeline, so `pretty` is ignored there.\n\tconst pretty = options?.pretty && process.env.NODE_ENV !== 'production';\n\tconst baseOptions = pretty\n\t\t? {\n\t\t\t\ttransport: {\n\t\t\t\t\ttarget: 'pino-pretty',\n\t\t\t\t\toptions: { colorize: true },\n\t\t\t\t},\n\t\t\t}\n\t\t: {};\n\n\tconst redact = resolveRedactConfig(options.redact);\n\n\t// An Error is serialized only under a key pino has a serializer for — `err`\n\t// by default — and anything else is JSON.stringify'd, which an Error survives\n\t// as `{}`: no message, no stack. `logger.error({ error }, …)` is how most\n\t// code writes it, so `error` gets the same serializer as `err`.\n\t//\n\t// The serialized copy is a plain object, so URL credentials in the message\n\t// or the stack are masked like any other field; path redaction runs after\n\t// serializers, so `err.message` and `error.stack` are paths it can reach.\n\tconst serializeError = (value: unknown) => {\n\t\tconst serialized = stdSerializers.err(value as Error);\n\t\tif (!redact || serialized === value) return serialized;\n\t\treturn redactUrlCredentialsIn({ ...serialized });\n\t};\n\n\tconst pinoOptions = {\n\t\t...baseOptions,\n\t\t...(options.level && { level: options.level }),\n\t\t...(redact && { redact }),\n\t\tserializers: { err: serializeError, error: serializeError },\n\t\thooks: {\n\t\t\t// Redaction also masks the credentials of any URL, in any field or\n\t\t\t// in the message: `s3://KEY:SECRET@uploads` is logged with its\n\t\t\t// secret under a name no path list can predict.\n\t\t\t...(redact && {\n\t\t\t\tlogMethod(this: Logger, args: Parameters<LogFn>, method: LogFn) {\n\t\t\t\t\tconst redacted = args.map((arg) =>\n\t\t\t\t\t\ttypeof arg === 'string' ? redactUrlCredentials(arg) : arg,\n\t\t\t\t\t) as Parameters<LogFn>;\n\t\t\t\t\treturn method.apply(this, redacted);\n\t\t\t\t},\n\t\t\t}),\n\t\t\t// Each line, once redacted and serialized, also goes to\n\t\t\t// OpenTelemetry when a LoggerProvider is registered. On the calling\n\t\t\t// thread, before any transport, so the active span is the caller's.\n\t\t\tstreamWrite: otelStreamWrite,\n\t\t},\n\t\tformatters: {\n\t\t\t...(redact && {\n\t\t\t\tlog: (object: Record<string, unknown>) =>\n\t\t\t\t\tredactUrlCredentialsIn(object),\n\t\t\t}),\n\t\t\tbindings() {\n\t\t\t\treturn { nodeVersion: process.version };\n\t\t\t},\n\t\t\tlevel: (label: string) => {\n\t\t\t\treturn { level: label.toUpperCase() };\n\t\t\t},\n\t\t},\n\t};\n\n\t// A transport writes on its own worker, so a destination only applies when\n\t// the logger is not pretty-printing.\n\treturn options.destination && !pretty\n\t\t? pino(pinoOptions, options.destination)\n\t\t: pino(pinoOptions);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAS,oBACR,QAC+B;CAC/B,IAAI,WAAW,OACd;CAGD,IAAI,WAAW,KAAA,KAAa,WAAW,MACtC,OAAOA,qBAAAA;CAIR,IAAI,MAAM,QAAQ,MAAM,GACvB,OAAO,CAAC,GAAGA,qBAAAA,sBAAsB,GAAG,MAAM;CAI3C,MAAM,EAAE,aAAa,SAAS,OAAO,QAAQ,WAAW;CAMxD,MAAM,SAA2B,EAAE,OAHlC,eAAe,aAAa,QAAQ,CAAC,GAAGA,qBAAAA,sBAAsB,GAAG,KAAK,EAGf;CACxD,IAAI,WAAW,KAAA,GAAW,OAAO,SAAS;CAC1C,IAAI,WAAW,KAAA,GAAW,OAAO,SAAS;CAE1C,OAAO;AACR;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,aAAa,UAA+B,CAAC,GAAG;CAG/D,MAAM,SAAS,SAAS,UAAU,QAAQ,IAAI,aAAa;CAC3D,MAAM,cAAc,SACjB,EACA,WAAW;EACV,QAAQ;EACR,SAAS,EAAE,UAAU,KAAK;CAC3B,EACD,IACC,CAAC;CAEJ,MAAM,SAAS,oBAAoB,QAAQ,MAAM;CAUjD,MAAM,kBAAkB,UAAmB;EAC1C,MAAM,aAAaC,KAAAA,eAAe,IAAI,KAAc;EACpD,IAAI,CAAC,UAAU,eAAe,OAAO,OAAO;EAC5C,OAAOC,qBAAAA,uBAAuB,EAAE,GAAG,WAAW,CAAC;CAChD;CAEA,MAAM,cAAc;EACnB,GAAG;EACH,GAAI,QAAQ,SAAS,EAAE,OAAO,QAAQ,MAAM;EAC5C,GAAI,UAAU,EAAE,OAAO;EACvB,aAAa;GAAE,KAAK;GAAgB,OAAO;EAAe;EAC1D,OAAO;GAIN,GAAI,UAAU,EACb,UAAwB,MAAyB,QAAe;IAC/D,MAAM,WAAW,KAAK,KAAK,QAC1B,OAAO,QAAQ,WAAWC,qBAAAA,qBAAqB,GAAG,IAAI,GACvD;IACA,OAAO,OAAO,MAAM,MAAM,QAAQ;GACnC,EACD;GAIA,aAAaC,aAAAA;EACd;EACA,YAAY;GACX,GAAI,UAAU,EACb,MAAM,WACLF,qBAAAA,uBAAuB,MAAM,EAC/B;GACA,WAAW;IACV,OAAO,EAAE,aAAa,QAAQ,QAAQ;GACvC;GACA,QAAQ,UAAkB;IACzB,OAAO,EAAE,OAAO,MAAM,YAAY,EAAE;GACrC;EACD;CACD;CAIA,OAAO,QAAQ,eAAe,CAAC,UAAA,GAC5B,KAAA,KAAA,CAAK,aAAa,QAAQ,WAAW,KAAA,GACrC,KAAA,KAAA,CAAK,WAAW;AACpB"}
|
package/dist/pino.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pino.d.cts","names":[],"sources":["../src/pino.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"pino.d.cts","names":[],"sources":["../src/pino.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;wBA+GgB,aAAa,UAAS,sBAAwB"}
|
package/dist/pino.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pino.d.mts","names":[],"sources":["../src/pino.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"pino.d.mts","names":[],"sources":["../src/pino.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;wBA+GgB,aAAa,UAAS,sBAAwB"}
|
package/dist/pino.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { otelStreamWrite } from "./otel.mjs";
|
|
1
2
|
import { DEFAULT_REDACT_PATHS, redactUrlCredentials, redactUrlCredentialsIn } from "./redact-paths.mjs";
|
|
2
|
-
import { pino } from "pino";
|
|
3
|
+
import { pino, stdSerializers } from "pino";
|
|
3
4
|
//#region src/pino.ts
|
|
4
5
|
/**
|
|
5
6
|
* Pino logger with built-in redaction support for sensitive data.
|
|
@@ -76,14 +77,26 @@ function createLogger(options = {}) {
|
|
|
76
77
|
options: { colorize: true }
|
|
77
78
|
} } : {};
|
|
78
79
|
const redact = resolveRedactConfig(options.redact);
|
|
80
|
+
const serializeError = (value) => {
|
|
81
|
+
const serialized = stdSerializers.err(value);
|
|
82
|
+
if (!redact || serialized === value) return serialized;
|
|
83
|
+
return redactUrlCredentialsIn({ ...serialized });
|
|
84
|
+
};
|
|
79
85
|
const pinoOptions = {
|
|
80
86
|
...baseOptions,
|
|
81
87
|
...options.level && { level: options.level },
|
|
82
88
|
...redact && { redact },
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
89
|
+
serializers: {
|
|
90
|
+
err: serializeError,
|
|
91
|
+
error: serializeError
|
|
92
|
+
},
|
|
93
|
+
hooks: {
|
|
94
|
+
...redact && { logMethod(args, method) {
|
|
95
|
+
const redacted = args.map((arg) => typeof arg === "string" ? redactUrlCredentials(arg) : arg);
|
|
96
|
+
return method.apply(this, redacted);
|
|
97
|
+
} },
|
|
98
|
+
streamWrite: otelStreamWrite
|
|
99
|
+
},
|
|
87
100
|
formatters: {
|
|
88
101
|
...redact && { log: (object) => redactUrlCredentialsIn(object) },
|
|
89
102
|
bindings() {
|
package/dist/pino.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pino.mjs","names":[],"sources":["../src/pino.ts"],"sourcesContent":["/**\n * Pino logger with built-in redaction support for sensitive data.\n *\n * Redaction is on by default: a logger that was never told about passwords\n * and tokens still masks them. `redact: false` is the explicit opt-out.\n *\n * @example\n * ```typescript\n * import { createLogger, DEFAULT_REDACT_PATHS } from '@geekmidas/logger/pino';\n *\n * // Redaction with sensible defaults — the same as `redact: true`\n * const logger = createLogger();\n *\n * // Sensitive data is automatically masked\n * logger.info({ password: 'secret123', user: 'john' }, 'Login');\n * // Output: { password: '[Redacted]', user: 'john' } Login\n *\n * // Add custom paths (merged with defaults)\n * const logger2 = createLogger({ redact: ['user.ssn'] });\n *\n * // Override defaults for full control\n * const logger3 = createLogger({\n * redact: {\n * paths: ['onlyThis'],\n * resolution: 'override',\n * }\n * });\n * ```\n *\n * @module\n */\nimport { type LogFn, type Logger, pino } from 'pino';\nimport {\n\tDEFAULT_REDACT_PATHS,\n\tredactUrlCredentials,\n\tredactUrlCredentialsIn,\n} from './redact-paths';\nimport type { CreateLoggerOptions, RedactOptions } from './types';\n\n// Re-export for backwards compatibility\nexport { DEFAULT_REDACT_PATHS } from './redact-paths';\n\n/**\n * Type for the resolved pino redact config (without our custom resolution field).\n */\ntype PinoRedactConfig =\n\t| string[]\n\t| {\n\t\t\tpaths: string[];\n\t\t\tcensor?: string | ((value: unknown, path: string[]) => unknown);\n\t\t\tremove?: boolean;\n\t };\n\n/**\n * Resolves redaction configuration from options.\n * Returns undefined only when redaction is turned off with `false`; leaving it\n * unset is the same as `true`, so sensitive paths are masked unless someone\n * decides they should not be.\n *\n * By default (resolution: 'merge'), custom paths are merged with DEFAULT_REDACT_PATHS.\n * With resolution: 'override', only the custom paths are used.\n */\nfunction resolveRedactConfig(\n\tredact: boolean | RedactOptions | undefined,\n): PinoRedactConfig | undefined {\n\tif (redact === false) {\n\t\treturn undefined;\n\t}\n\n\tif (redact === undefined || redact === true) {\n\t\treturn DEFAULT_REDACT_PATHS;\n\t}\n\n\t// Array syntax - merge with defaults\n\tif (Array.isArray(redact)) {\n\t\treturn [...DEFAULT_REDACT_PATHS, ...redact];\n\t}\n\n\t// Object syntax - check resolution mode\n\tconst { resolution = 'merge', paths, censor, remove } = redact;\n\n\tconst resolvedPaths =\n\t\tresolution === 'override' ? paths : [...DEFAULT_REDACT_PATHS, ...paths];\n\n\t// Return clean pino config without our resolution field\n\tconst config: PinoRedactConfig = { paths: resolvedPaths };\n\tif (censor !== undefined) config.censor = censor;\n\tif (remove !== undefined) config.remove = remove;\n\n\treturn config;\n}\n\n/**\n * Creates a pino logger instance with optional redaction support.\n *\n * @param options - Logger configuration options\n * @returns A configured pino logger instance\n *\n * @example\n * ```typescript\n * // Basic logger — sensitive paths are redacted by default\n * const logger = createLogger({ level: 'debug' });\n *\n * // Opt out of redaction\n * const rawLogger = createLogger({ redact: false });\n *\n * // Pretty printing in development\n * const devLogger = createLogger({ pretty: true });\n * ```\n */\nexport function createLogger(options: CreateLoggerOptions = {}) {\n\t// Pretty printing spawns a transport worker and writes for humans; in\n\t// production the output is for a log pipeline, so `pretty` is ignored there.\n\tconst pretty = options?.pretty && process.env.NODE_ENV !== 'production';\n\tconst baseOptions = pretty\n\t\t? {\n\t\t\t\ttransport: {\n\t\t\t\t\ttarget: 'pino-pretty',\n\t\t\t\t\toptions: { colorize: true },\n\t\t\t\t},\n\t\t\t}\n\t\t: {};\n\n\tconst redact = resolveRedactConfig(options.redact);\n\n\tconst pinoOptions = {\n\t\t...baseOptions,\n\t\t...(options.level && { level: options.level }),\n\t\t...(redact && { redact }),\n\t\t// Redaction also masks the credentials of any URL, in any field or
|
|
1
|
+
{"version":3,"file":"pino.mjs","names":[],"sources":["../src/pino.ts"],"sourcesContent":["/**\n * Pino logger with built-in redaction support for sensitive data.\n *\n * Redaction is on by default: a logger that was never told about passwords\n * and tokens still masks them. `redact: false` is the explicit opt-out.\n *\n * @example\n * ```typescript\n * import { createLogger, DEFAULT_REDACT_PATHS } from '@geekmidas/logger/pino';\n *\n * // Redaction with sensible defaults — the same as `redact: true`\n * const logger = createLogger();\n *\n * // Sensitive data is automatically masked\n * logger.info({ password: 'secret123', user: 'john' }, 'Login');\n * // Output: { password: '[Redacted]', user: 'john' } Login\n *\n * // Add custom paths (merged with defaults)\n * const logger2 = createLogger({ redact: ['user.ssn'] });\n *\n * // Override defaults for full control\n * const logger3 = createLogger({\n * redact: {\n * paths: ['onlyThis'],\n * resolution: 'override',\n * }\n * });\n * ```\n *\n * @module\n */\nimport { type LogFn, type Logger, pino, stdSerializers } from 'pino';\nimport { otelStreamWrite } from './otel';\nimport {\n\tDEFAULT_REDACT_PATHS,\n\tredactUrlCredentials,\n\tredactUrlCredentialsIn,\n} from './redact-paths';\nimport type { CreateLoggerOptions, RedactOptions } from './types';\n\n// Re-export for backwards compatibility\nexport { DEFAULT_REDACT_PATHS } from './redact-paths';\n\n/**\n * Type for the resolved pino redact config (without our custom resolution field).\n */\ntype PinoRedactConfig =\n\t| string[]\n\t| {\n\t\t\tpaths: string[];\n\t\t\tcensor?: string | ((value: unknown, path: string[]) => unknown);\n\t\t\tremove?: boolean;\n\t };\n\n/**\n * Resolves redaction configuration from options.\n * Returns undefined only when redaction is turned off with `false`; leaving it\n * unset is the same as `true`, so sensitive paths are masked unless someone\n * decides they should not be.\n *\n * By default (resolution: 'merge'), custom paths are merged with DEFAULT_REDACT_PATHS.\n * With resolution: 'override', only the custom paths are used.\n */\nfunction resolveRedactConfig(\n\tredact: boolean | RedactOptions | undefined,\n): PinoRedactConfig | undefined {\n\tif (redact === false) {\n\t\treturn undefined;\n\t}\n\n\tif (redact === undefined || redact === true) {\n\t\treturn DEFAULT_REDACT_PATHS;\n\t}\n\n\t// Array syntax - merge with defaults\n\tif (Array.isArray(redact)) {\n\t\treturn [...DEFAULT_REDACT_PATHS, ...redact];\n\t}\n\n\t// Object syntax - check resolution mode\n\tconst { resolution = 'merge', paths, censor, remove } = redact;\n\n\tconst resolvedPaths =\n\t\tresolution === 'override' ? paths : [...DEFAULT_REDACT_PATHS, ...paths];\n\n\t// Return clean pino config without our resolution field\n\tconst config: PinoRedactConfig = { paths: resolvedPaths };\n\tif (censor !== undefined) config.censor = censor;\n\tif (remove !== undefined) config.remove = remove;\n\n\treturn config;\n}\n\n/**\n * Creates a pino logger instance with optional redaction support.\n *\n * @param options - Logger configuration options\n * @returns A configured pino logger instance\n *\n * @example\n * ```typescript\n * // Basic logger — sensitive paths are redacted by default\n * const logger = createLogger({ level: 'debug' });\n *\n * // Opt out of redaction\n * const rawLogger = createLogger({ redact: false });\n *\n * // Pretty printing in development\n * const devLogger = createLogger({ pretty: true });\n * ```\n */\nexport function createLogger(options: CreateLoggerOptions = {}) {\n\t// Pretty printing spawns a transport worker and writes for humans; in\n\t// production the output is for a log pipeline, so `pretty` is ignored there.\n\tconst pretty = options?.pretty && process.env.NODE_ENV !== 'production';\n\tconst baseOptions = pretty\n\t\t? {\n\t\t\t\ttransport: {\n\t\t\t\t\ttarget: 'pino-pretty',\n\t\t\t\t\toptions: { colorize: true },\n\t\t\t\t},\n\t\t\t}\n\t\t: {};\n\n\tconst redact = resolveRedactConfig(options.redact);\n\n\t// An Error is serialized only under a key pino has a serializer for — `err`\n\t// by default — and anything else is JSON.stringify'd, which an Error survives\n\t// as `{}`: no message, no stack. `logger.error({ error }, …)` is how most\n\t// code writes it, so `error` gets the same serializer as `err`.\n\t//\n\t// The serialized copy is a plain object, so URL credentials in the message\n\t// or the stack are masked like any other field; path redaction runs after\n\t// serializers, so `err.message` and `error.stack` are paths it can reach.\n\tconst serializeError = (value: unknown) => {\n\t\tconst serialized = stdSerializers.err(value as Error);\n\t\tif (!redact || serialized === value) return serialized;\n\t\treturn redactUrlCredentialsIn({ ...serialized });\n\t};\n\n\tconst pinoOptions = {\n\t\t...baseOptions,\n\t\t...(options.level && { level: options.level }),\n\t\t...(redact && { redact }),\n\t\tserializers: { err: serializeError, error: serializeError },\n\t\thooks: {\n\t\t\t// Redaction also masks the credentials of any URL, in any field or\n\t\t\t// in the message: `s3://KEY:SECRET@uploads` is logged with its\n\t\t\t// secret under a name no path list can predict.\n\t\t\t...(redact && {\n\t\t\t\tlogMethod(this: Logger, args: Parameters<LogFn>, method: LogFn) {\n\t\t\t\t\tconst redacted = args.map((arg) =>\n\t\t\t\t\t\ttypeof arg === 'string' ? redactUrlCredentials(arg) : arg,\n\t\t\t\t\t) as Parameters<LogFn>;\n\t\t\t\t\treturn method.apply(this, redacted);\n\t\t\t\t},\n\t\t\t}),\n\t\t\t// Each line, once redacted and serialized, also goes to\n\t\t\t// OpenTelemetry when a LoggerProvider is registered. On the calling\n\t\t\t// thread, before any transport, so the active span is the caller's.\n\t\t\tstreamWrite: otelStreamWrite,\n\t\t},\n\t\tformatters: {\n\t\t\t...(redact && {\n\t\t\t\tlog: (object: Record<string, unknown>) =>\n\t\t\t\t\tredactUrlCredentialsIn(object),\n\t\t\t}),\n\t\t\tbindings() {\n\t\t\t\treturn { nodeVersion: process.version };\n\t\t\t},\n\t\t\tlevel: (label: string) => {\n\t\t\t\treturn { level: label.toUpperCase() };\n\t\t\t},\n\t\t},\n\t};\n\n\t// A transport writes on its own worker, so a destination only applies when\n\t// the logger is not pretty-printing.\n\treturn options.destination && !pretty\n\t\t? pino(pinoOptions, options.destination)\n\t\t: pino(pinoOptions);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAS,oBACR,QAC+B;CAC/B,IAAI,WAAW,OACd;CAGD,IAAI,WAAW,KAAA,KAAa,WAAW,MACtC,OAAO;CAIR,IAAI,MAAM,QAAQ,MAAM,GACvB,OAAO,CAAC,GAAG,sBAAsB,GAAG,MAAM;CAI3C,MAAM,EAAE,aAAa,SAAS,OAAO,QAAQ,WAAW;CAMxD,MAAM,SAA2B,EAAE,OAHlC,eAAe,aAAa,QAAQ,CAAC,GAAG,sBAAsB,GAAG,KAAK,EAGf;CACxD,IAAI,WAAW,KAAA,GAAW,OAAO,SAAS;CAC1C,IAAI,WAAW,KAAA,GAAW,OAAO,SAAS;CAE1C,OAAO;AACR;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,aAAa,UAA+B,CAAC,GAAG;CAG/D,MAAM,SAAS,SAAS,UAAU,QAAQ,IAAI,aAAa;CAC3D,MAAM,cAAc,SACjB,EACA,WAAW;EACV,QAAQ;EACR,SAAS,EAAE,UAAU,KAAK;CAC3B,EACD,IACC,CAAC;CAEJ,MAAM,SAAS,oBAAoB,QAAQ,MAAM;CAUjD,MAAM,kBAAkB,UAAmB;EAC1C,MAAM,aAAa,eAAe,IAAI,KAAc;EACpD,IAAI,CAAC,UAAU,eAAe,OAAO,OAAO;EAC5C,OAAO,uBAAuB,EAAE,GAAG,WAAW,CAAC;CAChD;CAEA,MAAM,cAAc;EACnB,GAAG;EACH,GAAI,QAAQ,SAAS,EAAE,OAAO,QAAQ,MAAM;EAC5C,GAAI,UAAU,EAAE,OAAO;EACvB,aAAa;GAAE,KAAK;GAAgB,OAAO;EAAe;EAC1D,OAAO;GAIN,GAAI,UAAU,EACb,UAAwB,MAAyB,QAAe;IAC/D,MAAM,WAAW,KAAK,KAAK,QAC1B,OAAO,QAAQ,WAAW,qBAAqB,GAAG,IAAI,GACvD;IACA,OAAO,OAAO,MAAM,MAAM,QAAQ;GACnC,EACD;GAIA,aAAa;EACd;EACA,YAAY;GACX,GAAI,UAAU,EACb,MAAM,WACL,uBAAuB,MAAM,EAC/B;GACA,WAAW;IACV,OAAO,EAAE,aAAa,QAAQ,QAAQ;GACvC;GACA,QAAQ,UAAkB;IACzB,OAAO,EAAE,OAAO,MAAM,YAAY,EAAE;GACrC;EACD;CACD;CAIA,OAAO,QAAQ,eAAe,CAAC,SAC5B,KAAK,aAAa,QAAQ,WAAW,IACrC,KAAK,WAAW;AACpB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/logger",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.71",
|
|
4
4
|
"description": "A simple and flexible logging library for Node.js and the browser",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -35,6 +35,16 @@
|
|
|
35
35
|
"default": "./dist/console.cjs"
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
|
+
"./otel": {
|
|
39
|
+
"import": {
|
|
40
|
+
"types": "./dist/otel.d.mts",
|
|
41
|
+
"default": "./dist/otel.mjs"
|
|
42
|
+
},
|
|
43
|
+
"require": {
|
|
44
|
+
"types": "./dist/otel.d.cts",
|
|
45
|
+
"default": "./dist/otel.cjs"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
38
48
|
"./redact": {
|
|
39
49
|
"import": {
|
|
40
50
|
"types": "./dist/redact-paths.d.mts",
|
|
@@ -57,8 +67,14 @@
|
|
|
57
67
|
"registry": "https://registry.npmjs.org/",
|
|
58
68
|
"access": "public"
|
|
59
69
|
},
|
|
60
|
-
"dependencies": {
|
|
61
|
-
|
|
70
|
+
"dependencies": {
|
|
71
|
+
"@opentelemetry/api-logs": "~0.222.0"
|
|
72
|
+
},
|
|
73
|
+
"devDependencies": {
|
|
74
|
+
"@opentelemetry/api": "~1.9.1",
|
|
75
|
+
"@opentelemetry/sdk-logs": "~0.222.0",
|
|
76
|
+
"@opentelemetry/sdk-trace-node": "~2.11.0"
|
|
77
|
+
},
|
|
62
78
|
"peerDependencies": {
|
|
63
79
|
"pino-pretty": ">=13.1.3",
|
|
64
80
|
"pino": ">=10.3.1"
|