@ogcio/fastify-logging-wrapper 4.0.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/README.md +55 -0
- package/__tests__/fastify-logging-wrapper.errors.test.ts +127 -0
- package/__tests__/fastify-logging-wrapper.test.ts +106 -0
- package/__tests__/helpers/build-fastify.ts +54 -0
- package/__tests__/helpers/build-logger.ts +28 -0
- package/__tests__/helpers/fastify-test-helpers.ts +258 -0
- package/__tests__/logging-wrapper.test.ts +168 -0
- package/dist/fastify-logging-wrapper.d.ts +5 -0
- package/dist/fastify-logging-wrapper.d.ts.map +1 -0
- package/dist/fastify-logging-wrapper.js +34 -0
- package/dist/fastify-logging-wrapper.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/logging-wrapper-entities.d.ts +55 -0
- package/dist/logging-wrapper-entities.d.ts.map +1 -0
- package/dist/logging-wrapper-entities.js +72 -0
- package/dist/logging-wrapper-entities.js.map +1 -0
- package/dist/logging-wrapper.d.ts +20 -0
- package/dist/logging-wrapper.d.ts.map +1 -0
- package/dist/logging-wrapper.js +66 -0
- package/dist/logging-wrapper.js.map +1 -0
- package/package.json +32 -0
- package/src/fastify-logging-wrapper.ts +59 -0
- package/src/index.ts +11 -0
- package/src/logging-wrapper-entities.ts +130 -0
- package/src/logging-wrapper.ts +106 -0
- package/tsconfig.json +12 -0
- package/tsconfig.prod.json +4 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { FastifyError } from "fastify";
|
|
2
|
+
import {
|
|
3
|
+
parseErrorForLogging,
|
|
4
|
+
} from "@ogcio/shared-errors";
|
|
5
|
+
import { HttpError} from "@fastify/sensible";
|
|
6
|
+
import {isHttpError} from "http-errors";
|
|
7
|
+
|
|
8
|
+
export interface LoggingRequest {
|
|
9
|
+
scheme: string;
|
|
10
|
+
method: string;
|
|
11
|
+
path: string | undefined;
|
|
12
|
+
hostname: string;
|
|
13
|
+
query_params: unknown;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface FullLoggingRequest extends LoggingRequest {
|
|
18
|
+
headers: unknown;
|
|
19
|
+
user_agent: string | undefined;
|
|
20
|
+
client_ip: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface LoggingResponse {
|
|
24
|
+
headers: unknown;
|
|
25
|
+
status_code: number;
|
|
26
|
+
[key: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface LoggingError {
|
|
30
|
+
class: LogErrorClasses;
|
|
31
|
+
message: string;
|
|
32
|
+
trace?: string;
|
|
33
|
+
parent?: { name: string; message: string; stack?: string };
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface LoggingContext {
|
|
38
|
+
request?: LoggingRequest;
|
|
39
|
+
response?: LoggingResponse;
|
|
40
|
+
error?: LoggingError;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export enum LogMessages {
|
|
44
|
+
NewRequest = "NEW_REQUEST",
|
|
45
|
+
Response = "RESPONSE",
|
|
46
|
+
Error = "ERROR",
|
|
47
|
+
ApiTrack = "API_TRACK",
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export enum LogErrorClasses {
|
|
51
|
+
ServerError = "SERVER_ERROR",
|
|
52
|
+
ValidationError = "VALIDATION_ERROR",
|
|
53
|
+
RequestError = "REQUEST_ERROR",
|
|
54
|
+
GatewayError = "GATEWAY_ERROR",
|
|
55
|
+
UnknownError = "UNKNOWN_ERROR",
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const REDACTED_VALUE = "[redacted]";
|
|
59
|
+
|
|
60
|
+
export const REDACTED_PATHS = [
|
|
61
|
+
'*.headers["x-amz-security-token"]',
|
|
62
|
+
'*.headers["x-api-key"]',
|
|
63
|
+
'*.headers["authorization"]',
|
|
64
|
+
'*.headers["cookie"]',
|
|
65
|
+
'*.headers["set-cookie"]',
|
|
66
|
+
'*.headers["proxy-authorization"]',
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
export const MESSAGE_KEY = "message";
|
|
70
|
+
|
|
71
|
+
export const REQUEST_ID_LOG_LABEL = "request_id";
|
|
72
|
+
|
|
73
|
+
const UNHANDLED_EXCEPTION_CODE = "UNHANDLED_EXCEPTION";
|
|
74
|
+
|
|
75
|
+
export const toLoggingError = (
|
|
76
|
+
error: HttpError | FastifyError,
|
|
77
|
+
): LoggingError => {
|
|
78
|
+
const output = {
|
|
79
|
+
class: parseErrorClass(error),
|
|
80
|
+
message: error.message,
|
|
81
|
+
trace: error.stack,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
if (isHttpError(error)) {
|
|
85
|
+
const parentInput = error.parentError ?? error.parent;
|
|
86
|
+
const parent = parentInput
|
|
87
|
+
? { parent: parseErrorForLogging(parentInput) }
|
|
88
|
+
: {};
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
...output,
|
|
92
|
+
code: error.name,
|
|
93
|
+
process: error.errorProcess,
|
|
94
|
+
...parent,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
...output,
|
|
100
|
+
code: error.code ?? UNHANDLED_EXCEPTION_CODE,
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const parseErrorClass = (
|
|
105
|
+
error: FastifyError | HttpError,
|
|
106
|
+
): LogErrorClasses => {
|
|
107
|
+
if (!error.statusCode) {
|
|
108
|
+
return LogErrorClasses.UnknownError;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const statusCode = Number(error.statusCode);
|
|
112
|
+
|
|
113
|
+
if (statusCode === 502) {
|
|
114
|
+
return LogErrorClasses.GatewayError;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (statusCode >= 500) {
|
|
118
|
+
return LogErrorClasses.ServerError;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (statusCode === 422) {
|
|
122
|
+
return LogErrorClasses.ValidationError;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (statusCode >= 400) {
|
|
126
|
+
return LogErrorClasses.RequestError;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return LogErrorClasses.UnknownError;
|
|
130
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { FastifyRequest, FastifyReply, FastifyError } from "fastify";
|
|
2
|
+
import { hostname } from "os";
|
|
3
|
+
import {
|
|
4
|
+
LoggingContext,
|
|
5
|
+
LoggingRequest,
|
|
6
|
+
FullLoggingRequest,
|
|
7
|
+
LoggingResponse,
|
|
8
|
+
LoggingError,
|
|
9
|
+
REDACTED_VALUE,
|
|
10
|
+
REDACTED_PATHS,
|
|
11
|
+
MESSAGE_KEY,
|
|
12
|
+
toLoggingError,
|
|
13
|
+
} from "./logging-wrapper-entities.js";
|
|
14
|
+
import { LogLevel, PinoLoggerOptions } from "fastify/types/logger.js";
|
|
15
|
+
import { HttpError } from "@fastify/sensible";
|
|
16
|
+
|
|
17
|
+
const loggingContext: LoggingContext = {};
|
|
18
|
+
|
|
19
|
+
type INPUT_ERROR_TYPES = FastifyError | HttpError;
|
|
20
|
+
|
|
21
|
+
export const getLoggingContext = (params: {
|
|
22
|
+
includeError: boolean;
|
|
23
|
+
}): LoggingContext =>
|
|
24
|
+
params.includeError
|
|
25
|
+
? loggingContext
|
|
26
|
+
: { ...loggingContext, error: undefined };
|
|
27
|
+
|
|
28
|
+
export const setLoggingContext = (params: {
|
|
29
|
+
request?: FastifyRequest;
|
|
30
|
+
response?: FastifyReply;
|
|
31
|
+
error?: INPUT_ERROR_TYPES;
|
|
32
|
+
}): void => {
|
|
33
|
+
if (params.request !== undefined) {
|
|
34
|
+
loggingContext.request = parseLoggingRequest(params.request);
|
|
35
|
+
}
|
|
36
|
+
if (params.response !== undefined) {
|
|
37
|
+
loggingContext.response = parseLoggingResponse(params.response);
|
|
38
|
+
}
|
|
39
|
+
if (params.error !== undefined) {
|
|
40
|
+
loggingContext.error = toLoggingError(params.error);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const resetLoggingContext = (): void => {
|
|
45
|
+
loggingContext.request = undefined;
|
|
46
|
+
loggingContext.response = undefined;
|
|
47
|
+
loggingContext.error = undefined;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const getLoggingContextError = (): LoggingError | undefined =>
|
|
51
|
+
getLoggingContext({ includeError: true }).error;
|
|
52
|
+
|
|
53
|
+
export const getPartialLoggingContextError = ():
|
|
54
|
+
| Omit<LoggingError, "trace">
|
|
55
|
+
| undefined => ({
|
|
56
|
+
...(getLoggingContext({ includeError: true }).error ?? {}),
|
|
57
|
+
trace: undefined,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const getPathWithoutParams = (req: FastifyRequest): string =>
|
|
61
|
+
req.routeOptions?.url ?? req.url.split("?")[0];
|
|
62
|
+
|
|
63
|
+
const parseLoggingRequest = (req: FastifyRequest): LoggingRequest => ({
|
|
64
|
+
scheme: req.protocol,
|
|
65
|
+
method: req.method,
|
|
66
|
+
path: getPathWithoutParams(req),
|
|
67
|
+
hostname: req.hostname,
|
|
68
|
+
query_params: req.query,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export const parseFullLoggingRequest = (
|
|
72
|
+
req: FastifyRequest,
|
|
73
|
+
): FullLoggingRequest => ({
|
|
74
|
+
...parseLoggingRequest(req),
|
|
75
|
+
headers: req.headers,
|
|
76
|
+
client_ip: req.ip,
|
|
77
|
+
user_agent: req.headers["user-agent"] ?? undefined,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const parseLoggingResponse = (res: FastifyReply): LoggingResponse => ({
|
|
81
|
+
status_code: res.statusCode,
|
|
82
|
+
headers: res.getHeaders(),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
export const getLoggerConfiguration = (
|
|
86
|
+
minimumLevel: LogLevel = "debug",
|
|
87
|
+
): PinoLoggerOptions => ({
|
|
88
|
+
base: { hostname: hostname() },
|
|
89
|
+
messageKey: MESSAGE_KEY,
|
|
90
|
+
mixin: () => ({
|
|
91
|
+
timestamp: Date.now(),
|
|
92
|
+
...getLoggingContext({ includeError: false }),
|
|
93
|
+
}),
|
|
94
|
+
redact: {
|
|
95
|
+
paths: REDACTED_PATHS,
|
|
96
|
+
censor: REDACTED_VALUE,
|
|
97
|
+
},
|
|
98
|
+
timestamp: false,
|
|
99
|
+
formatters: {
|
|
100
|
+
level: (name: string, levelVal: number) => ({
|
|
101
|
+
level: levelVal,
|
|
102
|
+
level_name: name.toUpperCase(),
|
|
103
|
+
}),
|
|
104
|
+
},
|
|
105
|
+
level: minimumLevel,
|
|
106
|
+
});
|
package/tsconfig.json
ADDED