feat: adjust log level from developer page
This commit is contained in:
parent
40279ae7f0
commit
346056ca91
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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) => {
|
||||
|
@ -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",
|
||||
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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> {
|
||||
|
Loading…
Reference in New Issue
Block a user