|
1 | | -import { FastifyInstance } from "fastify"; |
| 1 | +import type { FastifyInstance } from "fastify"; |
| 2 | +import { stringify } from "thirdweb/utils"; |
2 | 3 | import { logger } from "../../utils/logger"; |
| 4 | +import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes"; |
| 5 | + |
| 6 | +const SKIP_LOG_PATHS = new Set([ |
| 7 | + "", |
| 8 | + "/", |
| 9 | + "/favicon.ico", |
| 10 | + "/system/health", |
| 11 | + "/json", |
| 12 | + "/static", |
| 13 | + // Skip these routes case of importing sensitive details. |
| 14 | + "/backend-wallet/import", |
| 15 | + "/configuration/wallets", |
| 16 | +]); |
3 | 17 |
|
4 | 18 | export const withRequestLogs = async (server: FastifyInstance) => { |
5 | | - server.addHook("onRequest", async (request, reply) => { |
| 19 | + server.addHook("onSend", (request, reply, payload, done) => { |
6 | 20 | if ( |
7 | | - !request.routerPath?.includes("static") && |
8 | | - !request.routerPath?.includes("json") && |
9 | | - request.method !== "OPTIONS" && |
10 | | - request.routerPath !== "/" |
| 21 | + request.method === "OPTIONS" || |
| 22 | + !request.routeOptions.url || |
| 23 | + SKIP_LOG_PATHS.has(request.routeOptions.url) || |
| 24 | + request.routeOptions.url.startsWith(ADMIN_QUEUES_BASEPATH) |
11 | 25 | ) { |
12 | | - logger({ |
13 | | - service: "server", |
14 | | - level: "info", |
15 | | - message: `Request received - ${request.method} - ${request.routerPath}`, |
16 | | - }); |
| 26 | + done(); |
| 27 | + return; |
17 | 28 | } |
18 | 29 |
|
19 | | - if (process.env.NODE_ENV === "production") { |
20 | | - if ( |
21 | | - request.routerPath?.includes("static") && |
22 | | - // Bullboard requires access to static bundle files |
23 | | - !request.routerPath?.startsWith("/admin/queues") |
24 | | - ) { |
25 | | - return reply.status(404).send({ |
26 | | - statusCode: 404, |
27 | | - error: "Not Found", |
28 | | - message: "Not Found", |
29 | | - }); |
30 | | - } |
31 | | - } |
32 | | - }); |
| 30 | + const { method, routeOptions, headers, params, query, body } = request; |
| 31 | + const { statusCode, elapsedTime } = reply; |
| 32 | + const isError = statusCode >= 400; |
33 | 33 |
|
34 | | - server.addHook("preHandler", async (request, reply) => { |
35 | | - if ( |
36 | | - !request.routerPath?.includes("static") && |
37 | | - !request.routerPath?.includes("json") && |
38 | | - !request.routerPath?.includes("/backend-wallet/import") && |
39 | | - request.method !== "OPTIONS" |
40 | | - ) { |
41 | | - if (request.body && Object.keys(request.body).length > 0) { |
42 | | - logger({ |
43 | | - service: "server", |
44 | | - level: "info", |
45 | | - message: `Request body - ${request.method} - ${request.routerPath}`, |
46 | | - data: request.body, |
47 | | - }); |
48 | | - } |
| 34 | + const extractedHeaders = { |
| 35 | + "x-backend-wallet-address": headers["x-backend-wallet-address"], |
| 36 | + "x-idempotency-key": headers["x-idempotency-key"], |
| 37 | + "x-account-address": headers["x-account-address"], |
| 38 | + "x-account-factory-address": headers["x-account-factory-address"], |
| 39 | + }; |
49 | 40 |
|
50 | | - if (request.params && Object.keys(request.params).length > 0) { |
51 | | - logger({ |
52 | | - service: "server", |
53 | | - level: "info", |
54 | | - message: `Request params - ${request.method}`, |
55 | | - data: request.params, |
56 | | - }); |
57 | | - } |
| 41 | + const paramsStr = |
| 42 | + params && Object.keys(params).length |
| 43 | + ? `params=${stringify(params)}` |
| 44 | + : undefined; |
| 45 | + const queryStr = |
| 46 | + query && Object.keys(query).length |
| 47 | + ? `querystring=${stringify(query)}` |
| 48 | + : undefined; |
| 49 | + const bodyStr = |
| 50 | + body && Object.keys(body).length ? `body=${stringify(body)}` : undefined; |
| 51 | + const payloadStr = isError ? `payload=${payload}` : undefined; |
58 | 52 |
|
59 | | - if (request.query && Object.keys(request.query).length > 0) { |
60 | | - logger({ |
61 | | - service: "server", |
62 | | - level: "info", |
63 | | - message: `Request querystring - ${request.method} - ${request.routerPath}`, |
64 | | - data: request.query, |
65 | | - }); |
66 | | - } |
67 | | - } |
68 | | - }); |
69 | | - |
70 | | - server.addHook("onResponse", (request, reply, done) => { |
71 | | - if ( |
72 | | - !request.routerPath?.includes("static") && |
73 | | - !request.routerPath?.includes("json") && |
74 | | - request.method !== "OPTIONS" && |
75 | | - request.routerPath !== "/" |
76 | | - ) { |
77 | | - logger({ |
78 | | - service: "server", |
79 | | - level: "info", |
80 | | - message: `Request completed - ${request.method} - ${ |
81 | | - reply.request.routerPath |
82 | | - } - status code: ${reply.statusCode} - Response time: ${reply |
83 | | - .getResponseTime() |
84 | | - .toFixed(2)}ms`, |
85 | | - }); |
86 | | - } |
| 53 | + logger({ |
| 54 | + service: "server", |
| 55 | + level: isError ? "error" : "info", |
| 56 | + message: [ |
| 57 | + `[Request complete - ${statusCode}]`, |
| 58 | + `method=${method}`, |
| 59 | + `path=${routeOptions.url}`, |
| 60 | + `headers=${stringify(extractedHeaders)}`, |
| 61 | + paramsStr, |
| 62 | + queryStr, |
| 63 | + bodyStr, |
| 64 | + `duration=${elapsedTime.toFixed(1)}ms`, |
| 65 | + payloadStr, |
| 66 | + ].join(" "), |
| 67 | + }); |
87 | 68 |
|
88 | 69 | done(); |
89 | 70 | }); |
|
0 commit comments