From 8c22ff5b18af4b5feaaf96d1d0fe2358dc78f97e Mon Sep 17 00:00:00 2001 From: Daniele Ricci Date: Fri, 18 Oct 2024 11:59:55 +0200 Subject: [PATCH] test: stringify objects in service worker console log (#1471) --- .../src/lib/scripts/background/wallet.ts | 3 +- packages/common/package.json | 1 + packages/common/src/index.ts | 2 + packages/common/src/stringifyLogger.ts | 38 +++++++++++++++++++ yarn.lock | 8 ++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 packages/common/src/stringifyLogger.ts diff --git a/apps/browser-extension-wallet/src/lib/scripts/background/wallet.ts b/apps/browser-extension-wallet/src/lib/scripts/background/wallet.ts index e1554f9678..91fa3f2687 100644 --- a/apps/browser-extension-wallet/src/lib/scripts/background/wallet.ts +++ b/apps/browser-extension-wallet/src/lib/scripts/background/wallet.ts @@ -32,8 +32,7 @@ import axiosFetchAdapter from '@shiroyasha9/axios-fetch-adapter'; import { SharedWalletScriptKind } from '@lace/core'; import { getBaseUrlForChain } from '@utils/chain'; import { cacheNamiMetadataSubscription } from './cache-nami-metadata'; - -const logger = console; +import { logger } from '@lace/common'; // It is important that this file is not exported from index, // because creating wallet repository with store creates an actual pouchdb database diff --git a/packages/common/package.json b/packages/common/package.json index 65038813a0..a7ea57e27c 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -51,6 +51,7 @@ "react-tooltip": "4.2.7" }, "devDependencies": { + "ts-log": "^2.2.7", "typescript": "^4.9.5" }, "peerDependencies": { diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index bbad2d3efa..bb637492b8 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -30,3 +30,5 @@ export * from '@ui/components/Radio'; export * from '@ui/components/Timeline'; export * from '@analytics/index'; + +export * from './stringifyLogger'; diff --git a/packages/common/src/stringifyLogger.ts b/packages/common/src/stringifyLogger.ts new file mode 100644 index 0000000000..2252898ae0 --- /dev/null +++ b/packages/common/src/stringifyLogger.ts @@ -0,0 +1,38 @@ +/* eslint-disable no-console */ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { Logger } from 'ts-log'; +import { toSerializableObject } from '@cardano-sdk/util'; + +export class StringifyLogger implements Logger { + private convertParams(params: any[]) { + return params.map((param) => + param && typeof param === 'object' ? JSON.stringify(toSerializableObject(param)) : param + ); + } + + trace(...params: any[]): void { + console.trace(...this.convertParams(params)); + } + + debug(...params: any[]): void { + console.debug(...this.convertParams(params)); + } + + info(...params: any[]): void { + console.log(...this.convertParams(params)); + } + + warn(...params: any[]): void { + console.warn(...this.convertParams(params)); + } + + error(...params: any[]): void { + console.error(...this.convertParams(params)); + } + + fatal(...params: any[]): void { + console.error(...this.convertParams(params)); + } +} + +export const logger = new StringifyLogger(); diff --git a/yarn.lock b/yarn.lock index df2c81caad..5499e0554d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13331,6 +13331,7 @@ __metadata: react-copy-to-clipboard: 5.0.4 react-dom: 17.0.2 react-tooltip: 4.2.7 + ts-log: ^2.2.7 typescript: ^4.9.5 peerDependencies: react: 17.0.2 @@ -56718,6 +56719,13 @@ __metadata: languageName: node linkType: hard +"ts-log@npm:^2.2.7": + version: 2.2.7 + resolution: "ts-log@npm:2.2.7" + checksum: c423a5eb54abb9471578902953814d3d0c88b3f237db016998f8998ecf982cb0f748bb8ebf93670eeba9b836ff0ce407d8065a340f3ab218ea7b9442c255b3d4 + languageName: node + linkType: hard + "ts-mixer@npm:^6.0.3": version: 6.0.4 resolution: "ts-mixer@npm:6.0.4"