From 4e1b43a8f92dbf2f108773b4cb2ea80d77522a9a Mon Sep 17 00:00:00 2001 From: deuszx Date: Wed, 22 May 2024 16:54:44 +0200 Subject: [PATCH] CMN-607: Replace console.log with logging lib --- package-lock.json | 12 +++++++ package.json | 1 + src/grapqhl/index.ts | 4 ++- src/index.ts | 31 +++++++++++-------- src/servers/http/index.ts | 2 +- src/servers/ws/amm.ts | 23 +++++++------- src/servers/ws/nativeTransfers.ts | 19 ++++++------ ...sdPriceCache.ts => coingeckoPriceCache.ts} | 7 +++-- 8 files changed, 61 insertions(+), 38 deletions(-) rename src/services/{usdPriceCache.ts => coingeckoPriceCache.ts} (91%) diff --git a/package-lock.json b/package-lock.json index 7a4ca0d..834aeb6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "graphql-request": "^6.1.0", "graphql-ws": "^5.15.0", "rxjs": "^7.8.1", + "tslog": "^4.9.2", "ws": "^8.16.0" }, "devDependencies": { @@ -1445,6 +1446,17 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==" }, + "node_modules/tslog": { + "version": "4.9.2", + "resolved": "https://registry.npmjs.org/tslog/-/tslog-4.9.2.tgz", + "integrity": "sha512-wBM+LRJoNl34Bdu8mYEFxpvmOUedpNUwMNQB/NcuPIZKwdDde6xLHUev3bBjXQU7gdurX++X/YE7gLH8eXYsiQ==", + "engines": { + "node": ">=16" + }, + "funding": { + "url": "https://github.com/fullstack-build/tslog?sponsor=1" + } + }, "node_modules/type": { "version": "2.7.2", "resolved": "https://registry.npmjs.org/type/-/type-2.7.2.tgz", diff --git a/package.json b/package.json index 961f349..bc64fd7 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "graphql-request": "^6.1.0", "graphql-ws": "^5.15.0", "rxjs": "^7.8.1", + "tslog": "^4.9.2", "ws": "^8.16.0" } } diff --git a/src/grapqhl/index.ts b/src/grapqhl/index.ts index 254ad58..2aa2edf 100644 --- a/src/grapqhl/index.ts +++ b/src/grapqhl/index.ts @@ -4,6 +4,8 @@ import { Observable } from "rxjs"; import { Connection, ConnectionQuery } from "./connection"; import { SubscriptionQuery } from "./subscription"; +import { log } from "../index"; + /** * Executes GraphQL connection query that iterates over the paginated results. * @@ -41,7 +43,7 @@ export async function readWholeConnection( } } } catch (err) { - console.error(err); + log.error(err); break; } } diff --git a/src/index.ts b/src/index.ts index 3806f49..eb02ae6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,14 +10,23 @@ import { graphqlSubscribe$, RawElement } from "./grapqhl"; import { poolsV2SubscriptionQuery as v2PoolSubscriptionQuery } from "./grapqhl/v2/queries"; import { poolsV2SubscriptionQuery as v1PoolSubscriptionQuery } from "./grapqhl/v1/queries"; import { setupPoolsV2OverWs } from "./servers/ws/amm"; -import { UsdPriceCache } from "./services/usdPriceCache"; +import { UsdPriceCache } from "./services/coingeckoPriceCache"; import { loadInitPoolReserves, poolsV2$ } from "./grapqhl/pools"; import { poolDataSample$ } from "./mocks/pools"; import { Pools } from "./models/pool"; import { merge, Observable, share } from "rxjs"; import { isV2GraphQLReservesError, isV1GraphQLReservesError } from "./utils"; +import { Logger, ILogObj } from "tslog"; + +export const log: Logger = new Logger({ + prettyLogTemplate: + "{{logLevelName}}\t{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t[{{filePathWithLine}}{{name}}]\t", +}); + async function main(): Promise { + log.info("Starting Common API server"); + const app = express(); app.use( cors({ @@ -33,12 +42,8 @@ async function main(): Promise { const server = http.createServer(app); const config = new Config(); - console.log("Starting Common API server"); - server.listen(config.http.port, () => { - console.log( - `HTTP server listening at http://localhost:${config.http.port}`, - ); + log.info(`HTTP server listening at http://localhost:${config.http.port}`); }); let pools = new Pools(); @@ -46,7 +51,7 @@ async function main(): Promise { rest.healthcheckEnpoint(app, config); if (config.enablePriceCache) { - console.log("USD price cache enabled"); + log.info("USD price cache enabled"); const azeroUsdPriceCache = new UsdPriceCache( "aleph-zero", @@ -81,21 +86,21 @@ async function main(): Promise { if (config.enableDemoMode || config.enableGraphql) { const wsServer = new WebSocketServer(config.ws, () => { - console.log( + log.info( `WS server listening at ws://${config.ws.host}:${config.ws.port}`, ); }); if (config.enableDemoMode) { - console.log("Running in demo mode"); + log.info("Running in demo mode"); const source = poolDataSample$.pipe(share()); source.forEach((pool) => pools.update(pool)); setupPoolsV2OverWs(wsServer, source, pools); } else if (config.enableGraphql) { const graphqlClientUrl = `${config.graphql.proto}://${config.graphql.host}:${config.graphql.port}/graphql`; - console.log("Enabling updates over GraphQL/WS"); - console.log(`Connecting Graphql client to ${graphqlClientUrl}`); + log.info("Enabling updates over GraphQL/WS"); + log.info(`Connecting Graphql client to ${graphqlClientUrl}`); const graphqlClient = createClient({ webSocketImpl: WebSocket, @@ -139,7 +144,7 @@ async function main(): Promise { !isV2GraphQLReservesError(err) && !isV1GraphQLReservesError(err) ) { - console.error("Error updating pools", err); + log.error("Error updating pools", err); Promise.reject(err); } }); @@ -171,7 +176,7 @@ async function main(): Promise { // ); } } else { - console.log("Updates over GraphQL/WS are disabled."); + log.info("Updates over GraphQL/WS are disabled."); } } diff --git a/src/servers/http/index.ts b/src/servers/http/index.ts index ac07899..fc60976 100644 --- a/src/servers/http/index.ts +++ b/src/servers/http/index.ts @@ -1,5 +1,5 @@ import { TokenBalances } from "../../models/psp22"; -import { UsdPriceCache } from "../../services/usdPriceCache"; +import { UsdPriceCache } from "../../services/coingeckoPriceCache"; import { Pools } from "../../models/pool"; import express from "express"; import { Config } from "../../config"; diff --git a/src/servers/ws/amm.ts b/src/servers/ws/amm.ts index cfdd8a0..868466d 100644 --- a/src/servers/ws/amm.ts +++ b/src/servers/ws/amm.ts @@ -1,6 +1,7 @@ import { Observable } from "rxjs"; import { WebSocketServer } from "ws"; import { Pools, PoolV2 } from "../../models/pool"; +import { log } from "../../index"; export function setupPoolsV2OverWs( wssServer: WebSocketServer, @@ -9,29 +10,29 @@ export function setupPoolsV2OverWs( ) { wssServer.on("connection", (ws, request) => { ws.send(JSON.stringify(Object.fromEntries(knownState.pools)), (error) => { - if (error) { - console.log("error sending known state", error); - } + log.error("error sending known state", error); }); const subscription = pools.subscribe((pool) => { - ws.send(JSON.stringify(pool), function (err) { - console.error(`Error when sending data to the client over WS: ${err}`); + ws.send(JSON.stringify(pool), (error) => { + log.error("error when sending data to the client over WS", error); }); }); - console.log("started feeding new client the pools events"); + log.trace("started feeding new client the pools events"); - ws.on("error", console.error); + ws.on("error", (error) => { + log.error("error on the websocket connection", error); + }); - ws.on("message", function (message) { - console.log( - `Received message ${message} from user ${JSON.stringify(request)}`, + ws.on("message", (message) => { + log.trace( + `received message ${message} from user ${JSON.stringify(request)}`, ); }); ws.on("close", function () { - console.log("stopping client subscription"); + log.info("stopping client subscription"); subscription.unsubscribe(); }); }); diff --git a/src/servers/ws/nativeTransfers.ts b/src/servers/ws/nativeTransfers.ts index b0b6bfe..4b4f0ba 100644 --- a/src/servers/ws/nativeTransfers.ts +++ b/src/servers/ws/nativeTransfers.ts @@ -1,6 +1,7 @@ import { Observable } from "rxjs"; import { WebSocketServer } from "ws"; import { NativeTransfer } from "../../models/nativeTransfer"; +import { log } from "../../index"; export function setupNativeTransfersOverWss( wssServer: WebSocketServer, @@ -8,24 +9,24 @@ export function setupNativeTransfersOverWss( ) { wssServer.on("connection", (ws, request) => { const subscription = nativeTransfers.subscribe((transfer) => { - ws.send(JSON.stringify(transfer), function () { - // - // Ignore errors. - // + ws.send(JSON.stringify(transfer), (error) => { + log.error("error sending known state", error); }); }); - console.log("started feeding new client the nativeTransfers events"); + log.trace("started feeding new client the nativeTransfers events"); - ws.on("error", console.error); + ws.on("error", (error) => { + log.error("error on the websocket connection", error); + }); - ws.on("message", function (message) { - console.log( + ws.on("message", (message) => { + log.trace( `Received message ${message} from user ${JSON.stringify(request)}`, ); }); ws.on("close", function () { - console.log("stopping client subscription"); + log.info("stopping client subscription"); subscription.unsubscribe(); }); }); diff --git a/src/services/usdPriceCache.ts b/src/services/coingeckoPriceCache.ts similarity index 91% rename from src/services/usdPriceCache.ts rename to src/services/coingeckoPriceCache.ts index 51249ad..52ed479 100644 --- a/src/services/usdPriceCache.ts +++ b/src/services/coingeckoPriceCache.ts @@ -1,6 +1,7 @@ import axios from "axios"; import { UsdTokenPrice } from "../models/usdTokenPrice"; +import { log } from "../index"; export class UsdPriceCache { ticker: string; @@ -34,13 +35,13 @@ export class UsdPriceCache { lastUpdateTimestampSeconds: this.lastUpdateTimestampSeconds, }; } else if (response.status == 429) { - console.log("Rate limited, returning cached price"); + log.warn("Rate limited, returning cached price"); return { price: this.price, lastUpdateTimestampSeconds: this.lastUpdateTimestampSeconds, }; } else { - console.error( + log.error( `Unhandled status when fetching ${this.ticker} price`, response, ); @@ -50,7 +51,7 @@ export class UsdPriceCache { }; } } catch (e) { - console.error(`Error fetching ${this.ticker} price`, e); + log.warn(`Error fetching ticker's price`, {ticker: this.ticker, error: e}); return { price: this.price, lastUpdateTimestampSeconds: this.lastUpdateTimestampSeconds,