-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ALL-3453: tatum logger module init * ALL-3453: add logging support * ALL-3453: access logger via tatumsdk container * ALL-3453: add faucet info log to metamask wallet provider * ALL-3453: replace console logs with logger logs * ALL-3453: tweak missing API key log * ALL-3453: add logger test suite * ALL-3453: fix circular dependencies, cr feedback * ALL-3453: bump version, update dependencies (#1042) * ALL-3453: fix missing apikey warning condition --------- Co-authored-by: Filip KaΕ‘tovskΓ½ <[email protected]>
- Loading branch information
1 parent
99129cc
commit 972925f
Showing
29 changed files
with
1,662 additions
and
953 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Network } from '../dto' | ||
import { Ethereum, TatumSDK } from '../service' | ||
import { Logger } from '../service/logger/logger.types' | ||
|
||
describe('Logger', () => { | ||
let logger: Logger | ||
|
||
beforeEach(() => { | ||
logger = { | ||
trace: jest.fn(), | ||
debug: jest.fn(), | ||
error: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
} | ||
}) | ||
|
||
it('should warn on missing API key', async () => { | ||
const tatum = await TatumSDK.init<Ethereum>({ | ||
network: Network.ETHEREUM_SEPOLIA, | ||
logger, | ||
}) | ||
await tatum.destroy() | ||
|
||
expect(logger.warn).toHaveBeenCalledWith( | ||
'API key not provided - only a subset of SDK features will be enabled. Generate an API Key by accessing your Dashboard: https://co.tatum.io/signup', | ||
) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './logger.development' | ||
export * from './logger.development.browser' | ||
export * from './logger.production' | ||
export * from './logger.quiet' | ||
export * from './logger.types' |
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,121 @@ | ||
import { EnvUtils } from '../../util/env' | ||
import { LogLevel, Logger } from './logger.types' | ||
|
||
interface TatumDevelopmentBrowserLoggerOptions { | ||
welcome: boolean | ||
level: LogLevel | ||
} | ||
|
||
type BrowserFormattedMessage = [template: string, ...styles: string[]] | ||
|
||
export class TatumDevelopmentBrowserLogger implements Logger { | ||
private static readonly DISABLE_WELCOME = '__TTM_DISABLE_WELCOME__' | ||
private static isWelcomeDisabled(): boolean { | ||
if (!EnvUtils.isBrowser()) return false | ||
const anyWindow = window as unknown as { __TTM_DISABLE_WELCOME__: boolean } | ||
return anyWindow[TatumDevelopmentBrowserLogger.DISABLE_WELCOME] | ||
} | ||
|
||
private static disableWelcome(): void { | ||
if (!EnvUtils.isBrowser()) return | ||
const anyWindow = window as unknown as { __TTM_DISABLE_WELCOME__: boolean } | ||
anyWindow[TatumDevelopmentBrowserLogger.DISABLE_WELCOME] = true | ||
} | ||
|
||
private readonly options: TatumDevelopmentBrowserLoggerOptions | ||
private readonly logger = console | ||
|
||
private readonly _DEBUG: BrowserFormattedMessage = ['%c Debug %c ', bgGray, initial] | ||
private readonly _INFO: BrowserFormattedMessage = ['%c Info %c ', bgBlue, initial] | ||
private readonly _WARN: BrowserFormattedMessage = ['%c Warn %c ', bgYellowStrong, initial] | ||
private readonly _ERROR: BrowserFormattedMessage = ['%c Error %c ', bgRedStrong, initial] | ||
private readonly _TATUM: BrowserFormattedMessage = ['%c Tatum %c ', bgGradient, initial] | ||
|
||
constructor(options: Partial<TatumDevelopmentBrowserLoggerOptions> = {}) { | ||
const defaultOptions: TatumDevelopmentBrowserLoggerOptions = { | ||
welcome: EnvUtils.isDevelopment() && !TatumDevelopmentBrowserLogger.isWelcomeDisabled(), | ||
level: LogLevel.INFO, | ||
} | ||
|
||
this.options = { ...defaultOptions, ...options } | ||
|
||
if (this.options.welcome) { | ||
this.welcome() | ||
TatumDevelopmentBrowserLogger.disableWelcome() | ||
} | ||
} | ||
|
||
private join(...messages: BrowserFormattedMessage[]): BrowserFormattedMessage { | ||
return messages.reduce( | ||
(acc, curr) => { | ||
acc[0] += curr[0] | ||
acc.push(...curr.slice(1)) | ||
return acc | ||
}, | ||
[''], | ||
) | ||
} | ||
|
||
private welcome(): void { | ||
this.logger.log( | ||
...this.join(this._TATUM, WELCOME_MESSAGES[Math.floor(Math.random() * WELCOME_MESSAGES.length)]), | ||
) | ||
} | ||
|
||
trace(...args: unknown[]): void { | ||
if (this.options.level > LogLevel.TRACE) return | ||
this.logger.trace(...args) | ||
} | ||
|
||
debug(...args: unknown[]): void { | ||
if (this.options.level > LogLevel.DEBUG) return | ||
this.logger.debug(...this._DEBUG, ...args) | ||
} | ||
|
||
info(...args: unknown[]): void { | ||
if (this.options.level > LogLevel.INFO) return | ||
this.logger.info(...this._INFO, ...args) | ||
} | ||
|
||
warn(...args: unknown[]): void { | ||
if (this.options.level > LogLevel.WARN) return | ||
this.logger.warn(...this._WARN, ...args) | ||
} | ||
|
||
error(...args: unknown[]): void { | ||
if (this.options.level > LogLevel.ERROR) return | ||
this.logger.error(...this._ERROR, ...args) | ||
} | ||
} | ||
|
||
const bgGradient = | ||
'color: white; font-weight: bold; background-image: linear-gradient(126deg,#513bff 9%,#89ffca 97%);' | ||
|
||
const initial = 'color: inherit; font-weight: inherit;' | ||
const initialStrong = 'color: inherit; font-weight: bold;' | ||
const green = 'color: rgb(137, 255, 202);' | ||
|
||
const bgRedStrong = 'color: white; background-color: rgb(238, 52, 52); font-weight: bold;' | ||
const bgYellowStrong = 'color: white; background-color: rgb(255, 140, 0); font-weight: bold;' | ||
const bgBlue = 'color: white; background-color: rgb(81, 59, 255);' | ||
const bgGray = 'color: white; background-color: rgb(158, 158, 158);' | ||
|
||
const WELCOME = 'Hi! π Welcome to Tatum, the Javascript SDK for Web3.' | ||
|
||
const BOLD = [initialStrong, initial] | ||
|
||
const WELCOME_MESSAGES: BrowserFormattedMessage[] = [ | ||
[ | ||
`%c${WELCOME}%c\nVisit our docs to see how Tatum will help you launch projects fast: https://co.tatum.io/docs`, | ||
...BOLD, | ||
], | ||
[`%c${WELCOME}%c\nKick start by making your first RPC call: https://co.tatum.io/start`, ...BOLD], | ||
[`%c${WELCOME}%c\nSee what apps you can build with Tatum: https://co.tatum.io/apps`, ...BOLD], | ||
[ | ||
`%c${WELCOME}%c\n%cFREE Testnet Tokens%c: Explore %cTatum Faucets%c available for over 5 chains: https://co.tatum.io/faucets`, | ||
...BOLD, | ||
...BOLD, | ||
green, | ||
initial, | ||
], | ||
] |
Oops, something went wrong.