-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable setting logging level in the application
- Loading branch information
1 parent
cfd6b7b
commit 3a9fd12
Showing
9 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...-extension-wallet/src/views/browser-view/features/settings/components/SettingsLogging.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { ReactElement, useEffect, useState } from 'react'; | ||
import styles from '@views/browser/features/settings/components/SettingsLayout.module.scss'; | ||
import { SettingsCard, SettingsLink } from '@views/browser/features/settings'; | ||
import { Typography } from 'antd'; | ||
import { Switch } from '@lace/common'; | ||
import { getBackgroundStorage, setBackgroundStorage } from '@lib/scripts/background/storage'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
const { Title } = Typography; | ||
|
||
export const SettingsLogging = (): ReactElement => { | ||
const { t } = useTranslation(); | ||
const [isLoggingEnabled, setIsLoggingEnabled] = useState(false); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
const backgroundStorage = await getBackgroundStorage(); | ||
setIsLoggingEnabled(backgroundStorage.logLevel === 'debug'); | ||
})(); | ||
}, []); | ||
|
||
const handleLoggingOnChange = async () => { | ||
// todo: change this to 'info' | ||
await setBackgroundStorage({ logLevel: isLoggingEnabled ? 'warn' : 'debug' }); | ||
setIsLoggingEnabled(!isLoggingEnabled); | ||
}; | ||
|
||
return ( | ||
<> | ||
<SettingsCard> | ||
<Title level={5} className={styles.heading5} data-testid={'debugger-heading'}> | ||
{t('browserView.settings.debugging.title')} | ||
</Title> | ||
<SettingsLink | ||
onClick={handleLoggingOnChange} | ||
description={t('browserView.settings.debugging.description')} | ||
addon={ | ||
<Switch | ||
testId="settings-logging-switch" | ||
checked={isLoggingEnabled} | ||
onChange={() => setIsLoggingEnabled(!isLoggingEnabled)} | ||
className={styles.analyticsSwitch} | ||
/> | ||
} | ||
data-testid="settings-logging-level-section" | ||
> | ||
{t('browserView.settings.debugging.title')} | ||
</SettingsLink> | ||
</SettingsCard> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,69 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable no-console, no-magic-numbers */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Logger } from 'ts-log'; | ||
import { toSerializableObject } from '@cardano-sdk/util'; | ||
|
||
export class StringifyLogger implements Logger { | ||
enum LogLevel { | ||
error = 0, | ||
warn = 1, | ||
info = 2, | ||
debug = 3, | ||
trace = 4 | ||
} | ||
|
||
export type LogLevelString = keyof typeof LogLevel; | ||
|
||
class StringifyLogger implements Logger { | ||
private logLevel: LogLevel; | ||
|
||
constructor(logLevel: LogLevelString = 'info') { | ||
if (!(logLevel in LogLevel)) { | ||
throw new Error(`Invalid log level: ${logLevel}`); | ||
} | ||
this.logLevel = LogLevel[logLevel]; | ||
} | ||
|
||
private convertParams(params: any[]) { | ||
return params.map((param) => | ||
param && typeof param === 'object' ? JSON.stringify(toSerializableObject(param)) : param | ||
); | ||
} | ||
|
||
private shouldLog(level: LogLevel) { | ||
return level <= this.logLevel; | ||
} | ||
|
||
setLogLevel(logLevel: LogLevelString) { | ||
this.logLevel = LogLevel[logLevel]; | ||
} | ||
|
||
trace(...params: any[]): void { | ||
console.trace(...this.convertParams(params)); | ||
if (this.shouldLog(LogLevel.trace)) { | ||
console.trace(...this.convertParams(params)); | ||
} | ||
} | ||
|
||
debug(...params: any[]): void { | ||
console.debug(...this.convertParams(params)); | ||
if (this.shouldLog(LogLevel.debug)) { | ||
console.debug(...this.convertParams(params)); | ||
} | ||
} | ||
|
||
info(...params: any[]): void { | ||
console.log(...this.convertParams(params)); | ||
if (this.shouldLog(LogLevel.info)) { | ||
console.info(...this.convertParams(params)); | ||
} | ||
} | ||
|
||
warn(...params: any[]): void { | ||
console.warn(...this.convertParams(params)); | ||
if (this.shouldLog(LogLevel.warn)) { | ||
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters