Skip to content

Commit 9eb5d56

Browse files
authored
Update logger with unified interface (#343)
* Update logger with unified interface * Update * Remove notes * Capitalize
1 parent faecf22 commit 9eb5d56

File tree

20 files changed

+421
-159
lines changed

20 files changed

+421
-159
lines changed

src/db/configuration/getConfiguration.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ const withWalletConfig = async (config: Configuration): Promise<Config> => {
7070

7171
// If that succeeds, update the configuration with the encryption password instead
7272
if (decryptedSecretAccessKey) {
73-
logger.worker.info(
74-
`[Encryption] Updating awsSecretAccessKey to use ENCRYPTION_PASSWORD`,
75-
);
73+
logger({
74+
service: "worker",
75+
level: "info",
76+
message: `[Encryption] Updating awsSecretAccessKey to use ENCRYPTION_PASSWORD`,
77+
});
78+
7679
await updateConfiguration({
7780
awsSecretAccessKey: decryptedSecretAccessKey,
7881
});
@@ -113,9 +116,12 @@ const withWalletConfig = async (config: Configuration): Promise<Config> => {
113116

114117
// If that succeeds, update the configuration with the encryption password instead
115118
if (decryptedGcpKey) {
116-
logger.worker.info(
117-
`[Encryption] Updating gcpApplicationCredentialPrivateKey to use ENCRYPTION_PASSWORD`,
118-
);
119+
logger({
120+
service: "worker",
121+
level: "info",
122+
message: `[Encryption] Updating gcpApplicationCredentialPrivateKey to use ENCRYPTION_PASSWORD`,
123+
});
124+
119125
await updateConfiguration({
120126
gcpApplicationCredentialPrivateKey: decryptedGcpKey,
121127
});

src/db/wallets/getAllWallets.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ interface GetAllWalletsParams {
77
limit: number;
88
}
99

10-
// TODO: Add error logging handler from req.log to all queries
1110
export const getAllWallets = async ({
1211
pgtx,
1312
page,

src/server/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const main = async () => {
3838
}
3939

4040
const server: FastifyInstance = fastify({
41-
logger: logger.server,
4241
disableRequestLogging: true,
4342
...(env.ENABLE_HTTPS ? httpsObject : {}),
4443
}).withTypeProvider<TypeBoxTypeProvider>();
@@ -60,7 +59,12 @@ const main = async () => {
6059
},
6160
(err) => {
6261
if (err) {
63-
logger.server.error(err);
62+
logger({
63+
service: "server",
64+
level: "fatal",
65+
message: `Failed to start server`,
66+
error: err,
67+
});
6468
process.exit(1);
6569
}
6670
},

src/server/middleware/auth.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ export const withAuth = async (server: FastifyInstance) => {
108108
password: env.ENCRYPTION_PASSWORD,
109109
});
110110

111-
logger.worker.info(
112-
`[Encryption] Updating authWalletEncryptedJson to use ENCRYPTION_PASSWORD`,
113-
);
111+
logger({
112+
service: "worker",
113+
level: "info",
114+
message: `[Encryption] Updating authWalletEncryptedJson to use ENCRYPTION_PASSWORD`,
115+
});
116+
114117
await updateConfiguration({
115118
authWalletEncryptedJson: encryptedJson,
116119
});
@@ -146,7 +149,11 @@ export const withAuth = async (server: FastifyInstance) => {
146149
try {
147150
await revokeToken({ id: payload.jti });
148151
} catch {
149-
logger.worker.error(`[Auth] Failed to revoke token ${payload.jti}`);
152+
logger({
153+
service: "worker",
154+
level: "error",
155+
message: `[Auth] Failed to revoke token ${payload.jti}`,
156+
});
150157
}
151158
},
152159
},
@@ -223,9 +230,12 @@ export const withAuth = async (server: FastifyInstance) => {
223230
password: env.ENCRYPTION_PASSWORD,
224231
});
225232

226-
logger.worker.info(
227-
`[Encryption] Updating authWalletEncryptedJson to use ENCRYPTION_PASSWORD`,
228-
);
233+
logger({
234+
service: "worker",
235+
level: "info",
236+
message: `[Encryption] Updating authWalletEncryptedJson to use ENCRYPTION_PASSWORD`,
237+
});
238+
229239
await updateConfiguration({
230240
authWalletEncryptedJson: encryptedJson,
231241
});
@@ -312,7 +322,12 @@ export const withAuth = async (server: FastifyInstance) => {
312322
}
313323
}
314324
} catch (err: any) {
315-
logger.server.error(`[Auth] ${err?.message || err}`);
325+
logger({
326+
service: "server",
327+
level: "warn",
328+
message: `Caught error authenticating user`,
329+
error: err,
330+
});
316331
}
317332

318333
// If we have no secret key or authenticated user, return 401

src/server/middleware/error.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FastifyInstance } from "fastify";
22
import { ReasonPhrases, StatusCodes } from "http-status-codes";
33
import { env } from "../../utils/env";
4+
import { logger } from "../../utils/logger";
45

56
export type CustomError = {
67
message: string;
@@ -32,7 +33,12 @@ const flipObject = (data: any) =>
3233

3334
export const withErrorHandler = async (server: FastifyInstance) => {
3435
server.setErrorHandler((error: Error | CustomError, request, reply) => {
35-
server.log.error(error);
36+
logger({
37+
service: "server",
38+
level: "error",
39+
message: `Encountered unhandled server error`,
40+
error,
41+
});
3642

3743
if ("statusCode" in error && "code" in error) {
3844
// Transform unexpected errors into a standard payload

src/server/middleware/logs.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { FastifyInstance } from "fastify";
2+
import { logger } from "../../utils/logger";
23

34
export const withRequestLogs = async (server: FastifyInstance) => {
45
server.addHook("onRequest", async (request, reply) => {
56
if (
67
!request.routerPath?.includes("static") &&
78
!request.routerPath?.includes("json")
89
) {
9-
request.log.info(
10-
`Request Received - ${request.method} - ${request.routerPath}`,
11-
);
10+
logger({
11+
service: "server",
12+
level: "info",
13+
message: `Request received - ${request.method} - ${request.routerPath}`,
14+
});
1215
}
1316

1417
if (process.env.NODE_ENV === "production") {
@@ -29,15 +32,30 @@ export const withRequestLogs = async (server: FastifyInstance) => {
2932
!request.routerPath?.includes("/backend-wallet/import")
3033
) {
3134
if (request.body && Object.keys(request.body).length > 0) {
32-
request.log.info({ ...request.body }, "Request Body - ");
35+
logger({
36+
service: "server",
37+
level: "info",
38+
message: `Request body`,
39+
data: request.body,
40+
});
3341
}
3442

3543
if (request.params && Object.keys(request.params).length > 0) {
36-
request.log.info({ ...request.params }, "Request Params - ");
44+
logger({
45+
service: "server",
46+
level: "info",
47+
message: `Request params`,
48+
data: request.params,
49+
});
3750
}
3851

3952
if (request.query && Object.keys(request.query).length > 0) {
40-
request.log.info({ ...request.query }, "Request Querystring - ");
53+
logger({
54+
service: "server",
55+
level: "info",
56+
message: `Request querystring`,
57+
data: request.query,
58+
});
4159
}
4260
}
4361
});
@@ -47,13 +65,15 @@ export const withRequestLogs = async (server: FastifyInstance) => {
4765
!request.routerPath?.includes("static") &&
4866
!request.routerPath?.includes("json")
4967
) {
50-
request.log.info(
51-
`Request Completed - ${request.method} - ${
68+
logger({
69+
service: "server",
70+
level: "info",
71+
message: `Request completed - ${request.method} - ${
5272
reply.request.routerPath
53-
} - StatusCode: ${reply.statusCode} - Response Time: ${reply
73+
} - status code: ${reply.statusCode} - Response time: ${reply
5474
.getResponseTime()
5575
.toFixed(2)}ms`,
56-
);
76+
});
5777
}
5878

5979
done();

src/server/routes/transaction/status.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Static, Type } from "@sinclair/typebox";
33
import { FastifyInstance } from "fastify";
44
import { StatusCodes } from "http-status-codes";
55
import { getTxById } from "../../../db/transactions/getTxById";
6+
import { logger } from "../../../utils/logger";
67
import { createCustomError } from "../../middleware/error";
78
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
89
import { transactionResponseSchema } from "../../schemas/transaction";
@@ -94,10 +95,22 @@ export async function checkTxStatus(fastify: FastifyInstance) {
9495
});
9596
},
9697
wsHandler: async (connection: SocketStream, request) => {
97-
request.log.info(request, "Websocket Route Handler");
98+
logger({
99+
service: "server",
100+
level: "info",
101+
message: `Websocket route handler`,
102+
data: request,
103+
});
104+
98105
const { queueId } = request.params;
99106
// const timeout = await wsTimeout(connection, queueId, request);
100-
request.log.info(`Websocket Connection Established for ${queueId}`);
107+
108+
logger({
109+
service: "server",
110+
level: "info",
111+
message: `Websocket connection established for ${queueId}`,
112+
});
113+
101114
findOrAddWSConnectionInSharedState(connection, queueId, request);
102115

103116
const returnData = await getTxById({ queueId });
@@ -113,17 +126,34 @@ export async function checkTxStatus(fastify: FastifyInstance) {
113126
}
114127

115128
connection.socket.on("error", (error) => {
116-
request.log.error(error, "Websocket Error");
129+
logger({
130+
service: "server",
131+
level: "error",
132+
message: `Websocket error`,
133+
error,
134+
});
135+
117136
onError(error, connection, request);
118137
});
119138

120139
connection.socket.on("message", async (message, isBinary) => {
121-
request.log.info(message, "Websocket Message Received");
140+
logger({
141+
service: "server",
142+
level: "info",
143+
message: `Websocket message received`,
144+
data: message,
145+
});
146+
122147
onMessage(connection, request);
123148
});
124149

125150
connection.socket.on("close", () => {
126-
request.log.info("Websocket Connection Closed");
151+
logger({
152+
service: "server",
153+
level: "info",
154+
message: `Websocket connection closed`,
155+
});
156+
127157
onClose(connection, request);
128158
// clearTimeout(timeout);
129159
});

src/server/utils/storage/localStorage.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export class LocalFileStorage implements AsyncStorage {
2727
const dir = `${process.env.HOME}/.thirdweb`;
2828
const path = `${dir}/localWallet-${this.walletAddress}`;
2929
if (!fs.existsSync(dir) || !fs.existsSync(path)) {
30-
logger.worker.error(`No local wallet found!`);
30+
logger({
31+
service: "server",
32+
level: "error",
33+
message: `No local wallet found!`,
34+
});
3135
return null;
3236
}
3337

src/server/utils/wallets/getLocalWallet.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ export const getLocalWallet = async ({
6868
);
6969
}
7070

71-
logger.worker.info(
72-
`[Encryption] Updating local wallet ${walletAddress} to use ENCRYPTION_PASSWORD`,
73-
);
71+
logger({
72+
service: "worker",
73+
level: "info",
74+
message: `[Encryption] Updating local wallet ${walletAddress} to use ENCRYPTION_PASSWORD`,
75+
});
7476

7577
await wallet.save({
7678
strategy: "encryptedJson",

0 commit comments

Comments
 (0)