@ogcio/fastify-logging-wrapper 5.3.1 → 5.4.0
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/__tests__/fastify-logging-wrapper.errors.test.ts +2 -2
- package/__tests__/fastify-logging-wrapper.test.ts +85 -3
- package/__tests__/helpers/build-fastify.ts +14 -0
- package/__tests__/helpers/fastify-test-helpers.ts +2 -3
- package/__tests__/logging-wrapper.test.ts +8 -7
- package/dist/fastify-logging-wrapper.d.ts.map +1 -1
- package/dist/fastify-logging-wrapper.js +56 -4
- package/dist/fastify-logging-wrapper.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/logging-wrapper-entities.d.ts +7 -2
- 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 +2 -1
- package/dist/logging-wrapper.d.ts.map +1 -1
- package/dist/logging-wrapper.js +4 -0
- package/dist/logging-wrapper.js.map +1 -1
- package/package.json +35 -33
- package/src/fastify-logging-wrapper.ts +84 -7
- package/src/index.ts +5 -6
- package/src/logging-wrapper-entities.ts +9 -3
- package/src/logging-wrapper.ts +7 -1
- package/tsconfig.json +9 -11
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { httpErrors } from "@fastify/sensible";
|
|
2
|
-
import {
|
|
2
|
+
import { afterEach, assert, describe, it } from "vitest";
|
|
3
3
|
import { LogErrorClasses } from "../src/logging-wrapper-entities.js";
|
|
4
4
|
import {
|
|
5
|
-
DEFAULT_METHOD,
|
|
6
5
|
checkExpectedApiTrackEntry,
|
|
7
6
|
checkExpectedErrorEntry,
|
|
8
7
|
checkExpectedRequestEntry,
|
|
9
8
|
checkExpectedResponseEntry,
|
|
9
|
+
DEFAULT_METHOD,
|
|
10
10
|
initializeServer,
|
|
11
11
|
parseLogEntry,
|
|
12
12
|
runErrorTest,
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { REQUEST_ID_HEADER } from "@ogcio/shared-errors";
|
|
2
|
-
import {
|
|
2
|
+
import { afterEach, assert, describe, it } from "vitest";
|
|
3
3
|
import { LogMessages } from "../src/logging-wrapper-entities.js";
|
|
4
4
|
import {
|
|
5
|
-
DEFAULT_METHOD,
|
|
6
|
-
DEFAULT_PATH,
|
|
7
5
|
checkExpectedRequestEntry,
|
|
8
6
|
checkExpectedResponseEntry,
|
|
9
7
|
checkGenericEntryFields,
|
|
8
|
+
DEFAULT_METHOD,
|
|
9
|
+
DEFAULT_PATH,
|
|
10
10
|
initializeServer,
|
|
11
11
|
parseLogEntry,
|
|
12
12
|
} from "./helpers/fastify-test-helpers.js";
|
|
@@ -121,3 +121,85 @@ describe("Additional logs are correctly written", () => {
|
|
|
121
121
|
assert.ok(typeof parsedAdditional.request !== "undefined");
|
|
122
122
|
});
|
|
123
123
|
});
|
|
124
|
+
|
|
125
|
+
describe("PseudoUser is included in logging context", () => {
|
|
126
|
+
it("should include pseudoUser in response and apiTrack entries", async () => {
|
|
127
|
+
const { server, loggingDestination } = initializeServer();
|
|
128
|
+
afterEach(() => server.close());
|
|
129
|
+
|
|
130
|
+
const response = await server.inject({
|
|
131
|
+
method: DEFAULT_METHOD,
|
|
132
|
+
url: "/with-pseudo-user",
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
assert.ok(typeof response !== "undefined");
|
|
136
|
+
assert.equal(response?.statusCode, 200);
|
|
137
|
+
const loggedRecords = loggingDestination.getLoggedRecords();
|
|
138
|
+
assert.equal(loggedRecords.length, 3);
|
|
139
|
+
|
|
140
|
+
// Request entry should NOT have pseudoUser (not yet set)
|
|
141
|
+
const requestEntry = parseLogEntry(loggedRecords[0]);
|
|
142
|
+
assert.ok(typeof requestEntry.pseudoUser === "undefined");
|
|
143
|
+
|
|
144
|
+
// Response entry should have pseudoUser
|
|
145
|
+
const responseEntry = parseLogEntry(loggedRecords[1]);
|
|
146
|
+
assert.ok(typeof responseEntry.pseudoUser !== "undefined");
|
|
147
|
+
assert.equal(responseEntry.pseudoUser.id, "pseudo-user-id");
|
|
148
|
+
assert.equal(responseEntry.pseudoUser.version, "v1");
|
|
149
|
+
|
|
150
|
+
// ApiTrack entry should have pseudoUser
|
|
151
|
+
const apiTrackEntry = parseLogEntry(loggedRecords[2]);
|
|
152
|
+
assert.ok(typeof apiTrackEntry.pseudoUser !== "undefined");
|
|
153
|
+
assert.equal(apiTrackEntry.pseudoUser.id, "pseudo-user-id");
|
|
154
|
+
assert.equal(apiTrackEntry.pseudoUser.version, "v1");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("should not include pseudoUser when userData has invalid shape", async () => {
|
|
158
|
+
const { server, loggingDestination } = initializeServer();
|
|
159
|
+
afterEach(() => server.close());
|
|
160
|
+
|
|
161
|
+
const response = await server.inject({
|
|
162
|
+
method: DEFAULT_METHOD,
|
|
163
|
+
url: "/with-invalid-pseudo-user",
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
assert.ok(typeof response !== "undefined");
|
|
167
|
+
assert.equal(response?.statusCode, 200);
|
|
168
|
+
const loggedRecords = loggingDestination.getLoggedRecords();
|
|
169
|
+
assert.equal(loggedRecords.length, 3);
|
|
170
|
+
|
|
171
|
+
// pseudoUser should not be set when shape is invalid
|
|
172
|
+
const responseEntry = parseLogEntry(loggedRecords[1]);
|
|
173
|
+
assert.ok(typeof responseEntry.pseudoUser === "undefined");
|
|
174
|
+
|
|
175
|
+
const apiTrackEntry = parseLogEntry(loggedRecords[2]);
|
|
176
|
+
assert.ok(typeof apiTrackEntry.pseudoUser === "undefined");
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("should reset pseudoUser between requests", async () => {
|
|
180
|
+
const { server, loggingDestination } = initializeServer();
|
|
181
|
+
afterEach(() => server.close());
|
|
182
|
+
|
|
183
|
+
// First request with pseudoUser
|
|
184
|
+
await server.inject({
|
|
185
|
+
method: DEFAULT_METHOD,
|
|
186
|
+
url: "/with-pseudo-user",
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// Second request without pseudoUser
|
|
190
|
+
await server.inject({
|
|
191
|
+
method: DEFAULT_METHOD,
|
|
192
|
+
url: DEFAULT_PATH,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const loggedRecords = loggingDestination.getLoggedRecords();
|
|
196
|
+
assert.equal(loggedRecords.length, 6);
|
|
197
|
+
|
|
198
|
+
// Second request's response should NOT have pseudoUser
|
|
199
|
+
const secondResponseEntry = parseLogEntry(loggedRecords[4]);
|
|
200
|
+
assert.ok(typeof secondResponseEntry.pseudoUser === "undefined");
|
|
201
|
+
|
|
202
|
+
const secondApiTrackEntry = parseLogEntry(loggedRecords[5]);
|
|
203
|
+
assert.ok(typeof secondApiTrackEntry.pseudoUser === "undefined");
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -52,5 +52,19 @@ export const buildFastify = (loggerDestination?: DestinationStream) => {
|
|
|
52
52
|
return { data: { message: logMessage } };
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
+
server.get("/with-pseudo-user", async (request, _reply) => {
|
|
56
|
+
(request as unknown as { userData: unknown }).userData = {
|
|
57
|
+
pseudoUser: { id: "pseudo-user-id", version: "v1" },
|
|
58
|
+
};
|
|
59
|
+
return { data: "ok" };
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
server.get("/with-invalid-pseudo-user", async (request, _reply) => {
|
|
63
|
+
(request as unknown as { userData: unknown }).userData = {
|
|
64
|
+
pseudoUser: { id: 123 },
|
|
65
|
+
};
|
|
66
|
+
return { data: "ok" };
|
|
67
|
+
});
|
|
68
|
+
|
|
55
69
|
return server;
|
|
56
70
|
};
|
|
@@ -6,8 +6,8 @@ import {
|
|
|
6
6
|
} from "../../src/logging-wrapper-entities.js";
|
|
7
7
|
import { buildFastify } from "./build-fastify.js";
|
|
8
8
|
import {
|
|
9
|
-
type TestingLoggerDestination,
|
|
10
9
|
getTestingDestinationLogger,
|
|
10
|
+
type TestingLoggerDestination,
|
|
11
11
|
} from "./build-logger.js";
|
|
12
12
|
|
|
13
13
|
export const DEFAULT_HOSTNAME = "localhost";
|
|
@@ -33,8 +33,7 @@ export const initializeServer = (): {
|
|
|
33
33
|
return { server, loggingDestination };
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
-
// biome-ignore lint/suspicious/noExplicitAny:
|
|
37
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
36
|
+
// biome-ignore lint/suspicious/noExplicitAny: Needed for testing purposes
|
|
38
37
|
export const parseLogEntry = (logEntry: string): { [x: string]: any } =>
|
|
39
38
|
JSON.parse(logEntry);
|
|
40
39
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { LogLevel } from "fastify";
|
|
1
2
|
import { hostname } from "os";
|
|
2
3
|
import { assert, describe, it } from "vitest";
|
|
3
|
-
import { REDACTED_VALUE } from "../src/logging-wrapper-entities.js";
|
|
4
4
|
import { getLoggerConfiguration } from "../src/logging-wrapper.js";
|
|
5
|
+
import { REDACTED_VALUE } from "../src/logging-wrapper-entities.js";
|
|
5
6
|
import { buildLogger } from "./helpers/build-logger.js";
|
|
6
7
|
|
|
7
8
|
const getRandomFieldValue = () => Math.random().toString(36).slice(2);
|
|
@@ -59,7 +60,10 @@ const toRedactFields = {
|
|
|
59
60
|
},
|
|
60
61
|
};
|
|
61
62
|
|
|
62
|
-
const methodsDataProvider
|
|
63
|
+
const methodsDataProvider: {
|
|
64
|
+
method: LogLevel;
|
|
65
|
+
expected: { level: number; level_name: string };
|
|
66
|
+
}[] = [
|
|
63
67
|
{
|
|
64
68
|
method: "trace",
|
|
65
69
|
expected: {
|
|
@@ -121,7 +125,7 @@ describe("Basic format is the expected one", () => {
|
|
|
121
125
|
parsed.timestamp > Date.now() - 2000,
|
|
122
126
|
"the timestamp must be newer than 2 seconds ago",
|
|
123
127
|
);
|
|
124
|
-
|
|
128
|
+
|
|
125
129
|
delete parsed.timestamp;
|
|
126
130
|
assert.deepStrictEqual(parsed, {
|
|
127
131
|
level: 20,
|
|
@@ -141,13 +145,10 @@ describe("Fields are redacted as expected", () => {
|
|
|
141
145
|
|
|
142
146
|
const loggedRecords = loggedRecordsMethod();
|
|
143
147
|
const parsed = JSON.parse(loggedRecords[0]);
|
|
144
|
-
|
|
148
|
+
|
|
145
149
|
delete parsed.hostname;
|
|
146
|
-
// biome-ignore lint/performance/noDelete: Would change behaviour of the test
|
|
147
150
|
delete parsed.level;
|
|
148
|
-
// biome-ignore lint/performance/noDelete: Would change behaviour of the test
|
|
149
151
|
delete parsed.level_name;
|
|
150
|
-
// biome-ignore lint/performance/noDelete: Would change behaviour of the test
|
|
151
152
|
delete parsed.timestamp;
|
|
152
153
|
|
|
153
154
|
assert.deepStrictEqual(parsed, toRedactFields.expected_output);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fastify-logging-wrapper.d.ts","sourceRoot":"","sources":["../src/fastify-logging-wrapper.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"fastify-logging-wrapper.d.ts","sourceRoot":"","sources":["../src/fastify-logging-wrapper.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEV,eAAe,EAGf,oBAAoB,EACpB,aAAa,EACd,MAAM,SAAS,CAAC;AACjB,OAAO,KAAK,EACV,oBAAoB,EACpB,iBAAiB,EAClB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EAAE,KAAK,iBAAiB,EAA4B,MAAM,MAAM,CAAC;AA8BxE,eAAO,MAAM,sBAAsB,GAAI,QAAQ,eAAe,KAAG,IAiDhE,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAClC,eACI;IACE,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,uBAAuB,CAAC,EAAE,KAAK,CAAC;CACjC,GACD;IACE,WAAW,CAAC,EAAE,KAAK,CAAC;IACpB,iBAAiB,CAAC,EAAE,KAAK,CAAC;IAC1B,uBAAuB,CAAC,EAAE,oBAAoB,CAAC,aAAa,CAAC,GAC3D,iBAAiB,CAAC;CACrB,KACJ,oBA8BF,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { REQUEST_ID_HEADER } from "@ogcio/shared-errors";
|
|
2
2
|
import hyperid from "hyperid";
|
|
3
3
|
import { pino } from "pino";
|
|
4
|
+
import { getLoggerConfiguration, getLoggingContext, getLoggingContextError, getPartialLoggingContextError, parseFullLoggingRequest, resetLoggingContext, setLoggingContext, } from "./logging-wrapper.js";
|
|
4
5
|
import { LogMessages, REQUEST_ID_LOG_LABEL, } from "./logging-wrapper-entities.js";
|
|
5
|
-
import { getLoggerConfiguration, getLoggingContextError, getPartialLoggingContextError, parseFullLoggingRequest, resetLoggingContext, setLoggingContext, } from "./logging-wrapper.js";
|
|
6
6
|
const hyperidInstance = hyperid({ fixedLength: true, urlSafe: true });
|
|
7
7
|
const isObjectNotEmpty = (value) => {
|
|
8
8
|
return (value &&
|
|
@@ -22,8 +22,13 @@ export const initializeLoggingHooks = (server) => {
|
|
|
22
22
|
}
|
|
23
23
|
done(null, payload);
|
|
24
24
|
});
|
|
25
|
-
server.addHook("onResponse", (
|
|
26
|
-
|
|
25
|
+
server.addHook("onResponse", (req, reply, done) => {
|
|
26
|
+
const contextParams = addPseudoUserToLoggingContextParams({
|
|
27
|
+
request: req,
|
|
28
|
+
reply,
|
|
29
|
+
setRequestInContext: false,
|
|
30
|
+
});
|
|
31
|
+
setLoggingContext(contextParams);
|
|
27
32
|
reply.log.info(LogMessages.Response);
|
|
28
33
|
// Include error in API Track if exists
|
|
29
34
|
const error = getPartialLoggingContextError();
|
|
@@ -37,7 +42,12 @@ export const initializeLoggingHooks = (server) => {
|
|
|
37
42
|
done();
|
|
38
43
|
});
|
|
39
44
|
server.addHook("onError", (request, _reply, error, done) => {
|
|
40
|
-
|
|
45
|
+
const contextParams = addPseudoUserToLoggingContextParams({
|
|
46
|
+
request,
|
|
47
|
+
error,
|
|
48
|
+
setRequestInContext: false,
|
|
49
|
+
});
|
|
50
|
+
setLoggingContext(contextParams);
|
|
41
51
|
request.log.error({ error: getLoggingContextError() }, LogMessages.Error);
|
|
42
52
|
done();
|
|
43
53
|
});
|
|
@@ -69,4 +79,46 @@ export const getLoggingConfiguration = (customConfig) => {
|
|
|
69
79
|
requestIdHeader: REQUEST_ID_HEADER,
|
|
70
80
|
};
|
|
71
81
|
};
|
|
82
|
+
const getPseudoUser = (request) => {
|
|
83
|
+
const userData = "userData" in request && request.userData ? request.userData : undefined;
|
|
84
|
+
if (!userData || typeof userData !== "object") {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
const pseudoUserData = "pseudoUser" in userData && userData.pseudoUser
|
|
88
|
+
? userData.pseudoUser
|
|
89
|
+
: undefined;
|
|
90
|
+
if (!pseudoUserData || typeof pseudoUserData !== "object") {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
if (!("id" in pseudoUserData) || typeof pseudoUserData.id !== "string") {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
if (!("version" in pseudoUserData) ||
|
|
97
|
+
typeof pseudoUserData.version !== "string") {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
id: pseudoUserData.id,
|
|
102
|
+
version: pseudoUserData.version,
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const addPseudoUserToLoggingContextParams = (params) => {
|
|
106
|
+
const loggingContextParams = {};
|
|
107
|
+
if (params.request && params.setRequestInContext) {
|
|
108
|
+
loggingContextParams.request = params.request;
|
|
109
|
+
}
|
|
110
|
+
if (params.reply) {
|
|
111
|
+
loggingContextParams.response = params.reply;
|
|
112
|
+
}
|
|
113
|
+
if (params.error) {
|
|
114
|
+
loggingContextParams.error = params.error;
|
|
115
|
+
}
|
|
116
|
+
if (!getLoggingContext({ includeError: false }).pseudoUser) {
|
|
117
|
+
const pseudoUser = getPseudoUser(params.request);
|
|
118
|
+
if (pseudoUser) {
|
|
119
|
+
loggingContextParams.pseudoUser = pseudoUser;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return loggingContextParams;
|
|
123
|
+
};
|
|
72
124
|
//# sourceMappingURL=fastify-logging-wrapper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fastify-logging-wrapper.js","sourceRoot":"","sources":["../src/fastify-logging-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"fastify-logging-wrapper.js","sourceRoot":"","sources":["../src/fastify-logging-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAczD,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAA8C,IAAI,EAAE,MAAM,MAAM,CAAC;AACxE,OAAO,EACL,sBAAsB,EACtB,iBAAiB,EACjB,sBAAsB,EACtB,6BAA6B,EAC7B,uBAAuB,EACvB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,WAAW,EACX,oBAAoB,GACrB,MAAM,+BAA+B,CAAC;AAEvC,MAAM,eAAe,GAAG,OAAO,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;AAEtE,MAAM,gBAAgB,GAAG,CAAC,KAAyB,EAAE,EAAE;IACrD,OAAO,CACL,KAAK;QACL,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACrB,MAAM,CAAC,IAAI,CACT,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,C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B,GAA4C,EAAE,CAAC;IACzE,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,mBAAmB,EAAE,CAAC;QACjD,oBAAoB,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IAChD,CAAC;IACD,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QACjB,oBAAoB,CAAC,QAAQ,GAAG,MAAM,CAAC,KAAK,CAAC;IAC/C,CAAC;IACD,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QACjB,oBAAoB,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC,iBAAiB,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,CAAC,UAAU,EAAE,CAAC;QAC3D,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACjD,IAAI,UAAU,EAAE,CAAC;YACf,oBAAoB,CAAC,UAAU,GAAG,UAAU,CAAC;QAC/C,CAAC;IACH,CAAC;IAED,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { getLoggingConfiguration, initializeLoggingHooks, } from "./fastify-logging-wrapper.js";
|
|
2
|
-
export { toLoggingError, LogMessages, LoggingError, } from "./logging-wrapper-entities.js";
|
|
3
2
|
export { getLoggingContextError, setLoggingContext, } from "./logging-wrapper.js";
|
|
3
|
+
export { LoggingError, LogMessages, toLoggingError, } from "./logging-wrapper-entities.js";
|
|
4
4
|
//# 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,OAAO,EACL,
|
|
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,OAAO,EACL,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 { toLoggingError, LogMessages, } from "./logging-wrapper-entities.js";
|
|
3
2
|
export { 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;AACtC,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAEL,WAAW,EACX,cAAc,GACf,MAAM,+BAA+B,CAAC"}
|
|
@@ -1,12 +1,12 @@
|
|
|
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 {
|
|
@@ -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,C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Y,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;AACxB,CAAC,EALW,WAAW,KAAX,WAAW,QAKtB;AAED,MAAM,CAAN,IAAY,eAMX;AAND,WAAY,eAAe;IACzB,+CAA4B,CAAA;IAC5B,uDAAoC,CAAA;IACpC,iDAA8B,CAAA;IAC9B,iDAA8B,CAAA;IAC9B,iDAA8B,CAAA;AAChC,CAAC,EANW,eAAe,KAAf,eAAe,QAM1B;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,YAAY,CAAC;AAE3C,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,mCAAmC;IACnC,wBAAwB;IACxB,4BAA4B;IAC5B,qBAAqB;IACrB,yBAAyB;IACzB,kCAAkC;CACnC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,SAAS,CAAC;AAErC,MAAM,CAAC,MAAM,oBAAoB,GAAG,YAAY,CAAC;AAEjD,MAAM,wBAAwB,GAAG,qBAAqB,CAAC;AAEvD,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,KAA+B,EACjB,EAAE;IAChB,MAAM,MAAM,GAAG;QACb,KAAK,EAAE,eAAe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|
|
@@ -1,7 +1,7 @@
|
|
|
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 FullLoggingRequest, type LoggingContext, type LoggingError } from "./logging-wrapper-entities.js";
|
|
4
|
+
import { type FullLoggingRequest, type LoggingContext, type LoggingError, type LoggingPseudoUser } from "./logging-wrapper-entities.js";
|
|
5
5
|
type INPUT_ERROR_TYPES = FastifyError | HttpError;
|
|
6
6
|
export declare const getLoggingContext: (params: {
|
|
7
7
|
includeError: boolean;
|
|
@@ -10,6 +10,7 @@ export declare const setLoggingContext: (params: {
|
|
|
10
10
|
request?: FastifyRequest;
|
|
11
11
|
response?: FastifyReply;
|
|
12
12
|
error?: INPUT_ERROR_TYPES;
|
|
13
|
+
pseudoUser?: LoggingPseudoUser;
|
|
13
14
|
}) => void;
|
|
14
15
|
export declare const resetLoggingContext: () => void;
|
|
15
16
|
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,EACL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EAOvB,MAAM,+BAA+B,CAAC;AAIvC,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,kBAKD,CAAC;AAOH,eAAO,MAAM,sBAAsB,GACjC,eAAc,QAAkB,KAC/B,iBAmBD,CAAC"}
|
package/dist/logging-wrapper.js
CHANGED
|
@@ -14,11 +14,15 @@ export const setLoggingContext = (params) => {
|
|
|
14
14
|
if (params.error !== undefined) {
|
|
15
15
|
loggingContext.error = toLoggingError(params.error);
|
|
16
16
|
}
|
|
17
|
+
if (params.pseudoUser !== undefined) {
|
|
18
|
+
loggingContext.pseudoUser = params.pseudoUser;
|
|
19
|
+
}
|
|
17
20
|
};
|
|
18
21
|
export const resetLoggingContext = () => {
|
|
19
22
|
loggingContext.request = undefined;
|
|
20
23
|
loggingContext.response = undefined;
|
|
21
24
|
loggingContext.error = undefined;
|
|
25
|
+
loggingContext.pseudoUser = undefined;
|
|
22
26
|
};
|
|
23
27
|
export const getLoggingContextError = () => getLoggingContext({ includeError: true }).error;
|
|
24
28
|
export const getPartialLoggingContextError = () => ({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logging-wrapper.js","sourceRoot":"","sources":["../src/logging-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wrapper",
|
|
3
|
+
"version": "5.4.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "rm -rf dist tsconfig.prod.tsbuildinfo tsconfig.tsbuildinfo && tsc -p tsconfig.prod.json",
|
|
9
|
+
"test": "vitest run --coverage --outputFile=results.xml",
|
|
10
|
+
"prepublishOnly": "npm i && npm run build && npm run test"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": {
|
|
14
|
+
"name": "Samuele Salvatico",
|
|
15
|
+
"email": "samuele.salvatico@nearform.com"
|
|
16
|
+
},
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"description": "Enable standardized log entries for each request in fastify",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@fastify/error": "^4.2.0",
|
|
21
|
+
"@fastify/sensible": "^6.0.4",
|
|
22
|
+
"hyperid": "^3.3.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"fastify": "^5.8.4",
|
|
26
|
+
"@ogcio/shared-errors": "^1.1.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/http-errors": "^2.0.5"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/ogcio/shared-node-utils.git",
|
|
34
|
+
"directory": "packages/fastify-logging-wrapper"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/ogcio/shared-node-utils/tree/main/packages/fastify-logging-wrapper#readme"
|
|
35
37
|
}
|
|
@@ -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,22 @@ 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
17
|
import {
|
|
18
18
|
getLoggerConfiguration,
|
|
19
|
+
getLoggingContext,
|
|
19
20
|
getLoggingContextError,
|
|
20
21
|
getPartialLoggingContextError,
|
|
21
22
|
parseFullLoggingRequest,
|
|
22
23
|
resetLoggingContext,
|
|
23
24
|
setLoggingContext,
|
|
24
25
|
} from "./logging-wrapper.js";
|
|
26
|
+
import {
|
|
27
|
+
LogMessages,
|
|
28
|
+
REQUEST_ID_LOG_LABEL,
|
|
29
|
+
} from "./logging-wrapper-entities.js";
|
|
25
30
|
|
|
26
31
|
const hyperidInstance = hyperid({ fixedLength: true, urlSafe: true });
|
|
27
32
|
|
|
@@ -53,8 +58,14 @@ export const initializeLoggingHooks = (server: FastifyInstance): void => {
|
|
|
53
58
|
done(null, payload);
|
|
54
59
|
});
|
|
55
60
|
|
|
56
|
-
server.addHook("onResponse", (
|
|
57
|
-
|
|
61
|
+
server.addHook("onResponse", (req, reply, done) => {
|
|
62
|
+
const contextParams = addPseudoUserToLoggingContextParams({
|
|
63
|
+
request: req,
|
|
64
|
+
reply,
|
|
65
|
+
setRequestInContext: false,
|
|
66
|
+
});
|
|
67
|
+
setLoggingContext(contextParams);
|
|
68
|
+
|
|
58
69
|
reply.log.info(LogMessages.Response);
|
|
59
70
|
// Include error in API Track if exists
|
|
60
71
|
|
|
@@ -70,7 +81,12 @@ export const initializeLoggingHooks = (server: FastifyInstance): void => {
|
|
|
70
81
|
});
|
|
71
82
|
|
|
72
83
|
server.addHook("onError", (request, _reply, error, done) => {
|
|
73
|
-
|
|
84
|
+
const contextParams = addPseudoUserToLoggingContextParams({
|
|
85
|
+
request,
|
|
86
|
+
error,
|
|
87
|
+
setRequestInContext: false,
|
|
88
|
+
});
|
|
89
|
+
setLoggingContext(contextParams);
|
|
74
90
|
|
|
75
91
|
request.log.error({ error: getLoggingContextError() }, LogMessages.Error);
|
|
76
92
|
|
|
@@ -122,3 +138,64 @@ export const getLoggingConfiguration = (
|
|
|
122
138
|
requestIdHeader: REQUEST_ID_HEADER,
|
|
123
139
|
};
|
|
124
140
|
};
|
|
141
|
+
|
|
142
|
+
const getPseudoUser = (
|
|
143
|
+
request: FastifyRequest,
|
|
144
|
+
): { id: string; version: string } | undefined => {
|
|
145
|
+
const userData =
|
|
146
|
+
"userData" in request && request.userData ? request.userData : undefined;
|
|
147
|
+
if (!userData || typeof userData !== "object") {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const pseudoUserData =
|
|
152
|
+
"pseudoUser" in userData && userData.pseudoUser
|
|
153
|
+
? userData.pseudoUser
|
|
154
|
+
: undefined;
|
|
155
|
+
if (!pseudoUserData || typeof pseudoUserData !== "object") {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!("id" in pseudoUserData) || typeof pseudoUserData.id !== "string") {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (
|
|
164
|
+
!("version" in pseudoUserData) ||
|
|
165
|
+
typeof pseudoUserData.version !== "string"
|
|
166
|
+
) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
id: pseudoUserData.id,
|
|
172
|
+
version: pseudoUserData.version,
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const addPseudoUserToLoggingContextParams = (params: {
|
|
177
|
+
request: FastifyRequest;
|
|
178
|
+
reply?: FastifyReply;
|
|
179
|
+
error?: FastifyError | HttpError<number>;
|
|
180
|
+
setRequestInContext: boolean;
|
|
181
|
+
}) => {
|
|
182
|
+
const loggingContextParams: Parameters<typeof setLoggingContext>[0] = {};
|
|
183
|
+
if (params.request && params.setRequestInContext) {
|
|
184
|
+
loggingContextParams.request = params.request;
|
|
185
|
+
}
|
|
186
|
+
if (params.reply) {
|
|
187
|
+
loggingContextParams.response = params.reply;
|
|
188
|
+
}
|
|
189
|
+
if (params.error) {
|
|
190
|
+
loggingContextParams.error = params.error;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (!getLoggingContext({ includeError: false }).pseudoUser) {
|
|
194
|
+
const pseudoUser = getPseudoUser(params.request);
|
|
195
|
+
if (pseudoUser) {
|
|
196
|
+
loggingContextParams.pseudoUser = pseudoUser;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return loggingContextParams;
|
|
201
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -2,13 +2,12 @@ export {
|
|
|
2
2
|
getLoggingConfiguration,
|
|
3
3
|
initializeLoggingHooks,
|
|
4
4
|
} from "./fastify-logging-wrapper.js";
|
|
5
|
-
export {
|
|
6
|
-
toLoggingError,
|
|
7
|
-
LogMessages,
|
|
8
|
-
LoggingError,
|
|
9
|
-
} from "./logging-wrapper-entities.js";
|
|
10
|
-
|
|
11
5
|
export {
|
|
12
6
|
getLoggingContextError,
|
|
13
7
|
setLoggingContext,
|
|
14
8
|
} from "./logging-wrapper.js";
|
|
9
|
+
export {
|
|
10
|
+
LoggingError,
|
|
11
|
+
LogMessages,
|
|
12
|
+
toLoggingError,
|
|
13
|
+
} 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,7 +9,7 @@ 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
|
|
|
@@ -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 {
|
package/src/logging-wrapper.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { hostname } from "os";
|
|
2
1
|
import type { HttpError } from "@fastify/sensible";
|
|
3
2
|
import type { FastifyError, FastifyReply, FastifyRequest } from "fastify";
|
|
4
3
|
import type { LogLevel, PinoLoggerOptions } from "fastify/types/logger.js";
|
|
4
|
+
import { hostname } from "os";
|
|
5
5
|
import {
|
|
6
6
|
type FullLoggingRequest,
|
|
7
7
|
type LoggingContext,
|
|
8
8
|
type LoggingError,
|
|
9
|
+
type LoggingPseudoUser,
|
|
9
10
|
type LoggingRequest,
|
|
10
11
|
type LoggingResponse,
|
|
11
12
|
MESSAGE_KEY,
|
|
@@ -29,6 +30,7 @@ export const setLoggingContext = (params: {
|
|
|
29
30
|
request?: FastifyRequest;
|
|
30
31
|
response?: FastifyReply;
|
|
31
32
|
error?: INPUT_ERROR_TYPES;
|
|
33
|
+
pseudoUser?: LoggingPseudoUser;
|
|
32
34
|
}): void => {
|
|
33
35
|
if (params.request !== undefined) {
|
|
34
36
|
loggingContext.request = parseLoggingRequest(params.request);
|
|
@@ -39,12 +41,16 @@ export const setLoggingContext = (params: {
|
|
|
39
41
|
if (params.error !== undefined) {
|
|
40
42
|
loggingContext.error = toLoggingError(params.error);
|
|
41
43
|
}
|
|
44
|
+
if (params.pseudoUser !== undefined) {
|
|
45
|
+
loggingContext.pseudoUser = params.pseudoUser;
|
|
46
|
+
}
|
|
42
47
|
};
|
|
43
48
|
|
|
44
49
|
export const resetLoggingContext = (): void => {
|
|
45
50
|
loggingContext.request = undefined;
|
|
46
51
|
loggingContext.response = undefined;
|
|
47
52
|
loggingContext.error = undefined;
|
|
53
|
+
loggingContext.pseudoUser = undefined;
|
|
48
54
|
};
|
|
49
55
|
|
|
50
56
|
export const getLoggingContextError = (): LoggingError | undefined =>
|
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
]
|
|
12
|
-
}
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"composite": true,
|
|
5
|
+
"outDir": "dist",
|
|
6
|
+
"rootDir": "src"
|
|
7
|
+
},
|
|
8
|
+
"include": ["src"],
|
|
9
|
+
"references": [{ "path": "../shared-errors" }, { "path": "../pii-utils" }]
|
|
10
|
+
}
|