Skip to content
Closed
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
4 changes: 4 additions & 0 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ debug.formatters.a = (v?: Multiaddr): string => {
return v == null ? 'undefined' : v.toString()
}

// Add a formatter for stringifying BigInts
debug.formatters.i = (v?: bigint): string => {
return v == null ? 'undefined' : v.toLocaleString()
}
export interface Logger {
(formatter: any, ...args: any[]): void
error: (formatter: any, ...args: any[]) => void
Expand Down
15 changes: 13 additions & 2 deletions packages/logger/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { toString as unint8ArrayToString } from 'uint8arrays/to-string'
import { logger } from '../src/index.js'

describe('logger', () => {
const debugSpy = sinon.spy(debug, 'log')

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

Expand Down Expand Up @@ -80,13 +82,22 @@ describe('logger', () => {

const ma = multiaddr('/ip4/127.0.0.1/tcp/4001')

const debugSpy = sinon.spy(debug, 'log')

log('multiaddr %a', ma)

expect(debugSpy.firstCall.args[0], 'Multiaddr formatting not included').to.include(`multiaddr ${ma.toString()}`)
})

it('test printf style formatting for big int', () => {
const log = logger('printf-style')
debug.enable('printf-style')

const bi = BigInt(12345678901234567168)

log('big int %i', bi)

expect(debugSpy.secondCall.args[0], 'Big int formatting not included').to.include(`big int ${bi.toLocaleString()}`)
})

it('test ma formatter', () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001')

Expand Down