From 100d77dfa30fa2f26a9a4aca5476b8a7b39670ed Mon Sep 17 00:00:00 2001 From: Scorbajio Date: Mon, 2 Dec 2024 19:40:38 -0700 Subject: [PATCH] Implement debug_verbosity (#3809) * Implement debug_verbosity * Update docstring * Update test messages * Fix spelling mistake --- packages/client/src/rpc/modules/debug.ts | 22 ++++++++++++ .../client/test/rpc/debug/verbosity.spec.ts | 34 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 packages/client/test/rpc/debug/verbosity.spec.ts diff --git a/packages/client/src/rpc/modules/debug.ts b/packages/client/src/rpc/modules/debug.ts index 33e94cbb0e..c99331957c 100644 --- a/packages/client/src/rpc/modules/debug.ts +++ b/packages/client/src/rpc/modules/debug.ts @@ -43,6 +43,14 @@ export interface structLog { } error: boolean | undefined | null } + +const logLevels: { [key: number]: string } = { + 0: 'error', + 1: 'warn', + 2: 'info', + 3: 'debug', +} + /** * Validate tracer opts to ensure only supports opts are provided * @param opts a dictionary of {@link tracerOpts} @@ -85,6 +93,7 @@ const validateTracerConfig = (opts: tracerOpts): tracerOpts => { * @memberof module:rpc/modules */ export class Debug { + private client: EthereumClient private service: FullEthereumService private chain: Chain private vm: VM @@ -94,6 +103,7 @@ export class Debug { * @param client Client to which the module binds */ constructor(client: EthereumClient, rpcDebug: boolean) { + this.client = client this.service = client.service as FullEthereumService this.chain = this.service.chain this.vm = (this.service as FullEthereumService).execution?.vm @@ -138,6 +148,9 @@ export class Debug { 1, [[validators.hex]], ) + this.verbosity = middleware(callWithStackTrace(this.verbosity.bind(this), this._rpcDebug), 1, [ + [validators.unsignedInteger], + ]) } /** @@ -435,4 +448,13 @@ export class Debug { const tx = block.transactions[txIndex] return bytesToHex(tx.serialize()) } + /** + * Sets the verbosity level of the client logger + * @param level logger level to use with 0 as the lowest verbosity + */ + async verbosity(params: [number]) { + const [level] = params + this.client.config.logger.configure({ level: logLevels[level] }) + return `level: ${this.client.config.logger.level}` + } } diff --git a/packages/client/test/rpc/debug/verbosity.spec.ts b/packages/client/test/rpc/debug/verbosity.spec.ts new file mode 100644 index 0000000000..40a9e02393 --- /dev/null +++ b/packages/client/test/rpc/debug/verbosity.spec.ts @@ -0,0 +1,34 @@ +import { assert, describe, it } from 'vitest' + +import { createClient, createManager, getRPCClient, startRPC } from '../helpers.js' + +const method = 'debug_verbosity' + +const logLevels: { [key: number]: string } = { + 0: 'error', + 1: 'warn', + 2: 'info', + 3: 'debug', +} + +describe(method, () => { + it('works', async () => { + const manager = createManager(await createClient({ opened: true, noPeers: true })) + const rpc = getRPCClient(startRPC(manager.getMethods())) + const client = manager['_client'] + + let res + + // lowest level; e.g. only show errors + const levelError = 0 + res = await rpc.request(method, [levelError]) + assert.equal(res.result, 'level: error', 'verbosity level successfully lowered') + assert.equal(client.config.logger.level, logLevels[levelError]) + + // highest level; e.g. be very verbose and show even debug logs + const levelDebug = 3 + res = await rpc.request(method, [levelDebug]) + assert.equal(res.result, 'level: debug', 'verbosity level successfully increased') + assert.equal(client.config.logger.level, logLevels[levelDebug]) + }) +})