@ogcio/fastify-logging-wrapper 5.3.1 → 5.4.1
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 +29 -0
- package/README.md +32 -0
- package/__tests__/fastify-logging-wrapper.errors.test.ts +2 -2
- package/__tests__/fastify-logging-wrapper.test.ts +85 -3
- package/__tests__/field-filter.test.ts +57 -0
- package/__tests__/helpers/build-fastify.ts +20 -2
- package/__tests__/helpers/fastify-test-helpers.ts +7 -5
- package/__tests__/logging-filtering.test.ts +99 -0
- package/__tests__/logging-wrapper.test.ts +8 -7
- package/coverage/cobertura-coverage.xml +347 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/fastify-logging-wrapper.ts.html +706 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/field-filter.ts.html +223 -0
- package/coverage/lcov-report/index.html +176 -0
- package/coverage/lcov-report/index.ts.html +130 -0
- package/coverage/lcov-report/logging-wrapper-entities.ts.html +484 -0
- package/coverage/lcov-report/logging-wrapper.ts.html +481 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov.info +372 -0
- package/dist/fastify-logging-wrapper.d.ts +2 -1
- package/dist/fastify-logging-wrapper.d.ts.map +1 -1
- package/dist/fastify-logging-wrapper.js +58 -5
- package/dist/fastify-logging-wrapper.js.map +1 -1
- package/dist/field-filter.d.ts +17 -0
- package/dist/field-filter.d.ts.map +1 -0
- package/dist/field-filter.js +22 -0
- package/dist/field-filter.js.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/logging-wrapper-entities.d.ts +9 -4
- package/dist/logging-wrapper-entities.d.ts.map +1 -1
- package/dist/logging-wrapper-entities.js.map +1 -1
- package/dist/logging-wrapper.d.ts +4 -1
- package/dist/logging-wrapper.d.ts.map +1 -1
- package/dist/logging-wrapper.js +25 -8
- package/dist/logging-wrapper.js.map +1 -1
- package/package.json +38 -34
- package/src/fastify-logging-wrapper.ts +91 -8
- package/src/field-filter.ts +46 -0
- package/src/index.ts +7 -6
- package/src/logging-wrapper-entities.ts +11 -5
- package/src/logging-wrapper.ts +34 -9
- package/tsconfig.json +9 -11
- package/tsconfig.prod.tsbuildinfo +1 -0
- package/vitest.config.mts +8 -3
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface FieldRules {
|
|
2
|
+
omit?: string[];
|
|
3
|
+
mask?: string[];
|
|
4
|
+
}
|
|
5
|
+
export interface LoggingOptions {
|
|
6
|
+
request?: {
|
|
7
|
+
headers?: FieldRules;
|
|
8
|
+
queryParams?: FieldRules;
|
|
9
|
+
clientIp?: "omit";
|
|
10
|
+
userAgent?: "omit";
|
|
11
|
+
};
|
|
12
|
+
response?: {
|
|
13
|
+
headers?: FieldRules;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export declare const applyFieldRules: (value: unknown, rules: FieldRules | undefined, caseInsensitive?: boolean) => unknown;
|
|
17
|
+
//# sourceMappingURL=field-filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"field-filter.d.ts","sourceRoot":"","sources":["../src/field-filter.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE;QACR,OAAO,CAAC,EAAE,UAAU,CAAC;QACrB,WAAW,CAAC,EAAE,UAAU,CAAC;QACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IACF,QAAQ,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,UAAU,CAAC;KACtB,CAAC;CACH;AAED,eAAO,MAAM,eAAe,GAC1B,OAAO,OAAO,EACd,OAAO,UAAU,GAAG,SAAS,EAC7B,yBAAuB,KACtB,OAsBF,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { REDACTED_VALUE } from "./logging-wrapper-entities.js";
|
|
2
|
+
export const applyFieldRules = (value, rules, caseInsensitive = false) => {
|
|
3
|
+
if (!rules || (!rules.omit?.length && !rules.mask?.length)) {
|
|
4
|
+
return value;
|
|
5
|
+
}
|
|
6
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
const normalize = (key) => caseInsensitive ? key.toLowerCase() : key;
|
|
10
|
+
const toOmit = new Set((rules.omit ?? []).map(normalize));
|
|
11
|
+
const toMask = new Set((rules.mask ?? []).map(normalize));
|
|
12
|
+
const result = {};
|
|
13
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
14
|
+
const normalized = normalize(key);
|
|
15
|
+
if (toOmit.has(normalized)) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
result[key] = toMask.has(normalized) ? REDACTED_VALUE : entry;
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=field-filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"field-filter.js","sourceRoot":"","sources":["../src/field-filter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAmB/D,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,KAAc,EACd,KAA6B,EAC7B,eAAe,GAAG,KAAK,EACd,EAAE;IACX,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC;QAC3D,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACxE,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,SAAS,GAAG,CAAC,GAAW,EAAU,EAAE,CACxC,eAAe,CAAC,CAAC,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;IAC5C,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;IAC1D,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;IAE1D,MAAM,MAAM,GAA4B,EAAE,CAAC;IAC3C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,EAAE,CAAC;QAC5E,MAAM,UAAU,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;YAC3B,SAAS;QACX,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC;IAChE,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { getLoggingConfiguration, initializeLoggingHooks, } from "./fastify-logging-wrapper.js";
|
|
2
|
-
export {
|
|
3
|
-
export { getLoggingContextError, setLoggingContext, } from "./logging-wrapper.js";
|
|
2
|
+
export type { FieldRules, LoggingOptions } from "./field-filter.js";
|
|
3
|
+
export { getLoggingContext, getLoggingContextError, setLoggingContext, } from "./logging-wrapper.js";
|
|
4
|
+
export { LoggingError, LogMessages, toLoggingError, } from "./logging-wrapper-entities.js";
|
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,8BAA8B,CAAC;AACtC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,8BAA8B,CAAC;AACtC,YAAY,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,YAAY,EACZ,WAAW,EACX,cAAc,GACf,MAAM,+BAA+B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { getLoggingConfiguration, initializeLoggingHooks, } from "./fastify-logging-wrapper.js";
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export { getLoggingContext, getLoggingContextError, setLoggingContext, } from "./logging-wrapper.js";
|
|
3
|
+
export { LogMessages, toLoggingError, } from "./logging-wrapper-entities.js";
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAEL,WAAW,EACX,cAAc,GACf,MAAM,+BAA+B,CAAC"}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import type { FastifyError } from "fastify";
|
|
2
1
|
import type { HttpError } from "@fastify/sensible";
|
|
2
|
+
import type { FastifyError } from "fastify";
|
|
3
3
|
export interface LoggingRequest {
|
|
4
4
|
scheme: string;
|
|
5
5
|
method: string;
|
|
6
6
|
path: string | undefined;
|
|
7
7
|
hostname: string;
|
|
8
8
|
query_params: unknown;
|
|
9
|
-
port: number;
|
|
9
|
+
port: number | null;
|
|
10
10
|
[key: string]: unknown;
|
|
11
11
|
}
|
|
12
12
|
export interface FullLoggingRequest extends LoggingRequest {
|
|
13
13
|
headers: unknown;
|
|
14
|
-
user_agent
|
|
15
|
-
client_ip
|
|
14
|
+
user_agent?: string;
|
|
15
|
+
client_ip?: string;
|
|
16
16
|
}
|
|
17
17
|
export interface LoggingResponse {
|
|
18
18
|
headers: unknown;
|
|
@@ -30,10 +30,15 @@ export interface LoggingError {
|
|
|
30
30
|
};
|
|
31
31
|
[key: string]: unknown;
|
|
32
32
|
}
|
|
33
|
+
export interface LoggingPseudoUser {
|
|
34
|
+
id: string;
|
|
35
|
+
version: string;
|
|
36
|
+
}
|
|
33
37
|
export interface LoggingContext {
|
|
34
38
|
request?: LoggingRequest;
|
|
35
39
|
response?: LoggingResponse;
|
|
36
40
|
error?: LoggingError;
|
|
41
|
+
pseudoUser?: LoggingPseudoUser;
|
|
37
42
|
}
|
|
38
43
|
export declare enum LogMessages {
|
|
39
44
|
NewRequest = "NEW_REQUEST",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logging-wrapper-entities.d.ts","sourceRoot":"","sources":["../src/logging-wrapper-entities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"logging-wrapper-entities.d.ts","sourceRoot":"","sources":["../src/logging-wrapper-entities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAG5C,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,OAAO,CAAC;IACtB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,kBAAmB,SAAQ,cAAc;IACxD,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,eAAe,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3D,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,QAAQ,CAAC,EAAE,eAAe,CAAC;IAC3B,KAAK,CAAC,EAAE,YAAY,CAAC;IACrB,UAAU,CAAC,EAAE,iBAAiB,CAAC;CAChC;AAED,oBAAY,WAAW;IACrB,UAAU,gBAAgB;IAC1B,QAAQ,aAAa;IACrB,KAAK,UAAU;IACf,QAAQ,cAAc;CACvB;AAED,oBAAY,eAAe;IACzB,WAAW,iBAAiB;IAC5B,eAAe,qBAAqB;IACpC,YAAY,kBAAkB;IAC9B,YAAY,kBAAkB;IAC9B,YAAY,kBAAkB;CAC/B;AAED,eAAO,MAAM,cAAc,eAAe,CAAC;AAE3C,eAAO,MAAM,cAAc,UAO1B,CAAC;AAEF,eAAO,MAAM,WAAW,YAAY,CAAC;AAErC,eAAO,MAAM,oBAAoB,eAAe,CAAC;AAIjD,eAAO,MAAM,cAAc,GACzB,OAAO,SAAS,GAAG,YAAY,KAC9B,YAyBF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logging-wrapper-entities.js","sourceRoot":"","sources":["../src/logging-wrapper-entities.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"logging-wrapper-entities.js","sourceRoot":"","sources":["../src/logging-wrapper-entities.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA4C1C,MAAM,CAAN,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,yCAA0B,CAAA;IAC1B,oCAAqB,CAAA;IACrB,8BAAe,CAAA;IACf,qCAAsB,CAAA;A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AI,wBAAwB;KAC7C,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,eAAe,GAAG,CAAC,KAA+B,EAAmB,EAAE;IAC3E,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;QACtB,OAAO,eAAe,CAAC,YAAY,CAAC;IACtC,CAAC;IAED,MAAM,UAAU,GAAG,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAE5C,IAAI,UAAU,KAAK,GAAG,EAAE,CAAC;QACvB,OAAO,eAAe,CAAC,YAAY,CAAC;IACtC,CAAC;IAED,IAAI,UAAU,IAAI,GAAG,EAAE,CAAC;QACtB,OAAO,eAAe,CAAC,WAAW,CAAC;IACrC,CAAC;IAED,IAAI,UAAU,KAAK,GAAG,EAAE,CAAC;QACvB,OAAO,eAAe,CAAC,eAAe,CAAC;IACzC,CAAC;IAED,IAAI,UAAU,IAAI,GAAG,EAAE,CAAC;QACtB,OAAO,eAAe,CAAC,YAAY,CAAC;IACtC,CAAC;IAED,OAAO,eAAe,CAAC,YAAY,CAAC;AACtC,CAAC,CAAC"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { HttpError } from "@fastify/sensible";
|
|
2
2
|
import type { FastifyError, FastifyReply, FastifyRequest } from "fastify";
|
|
3
3
|
import type { LogLevel, PinoLoggerOptions } from "fastify/types/logger.js";
|
|
4
|
-
import { type
|
|
4
|
+
import { type LoggingOptions } from "./field-filter.js";
|
|
5
|
+
import { type FullLoggingRequest, type LoggingContext, type LoggingError, type LoggingPseudoUser } from "./logging-wrapper-entities.js";
|
|
6
|
+
export declare const setLoggingOptions: (options?: LoggingOptions) => void;
|
|
5
7
|
type INPUT_ERROR_TYPES = FastifyError | HttpError;
|
|
6
8
|
export declare const getLoggingContext: (params: {
|
|
7
9
|
includeError: boolean;
|
|
@@ -10,6 +12,7 @@ export declare const setLoggingContext: (params: {
|
|
|
10
12
|
request?: FastifyRequest;
|
|
11
13
|
response?: FastifyReply;
|
|
12
14
|
error?: INPUT_ERROR_TYPES;
|
|
15
|
+
pseudoUser?: LoggingPseudoUser;
|
|
13
16
|
}) => void;
|
|
14
17
|
export declare const resetLoggingContext: () => void;
|
|
15
18
|
export declare const getLoggingContextError: () => LoggingError | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logging-wrapper.d.ts","sourceRoot":"","sources":["../src/logging-wrapper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"logging-wrapper.d.ts","sourceRoot":"","sources":["../src/logging-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,KAAK,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE3E,OAAO,EAAmB,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EAOvB,MAAM,+BAA+B,CAAC;AAMvC,eAAO,MAAM,iBAAiB,GAAI,UAAS,cAAmB,KAAG,IAEhE,CAAC;AAEF,KAAK,iBAAiB,GAAG,YAAY,GAAG,SAAS,CAAC;AAElD,eAAO,MAAM,iBAAiB,GAAI,QAAQ;IACxC,YAAY,EAAE,OAAO,CAAC;CACvB,KAAG,cAGyC,CAAC;AAE9C,eAAO,MAAM,iBAAiB,GAAI,QAAQ;IACxC,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,QAAQ,CAAC,EAAE,YAAY,CAAC;IACxB,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAC1B,UAAU,CAAC,EAAE,iBAAiB,CAAC;CAChC,KAAG,IAaH,CAAC;AAEF,eAAO,MAAM,mBAAmB,QAAO,IAKtC,CAAC;AAEF,eAAO,MAAM,sBAAsB,QAAO,YAAY,GAAG,SACR,CAAC;AAElD,eAAO,MAAM,6BAA6B,QACtC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC,GAC3B,SAGF,CAAC;AAcH,eAAO,MAAM,uBAAuB,GAClC,KAAK,cAAc,KAClB,kBAaF,CAAC;AAWF,eAAO,MAAM,sBAAsB,GACjC,eAAc,QAAkB,KAC/B,iBAmBD,CAAC"}
|
package/dist/logging-wrapper.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { hostname } from "os";
|
|
2
|
+
import { applyFieldRules } from "./field-filter.js";
|
|
2
3
|
import { MESSAGE_KEY, REDACTED_PATHS, REDACTED_VALUE, toLoggingError, } from "./logging-wrapper-entities.js";
|
|
3
4
|
const loggingContext = {};
|
|
5
|
+
let loggingOptions = {};
|
|
6
|
+
export const setLoggingOptions = (options = {}) => {
|
|
7
|
+
loggingOptions = options;
|
|
8
|
+
};
|
|
4
9
|
export const getLoggingContext = (params) => params.includeError
|
|
5
10
|
? loggingContext
|
|
6
11
|
: { ...loggingContext, error: undefined };
|
|
@@ -14,11 +19,15 @@ export const setLoggingContext = (params) => {
|
|
|
14
19
|
if (params.error !== undefined) {
|
|
15
20
|
loggingContext.error = toLoggingError(params.error);
|
|
16
21
|
}
|
|
22
|
+
if (params.pseudoUser !== undefined) {
|
|
23
|
+
loggingContext.pseudoUser = params.pseudoUser;
|
|
24
|
+
}
|
|
17
25
|
};
|
|
18
26
|
export const resetLoggingContext = () => {
|
|
19
27
|
loggingContext.request = undefined;
|
|
20
28
|
loggingContext.response = undefined;
|
|
21
29
|
loggingContext.error = undefined;
|
|
30
|
+
loggingContext.pseudoUser = undefined;
|
|
22
31
|
};
|
|
23
32
|
export const getLoggingContextError = () => getLoggingContext({ includeError: true }).error;
|
|
24
33
|
export const getPartialLoggingContextError = () => ({
|
|
@@ -31,18 +40,26 @@ const parseLoggingRequest = (req) => ({
|
|
|
31
40
|
method: req.method,
|
|
32
41
|
path: getPathWithoutParams(req),
|
|
33
42
|
hostname: req.hostname,
|
|
34
|
-
query_params: req.query,
|
|
43
|
+
query_params: applyFieldRules(req.query, loggingOptions.request?.queryParams),
|
|
35
44
|
port: req.port,
|
|
36
45
|
});
|
|
37
|
-
export const parseFullLoggingRequest = (req) =>
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
46
|
+
export const parseFullLoggingRequest = (req) => {
|
|
47
|
+
const requestOptions = loggingOptions.request;
|
|
48
|
+
const parsed = {
|
|
49
|
+
...parseLoggingRequest(req),
|
|
50
|
+
headers: applyFieldRules(req.headers, requestOptions?.headers, true),
|
|
51
|
+
};
|
|
52
|
+
if (requestOptions?.clientIp !== "omit") {
|
|
53
|
+
parsed.client_ip = req.ip;
|
|
54
|
+
}
|
|
55
|
+
if (requestOptions?.userAgent !== "omit") {
|
|
56
|
+
parsed.user_agent = req.headers["user-agent"] ?? undefined;
|
|
57
|
+
}
|
|
58
|
+
return parsed;
|
|
59
|
+
};
|
|
43
60
|
const parseLoggingResponse = (res) => ({
|
|
44
61
|
status_code: res.statusCode,
|
|
45
|
-
headers: res.getHeaders(),
|
|
62
|
+
headers: applyFieldRules(res.getHeaders(), loggingOptions.response?.headers, true),
|
|
46
63
|
});
|
|
47
64
|
export const getLoggerConfiguration = (minimumLevel = "debug") => ({
|
|
48
65
|
base: { hostname: hostname() },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logging-wrapper.js","sourceRoot":"","sources":["../src/logging-wrapper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"logging-wrapper.js","sourceRoot":"","sources":["../src/logging-wrapper.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAuB,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAOL,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,GACf,MAAM,+BAA+B,CAAC;A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
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
2
|
+
"name": "@ogcio/fastify-logging-wrapper",
|
|
3
|
+
"version": "5.4.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [],
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Samuele Salvatico",
|
|
10
|
+
"email": "samuele.salvatico@nearform.com"
|
|
11
|
+
},
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"description": "Enable standardized log entries for each request in fastify",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@fastify/error": "^4.2.0",
|
|
16
|
+
"@fastify/sensible": "^6.0.4",
|
|
17
|
+
"hyperid": "^3.3.0",
|
|
18
|
+
"pino": "^10.3.1"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"fastify": "^5.8.4",
|
|
22
|
+
"@ogcio/shared-errors": "^1.1.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@ogcio/shared-errors": "^1.1.1",
|
|
26
|
+
"@types/http-errors": "^2.0.5",
|
|
27
|
+
"fastify": "^5.8.4"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/ogcio/shared-node-utils.git",
|
|
32
|
+
"directory": "packages/fastify-logging-wrapper"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/ogcio/shared-node-utils/tree/main/packages/fastify-logging-wrapper#readme",
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "rm -rf dist tsconfig.prod.tsbuildinfo tsconfig.tsbuildinfo && tsc -p tsconfig.prod.json",
|
|
37
|
+
"test": "vitest run --coverage --outputFile=results.xml"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { REQUEST_ID_HEADER } from "@ogcio/shared-errors";
|
|
2
2
|
import type {
|
|
3
|
+
FastifyError,
|
|
3
4
|
FastifyInstance,
|
|
5
|
+
FastifyReply,
|
|
6
|
+
FastifyRequest,
|
|
4
7
|
FastifyServerOptions,
|
|
5
8
|
RawServerBase,
|
|
6
9
|
} from "fastify";
|
|
@@ -8,20 +11,24 @@ import type {
|
|
|
8
11
|
FastifyLoggerOptions,
|
|
9
12
|
PinoLoggerOptions,
|
|
10
13
|
} from "fastify/types/logger.js";
|
|
14
|
+
import type { HttpError } from "http-errors";
|
|
11
15
|
import hyperid from "hyperid";
|
|
12
16
|
import { type DestinationStream, type LoggerOptions, pino } from "pino";
|
|
13
|
-
import {
|
|
14
|
-
LogMessages,
|
|
15
|
-
REQUEST_ID_LOG_LABEL,
|
|
16
|
-
} from "./logging-wrapper-entities.js";
|
|
17
|
+
import type { LoggingOptions } from "./field-filter.js";
|
|
17
18
|
import {
|
|
18
19
|
getLoggerConfiguration,
|
|
20
|
+
getLoggingContext,
|
|
19
21
|
getLoggingContextError,
|
|
20
22
|
getPartialLoggingContextError,
|
|
21
23
|
parseFullLoggingRequest,
|
|
22
24
|
resetLoggingContext,
|
|
23
25
|
setLoggingContext,
|
|
26
|
+
setLoggingOptions,
|
|
24
27
|
} from "./logging-wrapper.js";
|
|
28
|
+
import {
|
|
29
|
+
LogMessages,
|
|
30
|
+
REQUEST_ID_LOG_LABEL,
|
|
31
|
+
} from "./logging-wrapper-entities.js";
|
|
25
32
|
|
|
26
33
|
const hyperidInstance = hyperid({ fixedLength: true, urlSafe: true });
|
|
27
34
|
|
|
@@ -38,7 +45,11 @@ const isObjectNotEmpty = (value: object | undefined) => {
|
|
|
38
45
|
);
|
|
39
46
|
};
|
|
40
47
|
|
|
41
|
-
export const initializeLoggingHooks = (
|
|
48
|
+
export const initializeLoggingHooks = (
|
|
49
|
+
server: FastifyInstance,
|
|
50
|
+
options?: LoggingOptions,
|
|
51
|
+
): void => {
|
|
52
|
+
setLoggingOptions(options);
|
|
42
53
|
server.addHook("preParsing", (request, _reply, payload, done) => {
|
|
43
54
|
setLoggingContext({ request });
|
|
44
55
|
|
|
@@ -53,8 +64,14 @@ export const initializeLoggingHooks = (server: FastifyInstance): void => {
|
|
|
53
64
|
done(null, payload);
|
|
54
65
|
});
|
|
55
66
|
|
|
56
|
-
server.addHook("onResponse", (
|
|
57
|
-
|
|
67
|
+
server.addHook("onResponse", (req, reply, done) => {
|
|
68
|
+
const contextParams = addPseudoUserToLoggingContextParams({
|
|
69
|
+
request: req,
|
|
70
|
+
reply,
|
|
71
|
+
setRequestInContext: false,
|
|
72
|
+
});
|
|
73
|
+
setLoggingContext(contextParams);
|
|
74
|
+
|
|
58
75
|
reply.log.info(LogMessages.Response);
|
|
59
76
|
// Include error in API Track if exists
|
|
60
77
|
|
|
@@ -70,7 +87,12 @@ export const initializeLoggingHooks = (server: FastifyInstance): void => {
|
|
|
70
87
|
});
|
|
71
88
|
|
|
72
89
|
server.addHook("onError", (request, _reply, error, done) => {
|
|
73
|
-
|
|
90
|
+
const contextParams = addPseudoUserToLoggingContextParams({
|
|
91
|
+
request,
|
|
92
|
+
error,
|
|
93
|
+
setRequestInContext: false,
|
|
94
|
+
});
|
|
95
|
+
setLoggingContext(contextParams);
|
|
74
96
|
|
|
75
97
|
request.log.error({ error: getLoggingContextError() }, LogMessages.Error);
|
|
76
98
|
|
|
@@ -122,3 +144,64 @@ export const getLoggingConfiguration = (
|
|
|
122
144
|
requestIdHeader: REQUEST_ID_HEADER,
|
|
123
145
|
};
|
|
124
146
|
};
|
|
147
|
+
|
|
148
|
+
const getPseudoUser = (
|
|
149
|
+
request: FastifyRequest,
|
|
150
|
+
): { id: string; version: string } | undefined => {
|
|
151
|
+
const userData =
|
|
152
|
+
"userData" in request && request.userData ? request.userData : undefined;
|
|
153
|
+
if (!userData || typeof userData !== "object") {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const pseudoUserData =
|
|
158
|
+
"pseudoUser" in userData && userData.pseudoUser
|
|
159
|
+
? userData.pseudoUser
|
|
160
|
+
: undefined;
|
|
161
|
+
if (!pseudoUserData || typeof pseudoUserData !== "object") {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!("id" in pseudoUserData) || typeof pseudoUserData.id !== "string") {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (
|
|
170
|
+
!("version" in pseudoUserData) ||
|
|
171
|
+
typeof pseudoUserData.version !== "string"
|
|
172
|
+
) {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
id: pseudoUserData.id,
|
|
178
|
+
version: pseudoUserData.version,
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const addPseudoUserToLoggingContextParams = (params: {
|
|
183
|
+
request: FastifyRequest;
|
|
184
|
+
reply?: FastifyReply;
|
|
185
|
+
error?: FastifyError | HttpError<number>;
|
|
186
|
+
setRequestInContext: boolean;
|
|
187
|
+
}) => {
|
|
188
|
+
const loggingContextParams: Parameters<typeof setLoggingContext>[0] = {};
|
|
189
|
+
if (params.request && params.setRequestInContext) {
|
|
190
|
+
loggingContextParams.request = params.request;
|
|
191
|
+
}
|
|
192
|
+
if (params.reply) {
|
|
193
|
+
loggingContextParams.response = params.reply;
|
|
194
|
+
}
|
|
195
|
+
if (params.error) {
|
|
196
|
+
loggingContextParams.error = params.error;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (!getLoggingContext({ includeError: false }).pseudoUser) {
|
|
200
|
+
const pseudoUser = getPseudoUser(params.request);
|
|
201
|
+
if (pseudoUser) {
|
|
202
|
+
loggingContextParams.pseudoUser = pseudoUser;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return loggingContextParams;
|
|
207
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { REDACTED_VALUE } from "./logging-wrapper-entities.js";
|
|
2
|
+
|
|
3
|
+
export interface FieldRules {
|
|
4
|
+
omit?: string[];
|
|
5
|
+
mask?: string[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface LoggingOptions {
|
|
9
|
+
request?: {
|
|
10
|
+
headers?: FieldRules;
|
|
11
|
+
queryParams?: FieldRules;
|
|
12
|
+
clientIp?: "omit";
|
|
13
|
+
userAgent?: "omit";
|
|
14
|
+
};
|
|
15
|
+
response?: {
|
|
16
|
+
headers?: FieldRules;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const applyFieldRules = (
|
|
21
|
+
value: unknown,
|
|
22
|
+
rules: FieldRules | undefined,
|
|
23
|
+
caseInsensitive = false,
|
|
24
|
+
): unknown => {
|
|
25
|
+
if (!rules || (!rules.omit?.length && !rules.mask?.length)) {
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const normalize = (key: string): string =>
|
|
33
|
+
caseInsensitive ? key.toLowerCase() : key;
|
|
34
|
+
const toOmit = new Set((rules.omit ?? []).map(normalize));
|
|
35
|
+
const toMask = new Set((rules.mask ?? []).map(normalize));
|
|
36
|
+
|
|
37
|
+
const result: Record<string, unknown> = {};
|
|
38
|
+
for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {
|
|
39
|
+
const normalized = normalize(key);
|
|
40
|
+
if (toOmit.has(normalized)) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
result[key] = toMask.has(normalized) ? REDACTED_VALUE : entry;
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -2,13 +2,14 @@ export {
|
|
|
2
2
|
getLoggingConfiguration,
|
|
3
3
|
initializeLoggingHooks,
|
|
4
4
|
} from "./fastify-logging-wrapper.js";
|
|
5
|
+
export type { FieldRules, LoggingOptions } from "./field-filter.js";
|
|
5
6
|
export {
|
|
6
|
-
|
|
7
|
-
LogMessages,
|
|
8
|
-
LoggingError,
|
|
9
|
-
} from "./logging-wrapper-entities.js";
|
|
10
|
-
|
|
11
|
-
export {
|
|
7
|
+
getLoggingContext,
|
|
12
8
|
getLoggingContextError,
|
|
13
9
|
setLoggingContext,
|
|
14
10
|
} from "./logging-wrapper.js";
|
|
11
|
+
export {
|
|
12
|
+
LoggingError,
|
|
13
|
+
LogMessages,
|
|
14
|
+
toLoggingError,
|
|
15
|
+
} from "./logging-wrapper-entities.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { FastifyError } from "fastify";
|
|
2
|
-
import { parseErrorForLogging } from "@ogcio/shared-errors";
|
|
3
1
|
import type { HttpError } from "@fastify/sensible";
|
|
2
|
+
import { parseErrorForLogging } from "@ogcio/shared-errors";
|
|
3
|
+
import type { FastifyError } from "fastify";
|
|
4
4
|
import { isHttpError } from "http-errors";
|
|
5
5
|
|
|
6
6
|
export interface LoggingRequest {
|
|
@@ -9,14 +9,14 @@ export interface LoggingRequest {
|
|
|
9
9
|
path: string | undefined;
|
|
10
10
|
hostname: string;
|
|
11
11
|
query_params: unknown;
|
|
12
|
-
port: number;
|
|
12
|
+
port: number | null;
|
|
13
13
|
[key: string]: unknown;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export interface FullLoggingRequest extends LoggingRequest {
|
|
17
17
|
headers: unknown;
|
|
18
|
-
user_agent
|
|
19
|
-
client_ip
|
|
18
|
+
user_agent?: string;
|
|
19
|
+
client_ip?: string;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export interface LoggingResponse {
|
|
@@ -33,10 +33,16 @@ export interface LoggingError {
|
|
|
33
33
|
[key: string]: unknown;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
export interface LoggingPseudoUser {
|
|
37
|
+
id: string;
|
|
38
|
+
version: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
36
41
|
export interface LoggingContext {
|
|
37
42
|
request?: LoggingRequest;
|
|
38
43
|
response?: LoggingResponse;
|
|
39
44
|
error?: LoggingError;
|
|
45
|
+
pseudoUser?: LoggingPseudoUser;
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
export enum LogMessages {
|