@libp2p/logger
Version:
A logging component for use in js-libp2p modules
112 lines • 3.15 kB
TypeScript
/**
* @packageDocumentation
*
* A logger for libp2p based on [weald](https://www.npmjs.com/package/weald), a TypeScript port of the venerable [debug](https://www.npmjs.com/package/debug) module.
*
* @example
*
* ```TypeScript
* import { logger } from '@libp2p/logger'
*
* const log = logger('libp2p:my:component:name')
*
* try {
* // an operation
* log('something happened: %s', 'it was ok')
* } catch (err) {
* log.error('something bad happened: %e', err)
* }
*
* log('with this peer: %p', {})
* log('and this base58btc: %b', Uint8Array.from([0, 1, 2, 3]))
* log('and this base32: %t', Uint8Array.from([4, 5, 6, 7]))
* ```
*
* ```console
* $ DEBUG=libp2p:* node index.js
* something happened: it was ok
* something bad happened: <stack trace>
* with this peer: 12D3Foo
* with this base58btc: Qmfoo
* with this base32: bafyfoo
* ```
*/
import type { PeerId, Logger, ComponentLogger } from '@libp2p/interface';
import type { Options as LoggerOptions } from 'weald';
export type { LoggerOptions };
export type { Logger, ComponentLogger };
export interface PeerLoggerOptions extends LoggerOptions {
prefixLength?: number;
suffixLength?: number;
}
/**
* Create a component logger that will prefix any log messages with a truncated
* peer id.
*
* @example
*
* ```TypeScript
* import { peerLogger } from '@libp2p/logger'
* import { peerIdFromString } from '@libp2p/peer-id'
*
* const peerId = peerIdFromString('12D3FooBar')
* const logger = peerLogger(peerId)
*
* const log = logger.forComponent('my-component')
* log.info('hello world')
* // logs "12…oBar:my-component hello world"
* ```
*/
export declare function peerLogger(peerId: PeerId, options?: PeerLoggerOptions): ComponentLogger;
/**
* Create a component logger that will prefix any log messages with the passed
* string.
*
* @example
*
* ```TypeScript
* import { prefixLogger } from '@libp2p/logger'
*
* const logger = prefixLogger('my-node')
*
* const log = logger.forComponent('my-component')
* log.info('hello world')
* // logs "my-node:my-component hello world"
* ```
*/
export declare function prefixLogger(prefix: string, options?: LoggerOptions): ComponentLogger;
/**
* Create a component logger
*
* @example
*
* ```TypeScript
* import { defaultLogger } from '@libp2p/logger'
* import { peerIdFromString } from '@libp2p/peer-id'
*
* const logger = defaultLogger()
*
* const log = logger.forComponent('my-component')
* log.info('hello world')
* // logs "my-component hello world"
* ```
*/
export declare function defaultLogger(options?: LoggerOptions): ComponentLogger;
/**
* Creates a logger for the passed component name.
*
* @example
*
* ```TypeScript
* import { logger } from '@libp2p/logger'
*
* const log = logger('my-component')
* log.info('hello world')
* // logs "my-component hello world"
* ```
*/
export declare function logger(name: string, options?: LoggerOptions): Logger;
export declare function disable(): void;
export declare function enable(namespaces: string): void;
export declare function enabled(namespaces: string): boolean;
//# sourceMappingURL=index.d.ts.map