-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/logger' into 'dev'
feat: add libp2p logs Closes #46 See merge request ergo/rosen-bridge/rosenet!28
- Loading branch information
Showing
11 changed files
with
205 additions
and
50 deletions.
There are no files selected for viewing
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 @@ | ||
--- | ||
'@rosen-bridge/rosenet-utils': minor | ||
--- | ||
|
||
add libp2pLoggerFactory for customizing libp2p logger |
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,6 @@ | ||
--- | ||
'@rosen-bridge/rosenet-relay': minor | ||
'@rosen-bridge/rosenet-node': minor | ||
--- | ||
|
||
add config to enable libp2p logs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as addEventListeners } from './addEventListeners'; | ||
export { default as privateKeyToPeerId } from './privateKeyToPeerId'; | ||
export { default as libp2pLoggerFactory } from './logger'; |
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,57 @@ | ||
import { format as utilFormat } from 'node:util'; | ||
|
||
import { PeerId } from '@libp2p/interface'; | ||
import { Multiaddr } from '@multiformats/multiaddr'; | ||
import { Key } from 'interface-datastore'; | ||
import { base32 } from 'multiformats/bases/base32'; | ||
import { base58btc } from 'multiformats/bases/base58'; | ||
import { base64 } from 'multiformats/bases/base64'; | ||
import { CID } from 'multiformats/cid'; | ||
|
||
// based on https://github.com/libp2p/js-libp2p/blob/3c73707ff5c1635d4ab26dcc39499ab497d217a6/packages/logger/src/index.ts#L45 | ||
const formatters = { | ||
b: (v?: Uint8Array) => { | ||
return v == null ? 'undefined' : base58btc.baseEncode(v); | ||
}, | ||
t: (v?: Uint8Array) => { | ||
return v == null ? 'undefined' : base32.baseEncode(v); | ||
}, | ||
m: (v?: Uint8Array) => { | ||
return v == null ? 'undefined' : base64.baseEncode(v); | ||
}, | ||
p: (v?: PeerId) => { | ||
return v == null ? 'undefined' : v.toString(); | ||
}, | ||
c: (v?: CID) => { | ||
return v == null ? 'undefined' : v.toString(); | ||
}, | ||
k: (v: Key) => { | ||
return v == null ? 'undefined' : v.toString(); | ||
}, | ||
a: (v?: Multiaddr) => { | ||
return v == null ? 'undefined' : v.toString(); | ||
}, | ||
}; | ||
|
||
/** | ||
* format a string like `util.format`, but support custom libp2p specifiers, too | ||
* based on https://github.com/segmentio/fmt | ||
* @param str | ||
* @param args | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const format = (str: string, ...args: any[]) => { | ||
let j = 0; | ||
|
||
return str.replace(/%([a-z])/gi, (match, f: keyof typeof formatters) => { | ||
if (formatters[f]) { | ||
return formatters[f](args[j++]); | ||
} | ||
if (f) { | ||
return utilFormat(`%${f}`, args[j++]); | ||
} | ||
return match; | ||
}); | ||
}; | ||
|
||
export default format; |
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,50 @@ | ||
import { ComponentLogger, Logger } from '@libp2p/interface'; | ||
import { AbstractLogger, DummyLogger } from '@rosen-bridge/logger-interface'; | ||
|
||
import format from './format'; | ||
|
||
/** | ||
* convert AbstractLogger to a printf-friendly logger | ||
* @param name | ||
* @param logger | ||
* @param level | ||
*/ | ||
const convertToPrintfFormat = | ||
(name: string, logger: AbstractLogger, level: 'info' | 'error' | 'debug') => | ||
(formatter: string, ...rest: unknown[]) => { | ||
const formatted = format(formatter, ...rest); | ||
logger[level](`[${name}] ${formatted}`); | ||
}; | ||
|
||
/** | ||
* create logger for a specific libp2p component | ||
* @param name | ||
* @param logger | ||
*/ | ||
const createLogger = (name: string, logger: AbstractLogger): Logger => { | ||
const componentLogger = convertToPrintfFormat(name, logger, 'info') as Logger; | ||
componentLogger.trace = convertToPrintfFormat(name, logger, 'debug'); | ||
componentLogger.error = convertToPrintfFormat(name, logger, 'error'); | ||
componentLogger.enabled = true; | ||
|
||
return componentLogger; | ||
}; | ||
|
||
/** | ||
* create a ComponentLogger to be used as alternative to default libp2p logger | ||
* @param logger | ||
* @param components | ||
*/ | ||
const libp2pLoggerFactory: ( | ||
logger: AbstractLogger, | ||
components: string[], | ||
) => ComponentLogger = (logger: AbstractLogger, components: string[]) => ({ | ||
forComponent(name: string) { | ||
if (components.includes(name) || components.includes('*')) { | ||
return createLogger(name, logger); | ||
} | ||
return createLogger('', new DummyLogger()); | ||
}, | ||
}); | ||
|
||
export default libp2pLoggerFactory; |
Oops, something went wrong.