Skip to content

Commit

Permalink
fix: correct DEBUG log level handling in WalletManager (#265)
Browse files Browse the repository at this point in the history
- Refactor logger initialization in `WalletManager` to correctly handle DEBUG log level
- Add `WalletManagerOptions` interface for better type safety
- Improve log level determination logic
- Export `WalletManagerOptions` from index.ts for external use

This commit ensures that the DEBUG log level is properly set and respected, fixing an issue where DEBUG logs were not being output as expected.
  • Loading branch information
drichar authored Sep 18, 2024
1 parent 9880568 commit 4912e4e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/use-wallet/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { LogLevel } from './logger'
export { WalletManager, WalletManagerConfig } from './manager'
export { WalletManager, WalletManagerConfig, WalletManagerOptions } from './manager'
export { NetworkId } from './network'
export { State, WalletState, defaultState } from './store'
export { StorageAdapter } from './storage'
Expand Down
47 changes: 33 additions & 14 deletions packages/use-wallet/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,26 @@ import type {
WalletOptions
} from 'src/wallets/types'

export interface WalletManagerOptions {
resetNetwork?: boolean
debug?: boolean
logLevel?: LogLevel
}

export interface WalletManagerConfig {
wallets?: SupportedWallet[]
network?: NetworkId
algod?: NetworkConfig
options?: {
resetNetwork?: boolean
debug?: boolean
logLevel?: LogLevel
}
options?: WalletManagerOptions
}

export type PersistedState = Omit<State, 'algodClient'>

export class WalletManager {
public _clients: Map<WalletId, BaseWallet> = new Map()

public networkConfig: NetworkConfigMap

public store: Store<State>
public subscribe: (callback: (state: State) => void) => () => void

public options: { resetNetwork: boolean }

private logger: ReturnType<typeof logger.createScopedLogger>
Expand All @@ -61,12 +60,15 @@ export class WalletManager {
algod = {},
options = {}
}: WalletManagerConfig = {}) {
const logLevel = options.debug ? LogLevel.DEBUG : options.logLevel || LogLevel.WARN

Logger.setLevel(logLevel)
this.logger = logger.createScopedLogger('WalletManager')

this.logger.debug('Initializing WalletManager with options:', options)
// Initialize scoped logger
this.logger = this.initializeLogger(options)

this.logger.debug('Initializing WalletManager with config:', {
wallets,
network,
algod,
options
})

// Initialize network config
this.networkConfig = this.initNetworkConfig(network, algod)
Expand Down Expand Up @@ -114,6 +116,23 @@ export class WalletManager {
this.initializeWallets(wallets)
}

// ---------- Logging ----------------------------------------------- //

private initializeLogger(
options: WalletManagerOptions
): ReturnType<typeof logger.createScopedLogger> {
const logLevel = this.determineLogLevel(options)
Logger.setLevel(logLevel)
return logger.createScopedLogger('WalletManager')
}

private determineLogLevel(options: WalletManagerOptions): LogLevel {
if (options?.debug) {
return LogLevel.DEBUG
}
return options?.logLevel !== undefined ? options.logLevel : LogLevel.WARN
}

// ---------- Store ------------------------------------------------- //

public get algodClient(): algosdk.Algodv2 {
Expand Down

0 comments on commit 4912e4e

Please sign in to comment.