UNPKG

@fuel-ts/logger

Version:

A logger for the Fuel-TS ecosystem

1 lines 7.69 kB
{"version":3,"sources":["../src/index.ts","../src/utils.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-shadow */\n/**\n * @packageDocumentation\n *\n * A logger for fuel based on the [debug](https://www.npmjs.com/package/debug) module.\n *\n * @example\n *\n * ```TypeScript\n * import { logger } from '@fuel-ts/logger'\n *\n * const log = logger('fuel-ts:my:package:name')\n *\n * try {\n * // an operation\n * log('something happened: %s', 'it was ok')\n * } catch (err) {\n * log.error('something bad happened: %o', err)\n * }\n *\n * log('with this walletAddress: %w', address)\n * log('and this B256: %b', address)\n * ```\n *\n * ```console\n * $ DEBUG=fuel-ts:* node index.js\n * something happened: it was ok\n * something bad happened: <stack trace>\n * with this walletAddress: 0xf212....3aDc\n * ```\n */\n\nimport type { Address } from '@fuel-ts/address';\nimport type { BN } from '@fuel-ts/math';\nimport { bn } from '@fuel-ts/math';\nimport debug from 'debug';\n\nimport { truncateWalletAddress } from './utils';\n\nfunction createDisabledLogger(namespace: string): debug.Debugger {\n const logger = (): void => {};\n logger.enabled = false;\n logger.color = '';\n logger.diff = 0;\n logger.log = (): void => {};\n logger.namespace = namespace;\n logger.destroy = () => true;\n logger.extend = () => logger;\n\n return logger;\n}\n\n/**\n * Creates a logger for the passed component name.\n *\n * @example\n *\n * ```TypeScript\n * import { logger } from '@fuel-ts/logger'\n *\n * const log = logger('my-component')\n * log.info('hello world')\n * // logs \"my-component hello world\"\n * ```\n */\nexport function logger(name: string): Logger {\n // info logging is a no-op by default\n let info: debug.Debugger = createDisabledLogger(`${name}:info`);\n\n // look at all the debug names and see if info logging has explicitly been enabled\n if (\n debug.enabled(`${name}:info`) &&\n debug.names.map((r) => r.toString()).find((n) => n.includes(':info')) != null\n ) {\n info = debug(`${name}:info`);\n }\n\n return Object.assign(debug(name), {\n error: debug(`${name}:error`),\n warn: debug(`${name}:warn`),\n info,\n });\n}\n\nexport function disable(): void {\n debug.disable();\n}\n\nexport function enable(namespaces: string): void {\n debug.enable(namespaces);\n}\n\nexport function enabled(namespaces: string): boolean {\n return debug.enabled(namespaces);\n}\n\n// Add a formatter for outputting a comma separated BN string\ndebug.formatters.a = (v?: BN): string => {\n if (!v) {\n return 'undefined';\n }\n return v.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',');\n};\n\n// Add a formatter for converting to a b256 string\ndebug.formatters.b = (v?: Address): string => (v == null ? 'undefined' : v.toB256());\n\n// Add a formatter for outputting a BN hex string\ndebug.formatters.h = (v?: BN): string => (v == null ? 'undefined' : v.toHex());\n\n// Converts a hex string to a BN string\ndebug.formatters.n = (hex: string): string => {\n if (!hex) {\n return 'undefined';\n }\n try {\n const bnValue = bn(hex, 'hex');\n return bnValue.toString();\n } catch (error) {\n return 'invalid hex';\n }\n};\n\nexport interface Logger {\n (formatter: unknown, ...args: unknown[]): void;\n error(formatter: unknown, ...args: unknown[]): void;\n warn(formatter: unknown, ...args: unknown[]): void;\n info(formatter: unknown, ...args: unknown[]): void;\n enabled: boolean;\n}\n\nexport interface ComponentLogger {\n forComponent(name: string): Logger;\n}\n\nexport interface AddressLoggerOptions {\n prefixLength: number;\n suffixLength: number;\n}\n\n/**\n * Create a component logger that will prefix any log messages with the passed\n * string.\n *\n * @example\n *\n * ```TypeScript\n * import { prefixLogger } from '@fuel-ts/logger'\n *\n * const logger = prefixLogger('my-node')\n *\n * const log = logger.forComponent('my-component')\n * log.info('hello world')\n * // logs \"my-node:my-component hello world\"\n * ```\n */\nexport function prefixLogger(prefix: string): ComponentLogger {\n return {\n forComponent(name: string) {\n return logger(`${prefix}:${name}`);\n },\n };\n}\n\n/**\n * Create a component logger that will prefix any log messages with a truncated\n * wallet address.\n *\n * @example\n *\n * ```TypeScript\n * import { walletLogger } from '@fuel-ts/logger'\n *\n * const walletAddress = wallet.address.toAddress()\n * const logger = walletLogger(walletAddress)\n *\n * const log = logger.forComponent('my-component')\n * log.info('hello world')\n * ```\n */\nexport function walletLogger(\n walletAddress: Address,\n options: Partial<AddressLoggerOptions> = {}\n): ComponentLogger {\n return prefixLogger(truncateWalletAddress(walletAddress, options));\n}\n\n/**\n * Create a component logger\n *\n * @example\n *\n * ```TypeScript\n * import { defaultLogger } from '@fuel-ts/logger'\n *\n * const logger = defaultLogger()\n *\n * const log = logger.forComponent('my-package')\n * log.info('hello world')\n * // logs \"my-package hello world\"\n * ```\n */\nexport function defaultLogger(): ComponentLogger {\n return {\n forComponent(name: string) {\n return logger(name);\n },\n };\n}\n","import type { Address } from '@fuel-ts/address';\n\nimport type { AddressLoggerOptions } from './index';\n\nexport function truncateWalletAddress(\n walletAddress: Address,\n options: Partial<AddressLoggerOptions> = {}\n): string {\n const prefixLength = options.prefixLength ?? 2;\n const suffixLength = options.suffixLength ?? 4;\n\n const walletAddressString = walletAddress.toString();\n return `${walletAddressString.substring(0, prefixLength)}…${walletAddressString.substring(\n walletAddressString.length,\n walletAddressString.length - suffixLength\n )}`;\n}\n"],"mappings":";;;;AAkCA,SAAS,UAAU;AACnB,OAAO,WAAW;;;AC/BX,SAAS,sBACd,eACA,UAAyC,CAAC,GAClC;AACR,QAAM,eAAe,QAAQ,gBAAgB;AAC7C,QAAM,eAAe,QAAQ,gBAAgB;AAE7C,QAAM,sBAAsB,cAAc,SAAS;AACnD,SAAO,GAAG,oBAAoB,UAAU,GAAG,YAAY,CAAC,SAAI,oBAAoB;AAAA,IAC9E,oBAAoB;AAAA,IACpB,oBAAoB,SAAS;AAAA,EAC/B,CAAC;AACH;AAZgB;;;ADmChB,SAAS,qBAAqB,WAAmC;AAC/D,QAAMA,UAAS,6BAAY;AAAA,EAAC,GAAb;AACf,EAAAA,QAAO,UAAU;AACjB,EAAAA,QAAO,QAAQ;AACf,EAAAA,QAAO,OAAO;AACd,EAAAA,QAAO,MAAM,MAAY;AAAA,EAAC;AAC1B,EAAAA,QAAO,YAAY;AACnB,EAAAA,QAAO,UAAU,MAAM;AACvB,EAAAA,QAAO,SAAS,MAAMA;AAE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