Skip to content

Commit

Permalink
Add global loggers (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-maj authored Sep 18, 2023
1 parent d6fd6fc commit 5eff958
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 141 deletions.
29 changes: 0 additions & 29 deletions core/fastify/logSetting.ts

This file was deleted.

1 change: 0 additions & 1 deletion core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from "./database/dbConnect";
export * from "./env";
export * from "./error/customError";
export * from "./error/errorHandler";
export * from "./fastify/logSetting";
export * from "./sdk/sdk";
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"node-cron": "^3.0.2",
"p-queue": "^7.3.4",
"pg": "^8.11.0",
"pino": "^8.15.1",
"pino-pretty": "^10.0.0",
"prisma": "^5.2.0",
"uuidv4": "^6.2.13",
Expand Down
20 changes: 9 additions & 11 deletions server/controller/tx-update-listener.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { FastifyInstance } from "fastify";
import { connectToDatabase } from "../../core";
import { getTxById } from "../../src/db/transactions/getTxById";
import { logger } from "../../src/utils/logger";
import {
formatSocketMessage,
getStatusMessageAndConnectionStatus,
} from "../helpers/websocket";
import { subscriptionsData } from "../schemas/websocket";

export const startTxUpdatesNotificationListener = async (
server: FastifyInstance,
): Promise<void> => {
export const startTxUpdatesNotificationListener = async (): Promise<void> => {
try {
// Connect to the DB
const knex = await connectToDatabase();
server.log.info(`Starting update notification listener`);
logger.server.info(`Starting update notification listener`);
// Acquire a connection
const connection = await knex.client.acquireConnection();
connection.query("LISTEN updated_transaction_data");

connection.on(
"notification",
async (msg: { channel: string; payload: string }) => {
server.log.debug(
logger.server.debug(
`Received notification: ${msg.channel}, ${msg.payload}`,
);
const parsedPayload = JSON.parse(msg.payload);
Expand All @@ -47,20 +45,20 @@ export const startTxUpdatesNotificationListener = async (
);

connection.on("end", async () => {
server.log.info(`Connection database ended`);
logger.server.info(`Connection database ended`);
knex.client.releaseConnection(connection);
await knex.destroy();
server.log.info(`Released sql connection : on end`);
logger.server.info(`Released sql connection : on end`);
});

connection.on("error", async (err: any) => {
server.log.error(err);
logger.server.error(err);
knex.client.releaseConnection(connection);
await knex.destroy();
server.log.info(`Released sql connection: on error`);
logger.server.info(`Released sql connection: on error`);
});
} catch (error) {
server.log.error(`Error in notification listener: ${error}`);
logger.server.error(`Error in notification listener: ${error}`);
throw error;
}
};
13 changes: 6 additions & 7 deletions server/helpers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import fastifyExpress from "@fastify/express";
import { TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
import WebSocketPlugin from "@fastify/websocket";
import fastify, { FastifyInstance } from "fastify";
import { env, errorHandler, getLogSettings } from "../../core";
import { env, errorHandler } from "../../core";
import { apiRoutes } from "../../server/api";
import { logger } from "../../src/utils/logger";
import { performHTTPAuthentication } from "../middleware/auth";
import { openapi } from "./openapi";

const createServer = async (serverName: string): Promise<FastifyInstance> => {
const logOptions = getLogSettings(serverName);

const createServer = async (): Promise<FastifyInstance> => {
const server: FastifyInstance = fastify({
logger: logOptions ?? true,
logger: logger.server,
disableRequestLogging: true,
}).withTypeProvider<TypeBoxTypeProvider>();

Expand Down Expand Up @@ -53,11 +52,11 @@ const createServer = async (serverName: string): Promise<FastifyInstance> => {
request.headers.upgrade &&
request.headers.upgrade.toLowerCase() === "websocket"
) {
server.log.debug("WebSocket connection attempt");
logger.server.debug("WebSocket connection attempt");
// ToDo: Uncomment WebSocket Authentication post Auth SDK is implemented
// await performWSAuthentication(request, reply);
} else {
server.log.debug("Regular HTTP request");
logger.server.debug("Regular HTTP request");
await performHTTPAuthentication(request, reply);
}
});
Expand Down
7 changes: 4 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { env } from "../core";
import { logger } from "../src/utils/logger";
import { startTxUpdatesNotificationListener } from "./controller/tx-update-listener";
import createServer from "./helpers/server";

const main = async () => {
const server = await createServer("API-Server");
const server = await createServer();

server.listen(
{
Expand All @@ -12,15 +13,15 @@ const main = async () => {
},
(err) => {
if (err) {
server.log.error(err);
logger.server.error(err);
process.exit(1);
}
},
);

try {
// Check for the Tables Existence post startup
await startTxUpdatesNotificationListener(server);
await startTxUpdatesNotificationListener();
//check walletType and make sure i got all the access i need
} catch (err) {
console.log(err);
Expand Down
37 changes: 37 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { FastifyBaseLogger } from "fastify";
import Pino, { LoggerOptions } from "pino";
import { env } from "../../core/env";

const defaultConfig: LoggerOptions = {
redact: ["headers.authorization"],
transport: {
target: "pino-pretty",
options: {
translateTime: "HH:MM:ss Z",
ignore: "pid,hostname,reqId",
singleLine: true,
},
},
level:
env.NODE_ENV === "production"
? "info"
: env.NODE_ENV === "development"
? "debug"
: env.NODE_ENV === "testing"
? "debug"
: "trace",
};

interface Logger {
server: FastifyBaseLogger;
worker: FastifyBaseLogger;
}

const createLogger = (prefix: string) => {
return Pino({ ...defaultConfig, msgPrefix: prefix });
};

export const logger: Logger = {
server: createLogger("[Server] "),
worker: createLogger("[Worker] "),
};
5 changes: 0 additions & 5 deletions test/e2e/setup-init.ts

This file was deleted.

17 changes: 7 additions & 10 deletions worker/controller/blockchainReader.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import { BigNumber } from "ethers";
import { FastifyInstance } from "fastify";
import { env } from "../../core";
import { TransactionStatusEnum } from "../../server/schemas/transaction";
import { getSentTxs } from "../../src/db/transactions/getSentTxs";
import { updateTx } from "../../src/db/transactions/updateTx";
import { logger } from "../../src/utils/logger";
import { getTransactionReceiptWithBlockDetails } from "../services/blockchain";

const MINED_TX_CRON_ENABLED = env.MINED_TX_CRON_ENABLED;

export const checkForMinedTransactionsOnBlockchain = async (
server: FastifyInstance,
) => {
export const checkForMinedTransactionsOnBlockchain = async () => {
try {
if (!MINED_TX_CRON_ENABLED) {
server.log.warn("Mined Tx Cron is disabled");
logger.worker.warn("Mined Tx Cron is disabled");
return;
}
server.log.info(
logger.worker.info(
"Running Cron to check for mined transactions on blockchain",
);
const transactions = await getSentTxs();

if (transactions.length === 0) {
server.log.warn("No transactions to check for mined status");
logger.worker.warn("No transactions to check for mined status");
return;
}

const txReceiptsWithChainId = await getTransactionReceiptWithBlockDetails(
server,
transactions,
);

Expand All @@ -40,7 +37,7 @@ export const checkForMinedTransactionsOnBlockchain = async (
txReceiptData.effectiveGasPrice != BigNumber.from(-1) &&
txReceiptData.timestamp != -1
) {
server.log.info(
logger.worker.info(
`Got receipt for tx: ${txReceiptData.txHash}, queueId: ${txReceiptData.queueId}, effectiveGasPrice: ${txReceiptData.effectiveGasPrice}`,
);

Expand All @@ -60,7 +57,7 @@ export const checkForMinedTransactionsOnBlockchain = async (

return;
} catch (error) {
server.log.error(error);
logger.worker.error(error);
return;
}
};
30 changes: 14 additions & 16 deletions worker/controller/listener.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
import { FastifyInstance } from "fastify";
import { connectToDatabase } from "../../core";
import { logger } from "../../src/utils/logger";
import { queue } from "../services/pQueue";
import { processTransaction } from "./processTransaction";

const beginTransactionProcessing = (server: FastifyInstance) => {
const beginTransactionProcessing = () => {
return async () => {
server.log.info(`--- processing Q request started at ${new Date()} ---`);
await processTransaction(server);
server.log.info(`--- processing Q request ended at ${new Date()} ---`);
logger.worker.info(`--- processing Q request started at ${new Date()} ---`);
await processTransaction();
logger.worker.info(`--- processing Q request ended at ${new Date()} ---`);
};
};

export const startNotificationListener = async (
server: FastifyInstance,
): Promise<void> => {
export const startNotificationListener = async (): Promise<void> => {
try {
server.log.info(`Starting notification listener`);
logger.worker.info(`Starting notification listener`);
// Acquire a connection
const knex = await connectToDatabase();
const connection = await knex.client.acquireConnection();
connection.query("LISTEN new_transaction_data");

// Adding to Queue to Process Requests
queue.add(beginTransactionProcessing(server));
queue.add(beginTransactionProcessing());

connection.on(
"notification",
async (msg: { channel: string; payload: string }) => {
queue.add(beginTransactionProcessing(server));
queue.add(beginTransactionProcessing());
},
);

connection.on("end", async () => {
server.log.info(`Connection database ended`);
logger.worker.info(`Connection database ended`);
await knex.destroy();
knex.client.releaseConnection(connection);
server.log.info(`Released sql connection : on end`);
logger.worker.info(`Released sql connection : on end`);
});

connection.on("error", async (err: any) => {
server.log.error(err);
logger.worker.error(err);
knex.client.releaseConnection(connection);
await knex.destroy();
server.log.info(`Released sql connection: on error`);
logger.worker.info(`Released sql connection: on error`);
});
} catch (error) {
server.log.error(`Error in notification listener: ${error}`);
logger.worker.error(`Error in notification listener: ${error}`);
throw error;
}
};
Loading

0 comments on commit 5eff958

Please sign in to comment.