Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: logging improvement #102

Merged
merged 8 commits into from
Aug 20, 2024
3 changes: 2 additions & 1 deletion packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@libp2p/webtransport": "^4.1.2",
"@multiformats/multiaddr": "^12.3.0",
"it-pipe": "^3.0.1",
"libp2p": "^1.8.3"
"libp2p": "^1.8.3",
"tslog": "^4.9.3"
}
}
58 changes: 22 additions & 36 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ import { bootstrap } from "@libp2p/bootstrap";
import { webTransport } from "@libp2p/webtransport";
import { autoNAT } from "@libp2p/autonat";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
import { Logger, ILogObj, ISettingsParam } from "tslog";

let log: Logger<ILogObj> = new Logger();

// snake_casing to match the JSON config
export interface TopologyNetworkNodeConfig {
addresses?: string[];
bootstrap?: boolean;
bootstrap_peers?: string[];
private_key_seed?: string;
tslog_config?: ISettingsParam<ILogObj>;
}

export class TopologyNetworkNode {
Expand All @@ -42,6 +46,8 @@ export class TopologyNetworkNode {

constructor(config?: TopologyNetworkNodeConfig) {
this._config = config;
log = new Logger(config?.tslog_config);
log.settings.name = "topology::network";
}

async start() {
Expand Down Expand Up @@ -115,60 +121,47 @@ export class TopologyNetworkNode {
this._pubsub = this._node.services.pubsub as PubSub<GossipsubEvents>;
this.peerId = this._node.peerId.toString();

console.log(
"topology::network::start: Successfuly started topology network w/ peer_id",
this.peerId,
);
log.info("::start", "Successfuly started topology network w/ peer_id", this.peerId);

// TODO remove this or add better logger
// we need to keep it now for debugging
this._node.addEventListener("peer:connect", (e) =>
console.log("peer:connect", e.detail),
log.info("::start::peer::connect", e.detail)
);
this._node.addEventListener("peer:discovery", (e) =>
console.log("peer:discovery", e.detail),
log.info("::start::peer::discovery", e.detail)
);
this._node.addEventListener("peer:identify", (e) =>
console.log("peer:identify", e.detail),
log.info("::start::peer::identify", e.detail)
);
}

subscribe(topic: string) {
if (!this._node) {
console.error(
"topology::network::subscribe: Node not initialized, please run .start()",
);
log.error("::subscribe", "Node not initialized, please run .start()");
return;
}

try {
this._pubsub?.subscribe(topic);
this._pubsub?.getPeers();
console.log(
"topology::network::subscribe: Successfuly subscribed the topic",
topic,
);
log.info("::subscribe", "Successfuly subscribed the topic", topic);
} catch (e) {
console.error("topology::network::subscribe:", e);
log.error("::subscribe", e);
}
}

unsubscribe(topic: string) {
if (!this._node) {
console.error(
"topology::network::unsubscribe: Node not initialized, please run .start()",
);
log.error("::unsubscribe", "Node not initialized, please run .start()");
return;
}

try {
this._pubsub?.unsubscribe(topic);
console.log(
"topology::network::unsubscribe: Successfuly unsubscribed the topic",
topic,
);
log.info("::unsubscribe", "Successfuly unsubscribed the topic", topic);
} catch (e) {
console.error("topology::network::unsubscribe:", e);
log.error("::unsubscribe", e);
}
}

Expand All @@ -189,12 +182,9 @@ export class TopologyNetworkNode {
if (this._pubsub?.getSubscribers(topic)?.length === 0) return;
await this._pubsub?.publish(topic, message);

console.log(
"topology::network::broadcastMessage: Successfuly broadcasted message to topic",
topic,
);
log.info("::broadcastMessage", "Successfuly broadcasted message to topic", topic);
} catch (e) {
console.error("topology::network::broadcastMessage:", e);
log.error("::sendMessage", e);
}
}

Expand All @@ -204,11 +194,9 @@ export class TopologyNetworkNode {
const stream = <Stream>await connection?.newStream(protocols);
stringToStream(stream, message);

console.log(
`topology::network::sendMessage: Successfuly sent message to peer: ${peerId} with message: ${message}`,
);
log.info("::sendMessage", "Successfuly sent message to peer", peerId);
} catch (e) {
console.error("topology::network::sendMessage:", e);
log.error("::sendMessage", e);
}
}

Expand All @@ -226,11 +214,9 @@ export class TopologyNetworkNode {
const stream: Stream = (await connection?.newStream(protocols)) as Stream;
stringToStream(stream, message);

console.log(
`topology::network::sendMessageRandomTopicPeer: Successfuly sent message to peer: ${peerId} with message: ${message}`,
);
log.info("::sendGroupMessageRandomPeer", `Successfuly sent message to peer: ${peerId} with message: ${message}`);
} catch (e) {
console.error("topology::network::sendMessageRandomTopicPeer:", e);
log.error("::sendGroupMessageRandomPeer", e);
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/node/configs/bootstrap.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
"/dns4/relay.droak.sh/tcp/443/wss/p2p/Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP"
],
"private_key_seed": "bootstrap"
},
"tslog_config": {
"minLevel": 3,
"type": "hidden",
"hideLogPositionForProduction": true
}
}
112 changes: 112 additions & 0 deletions packages/node/configs/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"network_config": {
"addresses": ["/webrtc"],
"bootstrap_peers": [
"/ip4/127.0.0.1/tcp/50000/ws/p2p/12D3KooWC6sm9iwmYbeQJCJipKTRghmABNz1wnpJANvSMabvecwJ"
],
"private_key_seed": "node"
},
"tslog_config": {
// Visit https://tslog.js.org/#/ for full reference

// "pretty" for formatted output, "json" for JSON output, "hidden" to suppress output
"type": "pretty",

// Optional name for the logger
"name": "mainLogger",

// Minimum log level (0: silly, 1: trace, 2: debug, 3: info, 4: warn, 5: error, 6: fatal)
"minLevel": 3,

// Name of the property to store log arguments (if needed)
"argumentsArrayName": "argumentsArray",

// Set to true to improve performance in production by hiding log positions
"hideLogPositionForProduction": false,

// Template for pretty log output
"prettyLogTemplate": "{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t{{logLevelName}}\t[{{filePathWithLine}}{{name}}]\t",

// Template for pretty error output
"prettyErrorTemplate": "\n{{errorName}} {{errorMessage}}\nerror stack:\n{{errorStack}}",

// Template for pretty error stack trace lines
"prettyErrorStackTemplate": " • {{fileName}}\t{{method}}\n\t{{filePathWithLine}}",

// Separator for parent logger names
"prettyErrorParentNamesSeparator": ":",

// Delimiter for logger name in error output
"prettyErrorLoggerNameDelimiter": "\t",

// Enable or disable styling of pretty logs
"stylePrettyLogs": true,

// Timezone for pretty log messages ("UTC" or "local")
"prettyLogTimeZone": "UTC",

// Styles for different parts of the pretty log output
"prettyLogStyles": {
// ... (styles configuration, example below)
"logLevelName": {
"*": ["bold", "black", "bgWhiteBright", "dim"],
"SILLY": ["bold", "white"],
"TRACE": ["bold", "whiteBright"],
"DEBUG": ["bold", "green"],
"INFO": ["bold", "blue"],
"WARN": ["bold", "yellow"],
"ERROR": ["bold", "red"],
"FATAL": ["bold", "redBright"]
},
"dateIsoStr": "white",
"filePathWithLine": "white",
"name": ["white", "bold"],
"nameWithDelimiterPrefix": ["white", "bold"],
"nameWithDelimiterSuffix": ["white", "bold"],
"errorName": ["bold", "bgRedBright", "whiteBright"],
"fileName": ["yellow"],
"fileNameWithLine": "white"
},

// Options for util.inspect when formatting objects
"prettyInspectOptions": {
"colors": true,
"depth": 4
},

// Placeholder for masked values
"maskPlaceholder": "[***]",

// Array of keys whose values should be masked
"maskValuesOfKeys": ["password"],

// Whether to match mask keys case-insensitively
"maskValuesOfKeysCaseInsensitive": false,

// RegEx for masking values (null to disable)
"maskValuesRegEx": null,

// Array of prefixes to add to log messages
"prefix": [],

// Array of attached transport functions
"attachedTransports": [],

// Property name for storing meta information
"metaProperty": "_meta",

// Overwrite default behavior (all null by default)
"overwrite": {
"mask": null,
"toLogObj": null,
"addMeta": null,
"formatMeta": null,
"formatLogObj": null,
"transportFormatted": null,
"transportJSON": null,
"addPlaceholders": null
}
}

}

5 changes: 5 additions & 0 deletions packages/node/configs/node.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"/ip4/127.0.0.1/tcp/50000/ws/p2p/12D3KooWC6sm9iwmYbeQJCJipKTRghmABNz1wnpJANvSMabvecwJ"
],
"private_key_seed": "node"
},
"tslog_config": {
"minLevel": 3,
"type": "hidden",
"hideLogPositionForProduction": true
}
}
3 changes: 2 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@topology-foundation/crdt": "0.0.23-5",
"@topology-foundation/network": "0.0.23-5",
"@topology-foundation/object": "0.0.23-5",
"commander": "^12.1.0"
"commander": "^12.1.0",
"tslog": "^4.9.3"
}
}
16 changes: 15 additions & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { TopologyObjectStore } from "./store";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
import { OPERATIONS } from "./operations.js";
import { ILogObj, ISettingsParam } from "tslog"

export * from "./operations.js";

// snake_casing to match the JSON config
export interface TopologyNodeConfig {
d-roak marked this conversation as resolved.
Show resolved Hide resolved
network_config?: TopologyNetworkNodeConfig;
tslog_config?: ISettingsParam<ILogObj>;
}

export class TopologyNode {
Expand All @@ -26,7 +28,19 @@ export class TopologyNode {

constructor(config?: TopologyNodeConfig) {
this._config = config;
this.networkNode = new TopologyNetworkNode(config?.network_config);
if(!this._config) this._config = {};
if(!this._config?.tslog_config){
this._config.tslog_config = {
type: "hidden",
minLevel: 3,
hideLogPositionForProduction: true,
prettyLogTemplate: "{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t{{logLevelName}}\t{{filePathWithLine}}{{name}}"
};
}
if(!this._config.network_config?.tslog_config){
this._config.network_config = { tslog_config: this._config?.tslog_config };
}
this.networkNode = new TopologyNetworkNode(this._config?.network_config);
this._objectStore = new TopologyObjectStore();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.0.22";
export const VERSION = "0.0.23-5";
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"skipLibCheck": true,
"stripInternal": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"inlineSourceMap": true
d-roak marked this conversation as resolved.
Show resolved Hide resolved
}
}