From 952865950c13f070a6a2de064a81b5d9b13279de Mon Sep 17 00:00:00 2001 From: Clockwork Date: Tue, 19 Sep 2023 15:16:56 +0300 Subject: [PATCH] feat: Make relayer logging level configurable (#3626) * feat: Make relayer logging level configurable * fix: LogLevel constants * chore: Add changelog entry * fix: address review --------- Co-authored-by: Danilo Pantani --- changelog.md | 1 + .../data/gen-nodetime/src/relayer/index.ts | 4 ++- .../gen-nodetime/src/relayer/lib/logger.ts | 36 ++++++++++++++----- .../gen-nodetime/src/relayer/lib/relayer.ts | 17 +++++---- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/changelog.md b/changelog.md index 4df7449dd2..53c1c3ba6d 100644 --- a/changelog.md +++ b/changelog.md @@ -36,6 +36,7 @@ - [#3446](https://github.com/ignite/cli/pull/3446) Add `gas-adjustment` flag to the cosmos client. - [#3439](https://github.com/ignite/cli/pull/3439) Add `--build.tags` flag for `chain serve` and `chain build` commands. - [#3524](https://github.com/ignite/cli/pull/3524) Apply auto tools migration to other commands +- [#3636](https://github.com/ignite/cli/pull/3626) Add logging levels to relayer - Added compatibility check and auto migration features and interactive guidelines for the latest versions of the SDK ### Changes diff --git a/scripts/data/gen-nodetime/src/relayer/index.ts b/scripts/data/gen-nodetime/src/relayer/index.ts index c7c7000620..fbc98cffa2 100644 --- a/scripts/data/gen-nodetime/src/relayer/index.ts +++ b/scripts/data/gen-nodetime/src/relayer/index.ts @@ -1,8 +1,10 @@ import run from "./jsonrpc"; +import { LogLevels } from "./lib/logger"; import Relayer from "./lib/relayer"; -const relayer = new Relayer(); +const logLevel = parseInt(process.argv[2]); +const relayer = new Relayer(isNaN(logLevel) ? LogLevels.INFO: logLevel); run([ ["link", relayer.link.bind(relayer)], diff --git a/scripts/data/gen-nodetime/src/relayer/lib/logger.ts b/scripts/data/gen-nodetime/src/relayer/lib/logger.ts index 8f19bf92f7..00a966f12e 100644 --- a/scripts/data/gen-nodetime/src/relayer/lib/logger.ts +++ b/scripts/data/gen-nodetime/src/relayer/lib/logger.ts @@ -7,7 +7,13 @@ interface Logger { verbose: LogMethod; debug: LogMethod; } - +export enum LogLevels { + ERROR = 0, + WARN = 1, + INFO = 2, + VERBOSE = 3, + DEBUG = 4 +} export default class ConsoleLogger { public readonly error: LogMethod; public readonly warn: LogMethod; @@ -15,23 +21,37 @@ export default class ConsoleLogger { public readonly verbose: LogMethod; public readonly debug: LogMethod; - constructor() { - this.error = () => { + constructor(logLevel:LogLevels) { + this.error = (msg) => { + if(logLevel>=LogLevels.ERROR) { + console.log(msg); + } return this; }; - this.warn = () => { + this.warn = (msg) => { + if(logLevel>=LogLevels.WARN) { + console.log(msg); + } return this; }; this.info = (msg) => { - if (msg.indexOf('Relay') == 0 && msg.indexOf('Relay 0') == -1) { - console.log(msg); + if(logLevel>=LogLevels.INFO) { + if (msg.indexOf('Relay') == 0 && msg.indexOf('Relay 0') == -1) { + console.log(msg); + } } return this; }; - this.verbose = () => { + this.verbose = (msg) => { + if(logLevel>=LogLevels.VERBOSE) { + console.log(msg); + } return this; }; - this.debug = () => { + this.debug = (msg) => { + if(logLevel>=LogLevels.DEBUG) { + console.log(msg); + } return this; }; } diff --git a/scripts/data/gen-nodetime/src/relayer/lib/relayer.ts b/scripts/data/gen-nodetime/src/relayer/lib/relayer.ts index 59e385e4fd..da8ca80123 100644 --- a/scripts/data/gen-nodetime/src/relayer/lib/relayer.ts +++ b/scripts/data/gen-nodetime/src/relayer/lib/relayer.ts @@ -7,7 +7,7 @@ import {buildCreateClientArgs, IbcClientOptions, prepareConnectionHandshake} fro import {orderFromJSON} from "cosmjs-types/ibc/core/channel/v1/channel"; // local imports. -import ConsoleLogger from './logger'; +import ConsoleLogger, { LogLevels } from './logger'; type Chain = { id: string; @@ -42,7 +42,11 @@ const defaultEstimatedIndexerTime = 80; export default class Relayer { private defaultMaxAge = 86400; + private logLevel = 2; + constructor(logLevel: LogLevels=LogLevels.INFO) { + if (logLevel) this.logLevel=logLevel; + } public async link([ path, srcChain, @@ -52,7 +56,7 @@ export default class Relayer { ]: [Path, Chain, Chain, string, string]): Promise { const srcClient = await Relayer.getIBCClient(srcChain, srcKey); const dstClient = await Relayer.getIBCClient(dstChain, dstKey); - const link = await Relayer.create(srcClient, dstClient, srcChain.client_id, dstChain.client_id); + const link = await Relayer.create(srcClient, dstClient, srcChain.client_id, dstChain.client_id, this.logLevel); const channels = await link.createChannel( 'A', @@ -69,7 +73,7 @@ export default class Relayer { return path; } - + public async start([ path, srcChain, @@ -85,7 +89,7 @@ export default class Relayer { dstClient, path.src.connection_id, path.dst.connection_id, - new ConsoleLogger() + new ConsoleLogger(this.logLevel) ); const heights = await link.checkAndRelayPacketsAndAcks( @@ -133,7 +137,8 @@ export default class Relayer { nodeA: IbcClient, nodeB: IbcClient, clientA: string, - clientB: string + clientB: string, + logLevel:number ): Promise { let dstClientID = clientB; if (!clientB) { @@ -199,6 +204,6 @@ export default class Relayer { const endA = new Endpoint(nodeA, srcClientID, connIdA); const endB = new Endpoint(nodeB, dstClientID, connIdB); - return new Link(endA, endB, new ConsoleLogger()); + return new Link(endA, endB, new ConsoleLogger(logLevel)); } }