diff options
author | Sebastian <sebasjm@gmail.com> | 2023-01-19 17:05:34 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-01-19 17:05:34 -0300 |
commit | 346056ca91d5d91fa3392217c24e8d1e32c91c54 (patch) | |
tree | 96496ed54196e86cb1baa6f1a94442679df806d8 | |
parent | 40279ae7f0520b25f5aecf5b2318a1bb5772c10c (diff) |
feat: adjust log level from developer page
9 files changed, 106 insertions, 35 deletions
diff --git a/packages/taler-util/src/logging.ts b/packages/taler-util/src/logging.ts index 840402d6f..ae4e2df53 100644 --- a/packages/taler-util/src/logging.ts +++ b/packages/taler-util/src/logging.ts @@ -34,34 +34,36 @@ export enum LogLevel { export let globalLogLevel = LogLevel.Info; -export function setGlobalLogLevelFromString(logLevelStr: string) { - let level: LogLevel; +export function setGlobalLogLevelFromString(logLevelStr: string): void { + globalLogLevel = getLevelForString(logLevelStr); +} + +export const byTagLogLevel: Record<string, LogLevel> = {}; +export function setLogLevelFromString(tag: string, logLevelStr: string): void { + byTagLogLevel[tag] = getLevelForString(logLevelStr); +} + +function getLevelForString(logLevelStr: string): LogLevel { switch (logLevelStr.toLowerCase()) { case "trace": - level = LogLevel.Trace; - break; + return LogLevel.Trace; case "info": - level = LogLevel.Info; - break; + return LogLevel.Info; case "warn": case "warning": - level = LogLevel.Warn; - break; + return LogLevel.Warn; case "error": - level = LogLevel.Error; - break; + return LogLevel.Error; case "none": - level = LogLevel.None; - break; + return LogLevel.None; default: if (isNode) { process.stderr.write(`Invalid log level, defaulting to WARNING\n`); } else { console.warn(`Invalid log level, defaulting to WARNING`); } - level = LogLevel.Warn; + return LogLevel.Warn; } - globalLogLevel = level; } function writeNodeLog( @@ -96,10 +98,11 @@ function writeNodeLog( * and uses the corresponding console.* method to log in the browser. */ export class Logger { - constructor(private tag: string) {} + constructor(private tag: string) { } - shouldLogTrace() { - switch (globalLogLevel) { + shouldLogTrace(): boolean { + const level = byTagLogLevel[this.tag] ?? globalLogLevel; + switch (level) { case LogLevel.Trace: return true; case LogLevel.Message: @@ -111,8 +114,9 @@ export class Logger { } } - shouldLogInfo() { - switch (globalLogLevel) { + shouldLogInfo(): boolean { + const level = byTagLogLevel[this.tag] ?? globalLogLevel; + switch (level) { case LogLevel.Trace: case LogLevel.Message: case LogLevel.Info: @@ -124,8 +128,9 @@ export class Logger { } } - shouldLogWarn() { - switch (globalLogLevel) { + shouldLogWarn(): boolean { + const level = byTagLogLevel[this.tag] ?? globalLogLevel; + switch (level) { case LogLevel.Trace: case LogLevel.Message: case LogLevel.Info: @@ -137,8 +142,9 @@ export class Logger { } } - shouldLogError() { - switch (globalLogLevel) { + shouldLogError(): boolean { + const level = byTagLogLevel[this.tag] ?? globalLogLevel; + switch (level) { case LogLevel.Trace: case LogLevel.Message: case LogLevel.Info: @@ -192,7 +198,7 @@ export class Logger { } } - trace(message: any, ...args: any[]): void { + trace(message: string, ...args: any[]): void { if (!this.shouldLogTrace()) { return; } diff --git a/packages/taler-wallet-core/src/dev-experiments.ts b/packages/taler-wallet-core/src/dev-experiments.ts index e1de7dbf1..6c36d6f6c 100644 --- a/packages/taler-wallet-core/src/dev-experiments.ts +++ b/packages/taler-wallet-core/src/dev-experiments.ts @@ -129,7 +129,7 @@ export class DevExperimentHttpLib implements HttpRequestLibrary { url: string, opt?: HttpRequestOptions | undefined, ): Promise<HttpResponse> { - logger.info(`devexperiment httplib ${url}`); + logger.trace(`devexperiment httplib ${url}`); return this.underlyingLib.get(url, opt); } @@ -138,7 +138,7 @@ export class DevExperimentHttpLib implements HttpRequestLibrary { body: any, opt?: HttpRequestOptions | undefined, ): Promise<HttpResponse> { - logger.info(`devexperiment httplib ${url}`); + logger.trace(`devexperiment httplib ${url}`); return this.underlyingLib.postJson(url, body, opt); } @@ -146,7 +146,7 @@ export class DevExperimentHttpLib implements HttpRequestLibrary { url: string, opt?: HttpRequestOptions | undefined, ): Promise<HttpResponse> { - logger.info(`devexperiment httplib ${url}`); + logger.trace(`devexperiment httplib ${url}`); return this.underlyingLib.fetch(url, opt); } } diff --git a/packages/taler-wallet-core/src/operations/withdraw.ts b/packages/taler-wallet-core/src/operations/withdraw.ts index f7edfed91..667b97361 100644 --- a/packages/taler-wallet-core/src/operations/withdraw.ts +++ b/packages/taler-wallet-core/src/operations/withdraw.ts @@ -568,7 +568,7 @@ async function processPlanchetExchangeRequest( } catch (e) { const errDetail = getErrorDetailFromException(e); logger.trace("withdrawal request failed", e); - logger.trace(e); + logger.trace(String(e)); await ws.db .mktx((x) => [x.planchets]) .runReadWrite(async (tx) => { diff --git a/packages/taler-wallet-core/src/util/retries.ts b/packages/taler-wallet-core/src/util/retries.ts index 851824866..fcb63ecd1 100644 --- a/packages/taler-wallet-core/src/util/retries.ts +++ b/packages/taler-wallet-core/src/util/retries.ts @@ -24,6 +24,7 @@ import { AbsoluteTime, Duration, + Logger, TalerErrorDetail, } from "@gnu-taler/taler-util"; import { @@ -44,6 +45,8 @@ import { InternalWalletState } from "../internal-wallet-state.js"; import { PendingTaskType } from "../pending-types.js"; import { GetReadWriteAccess } from "./query.js"; +const logger = new Logger("util/retries.ts"); + export enum OperationAttemptResultType { Finished = "finished", Pending = "pending", diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index bba502842..24c7f7b9e 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -948,9 +948,9 @@ async function dumpCoins(ws: InternalWalletState): Promise<CoinDumpJson> { ageCommitmentProof: c.ageCommitmentProof, spend_allocation: c.spendAllocation ? { - amount: c.spendAllocation.amount, - id: c.spendAllocation.id, - } + amount: c.spendAllocation.amount, + id: c.spendAllocation.id, + } : undefined, }); } diff --git a/packages/taler-wallet-webextension/src/cta/InvoiceCreate/views.tsx b/packages/taler-wallet-webextension/src/cta/InvoiceCreate/views.tsx index 10e0e68d5..71227ace1 100644 --- a/packages/taler-wallet-webextension/src/cta/InvoiceCreate/views.tsx +++ b/packages/taler-wallet-webextension/src/cta/InvoiceCreate/views.tsx @@ -16,7 +16,6 @@ import { format } from "date-fns"; import { h, VNode } from "preact"; -import { ErrorTalerOperation } from "../../components/ErrorTalerOperation.js"; import { LogoHeader } from "../../components/LogoHeader.js"; import { Part } from "../../components/Part.js"; import { diff --git a/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx b/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx index 3df3a470c..ee0986076 100644 --- a/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx +++ b/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx @@ -19,6 +19,7 @@ import { CoinDumpJson, CoinStatus, ExchangeListItem, + LogLevel, NotificationType, } from "@gnu-taler/taler-util"; import { @@ -29,6 +30,7 @@ import { format } from "date-fns"; import { Fragment, h, VNode } from "preact"; import { useEffect, useRef, useState } from "preact/hooks"; import { Diagnostics } from "../components/Diagnostics.js"; +import { SelectList } from "../components/SelectList.js"; import { NotifyUpdateFadeOut } from "../components/styled/index.js"; import { Time } from "../components/Time.js"; import { useBackendContext } from "../context/backend.js"; @@ -37,6 +39,8 @@ import { useAsyncAsHook } from "../hooks/useAsyncAsHook.js"; import { useDiagnostics } from "../hooks/useDiagnostics.js"; import { Button } from "../mui/Button.js"; import { Grid } from "../mui/Grid.js"; +import { Paper } from "../mui/Paper.js"; +import { TextField } from "../mui/TextField.js"; export function DeveloperPage(): VNode { const [status, timedOut] = useDiagnostics(); @@ -167,6 +171,9 @@ export function View({ [exchange_name: string]: CalculatedCoinfInfo[]; }, ); + + const [tagName, setTagName] = useState(""); + const [logLevel, setLogLevel] = useState("info"); return ( <div> <p> @@ -312,6 +319,42 @@ export function View({ </Button> </Grid>{" "} </Grid> + <Paper style={{ padding: 10, margin: 10 }}> + <h3>Logging</h3> + <div> + <TextField + label="Tag name" + placeholder="wallet.ts" + variant="filled" + // error={subject.error} + required + value={tagName} + onChange={setTagName} + /> + <SelectList + label={i18n.str`Log levels`} + list={{ + trace: "TRACE", + info: "INFO", + error: "ERROR", + }} + name="logLevel" + value={logLevel} + onChange={(v) => setLogLevel(v)} + /> + </div> + <Button + variant="contained" + onClick={async () => { + api.background.call("setLoggingLevel", { + tag: tagName, + level: logLevel as LogLevel, + }); + }} + > + Set log level + </Button> + </Paper> {downloadedDatabase && ( <div> <i18n.Translate> diff --git a/packages/taler-wallet-webextension/src/wxApi.ts b/packages/taler-wallet-webextension/src/wxApi.ts index 58ca48745..001f77934 100644 --- a/packages/taler-wallet-webextension/src/wxApi.ts +++ b/packages/taler-wallet-webextension/src/wxApi.ts @@ -25,6 +25,7 @@ import { AbsoluteTime, CoreApiResponse, Logger, + LogLevel, NotificationType, TalerErrorCode, TalerErrorDetail, @@ -85,6 +86,13 @@ export interface BackgroundOperations { request: void; response: void; }; + setLoggingLevel: { + request: { + tag?: string, + level: LogLevel + }; + response: void; + }; } export interface BackgroundApiClient { @@ -122,7 +130,6 @@ class BackgroundApiClientImpl implements BackgroundApiClient { try { response = await platform.sendMessageToBackground(message); } catch (error) { - console.log("Error calling backend"); if (error instanceof Error) { throw new BackgroundError(operation, { code: TalerErrorCode.GENERIC_UNEXPECTED_REQUEST_ERROR, @@ -131,13 +138,13 @@ class BackgroundApiClientImpl implements BackgroundApiClient { } throw error; } - logger.info("got response", response); if (response.type === "error") { throw new BackgroundError( `Background operation "${operation}" failed`, response.error, ); } + logger.trace("response", response); return response.result as any; } } @@ -162,13 +169,13 @@ class WalletApiClientImpl implements WalletCoreApiClient { console.log("Error calling backend"); throw new Error(`Error contacting backend: ${e}`); } - logger.info("got response", response); if (response.type === "error") { throw new BackgroundError( `Wallet operation "${operation}" failed`, response.error, ); } + logger.trace("got response", response); return response.result as any; } } diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index c7b964c28..1bfee1064 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -26,6 +26,9 @@ import { classifyTalerUri, Logger, + LogLevel, + setGlobalLogLevelFromString, + setLogLevelFromString, TalerErrorCode, TalerUriType, WalletDiagnostics, @@ -180,8 +183,18 @@ const backendHandlers: BackendHandlerType = { resetDb, runGarbageCollector, toggleHeaderListener, + setLoggingLevel, }; +async function setLoggingLevel({ tag, level }: { tag?: string, level: LogLevel }): Promise<void> { + logger.info(`setting ${tag} to ${level}`) + if (!tag) { + setGlobalLogLevelFromString(level) + } else { + setLogLevelFromString(tag, level) + } +} + async function dispatch<Op extends WalletOperations | BackgroundOperations>( req: MessageFromFrontend<Op> & { id: string }, ): Promise<MessageResponse> { |