Skip to content

Commit

Permalink
Merge branch 'feat/logger' into 'dev'
Browse files Browse the repository at this point in the history
feat: add libp2p logs

Closes #46

See merge request ergo/rosen-bridge/rosenet!28
  • Loading branch information
vorujack committed May 16, 2024
2 parents 0d8cb79 + 5499c31 commit 0f22714
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-bikes-grow.md
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
6 changes: 6 additions & 0 deletions .changeset/quiet-kings-raise.md
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
121 changes: 72 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/rosenet-node/lib/createRoseNetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createLibp2p } from 'libp2p';

import {
addEventListeners,
libp2pLoggerFactory,
privateKeyToPeerId,
} from '@rosen-bridge/rosenet-utils';

Expand Down Expand Up @@ -112,6 +113,7 @@ const createRoseNetNode = async ({
runOnTransientConnection: true,
}),
},
logger: libp2pLoggerFactory(logger, config.debug?.libp2pComponents ?? []),
});
RoseNetNodeContext.logger.debug('RoseNet node created');

Expand Down
3 changes: 3 additions & 0 deletions packages/rosenet-node/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ export interface RoseNetNodeConfig {
privateKey: string;
port?: number;
whitelist?: string[];
debug?: {
libp2pComponents?: string[];
};
}
2 changes: 2 additions & 0 deletions packages/rosenet-relay/lib/createRoseNetRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createLibp2p } from 'libp2p';

import {
addEventListeners,
libp2pLoggerFactory,
privateKeyToPeerId,
} from '@rosen-bridge/rosenet-utils';

Expand Down Expand Up @@ -83,6 +84,7 @@ const createRoseNetRelay = async ({
name: 'rosenet-relay',
version: packageJson.version,
},
logger: libp2pLoggerFactory(logger, config.debug?.libp2pComponents ?? []),
});

logger.debug('RoseNet relay created');
Expand Down
3 changes: 3 additions & 0 deletions packages/rosenet-relay/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export interface RoseNetRelayConfig {
logger: AbstractLogger;
whitelist: string[];
maxReservations?: number;
debug?: {
libp2pComponents?: string[];
};
}
1 change: 1 addition & 0 deletions packages/utils/lib/index.ts
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';
57 changes: 57 additions & 0 deletions packages/utils/lib/logger/format.ts
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;
50 changes: 50 additions & 0 deletions packages/utils/lib/logger/index.ts
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;
Loading

0 comments on commit 0f22714

Please sign in to comment.