Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@multiformats/multiaddr": "^13.0.1",
"interface-datastore": "^9.0.1",
"multiformats": "^13.4.0",
"weald": "^1.0.6"
"weald": "^1.1.0"
},
"devDependencies": {
"@libp2p/peer-id": "^6.0.3",
Expand Down
31 changes: 17 additions & 14 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import type { PeerId, Logger, ComponentLogger } from '@libp2p/interface'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Key } from 'interface-datastore'
import type { CID } from 'multiformats/cid'
import type { Options as LoggerOptions } from 'weald'

export type { LoggerOptions }

// Add a formatter for converting to a base58 string
debug.formatters.b = (v?: Uint8Array): string => {
Expand Down Expand Up @@ -123,9 +126,9 @@ function createDisabledLogger (namespace: string): debug.Debugger {
return logger
}

export interface PeerLoggerOptions {
prefixLength: number
suffixLength: number
export interface PeerLoggerOptions extends LoggerOptions {
prefixLength?: number
suffixLength?: number
}

/**
Expand All @@ -146,8 +149,8 @@ export interface PeerLoggerOptions {
* // logs "12…oBar:my-component hello world"
* ```
*/
export function peerLogger (peerId: PeerId, options: Partial<PeerLoggerOptions> = {}): ComponentLogger {
return prefixLogger(truncatePeerId(peerId, options))
export function peerLogger (peerId: PeerId, options: PeerLoggerOptions = {}): ComponentLogger {
return prefixLogger(truncatePeerId(peerId, options), options)
}

/**
Expand All @@ -166,10 +169,10 @@ export function peerLogger (peerId: PeerId, options: Partial<PeerLoggerOptions>
* // logs "my-node:my-component hello world"
* ```
*/
export function prefixLogger (prefix: string): ComponentLogger {
export function prefixLogger (prefix: string, options?: LoggerOptions): ComponentLogger {
return {
forComponent (name: string) {
return logger(`${prefix}:${name}`)
return logger(`${prefix}:${name}`, options)
}
}
}
Expand All @@ -190,10 +193,10 @@ export function prefixLogger (prefix: string): ComponentLogger {
* // logs "my-component hello world"
* ```
*/
export function defaultLogger (): ComponentLogger {
export function defaultLogger (options?: LoggerOptions): ComponentLogger {
return {
forComponent (name: string) {
return logger(name)
return logger(name, options)
}
}
}
Expand All @@ -211,19 +214,19 @@ export function defaultLogger (): ComponentLogger {
* // logs "my-component hello world"
* ```
*/
export function logger (name: string): Logger {
export function logger (name: string, options?: LoggerOptions): Logger {
// trace logging is a no-op by default
let trace: debug.Debugger = createDisabledLogger(`${name}:trace`)

// look at all the debug names and see if trace logging has explicitly been enabled
if (debug.enabled(`${name}:trace`) && debug.names.map((r: any) => r.toString()).find((n: string) => n.includes(':trace')) != null) {
trace = debug(`${name}:trace`)
trace = debug(`${name}:trace`, options)
}

return Object.assign(debug(name), {
error: debug(`${name}:error`),
return Object.assign(debug(name, options), {
error: debug(`${name}:error`, options),
trace,
newScope: (scope: string) => logger(`${name}:${scope}`)
newScope: (scope: string) => logger(`${name}:${scope}`, options)
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PeerLoggerOptions } from './index.js'
import type { PeerId } from '@libp2p/interface'

export function truncatePeerId (peerId: PeerId, options: Partial<PeerLoggerOptions> = {}): string {
export function truncatePeerId (peerId: PeerId, options: PeerLoggerOptions = {}): string {
const prefixLength = options.prefixLength ?? 2
const suffixLength = options.suffixLength ?? 4

Expand Down
30 changes: 29 additions & 1 deletion packages/logger/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import sinon from 'sinon'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import debug from 'weald'
import { logger, peerLogger } from '../src/index.js'
import { format } from 'weald/format'
import { disable, enable, logger, peerLogger } from '../src/index.js'

describe('logger', () => {
afterEach(() => {
disable()
})

it('creates a logger', () => {
const log = logger('hello')

Expand Down Expand Up @@ -163,4 +168,27 @@ describe('logger', () => {

expect(debug.formatters.k(key)).to.equal(key.toString())
})

it('collects logs', () => {
enable('*,*:trace')
debug.useColors = () => false

const logs: any[] = []
const log = logger('hello', {
onLog (...args) {
logs.push(format(...args))
}
})

log('hello world')
log.error('oh no')
log.trace('shh')

const scope = log.newScope('new-scope')
scope('hello world')
scope.error('oh no')
scope.trace('shh')

expect(logs).to.have.lengthOf(6)
})
})