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

Revert "feat: logging improvement" #118

Merged
merged 1 commit into from
Aug 21, 2024
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
3 changes: 1 addition & 2 deletions packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"@libp2p/webtransport": "^4.1.2",
"@multiformats/multiaddr": "^12.3.0",
"it-pipe": "^3.0.1",
"libp2p": "^1.8.3",
"tslog": "^4.9.3"
"libp2p": "^1.8.3"
}
}
58 changes: 36 additions & 22 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ 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 @@ -46,8 +42,6 @@ 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 @@ -121,47 +115,60 @@ export class TopologyNetworkNode {
this._pubsub = this._node.services.pubsub as PubSub<GossipsubEvents>;
this.peerId = this._node.peerId.toString();

log.info("::start", "Successfuly started topology network w/ peer_id", this.peerId);
console.log(
"topology::network::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) =>
log.info("::start::peer::connect", e.detail)
console.log("peer:connect", e.detail),
);
this._node.addEventListener("peer:discovery", (e) =>
log.info("::start::peer::discovery", e.detail)
console.log("peer:discovery", e.detail),
);
this._node.addEventListener("peer:identify", (e) =>
log.info("::start::peer::identify", e.detail)
console.log("peer:identify", e.detail),
);
}

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

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

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

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

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

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

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

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

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

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

Expand Down
5 changes: 0 additions & 5 deletions packages/node/configs/bootstrap.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@
"/dns4/relay.droak.sh/tcp/443/wss/p2p/Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP"
],
"private_key_seed": "bootstrap"
},
"tslog_config": {
"minLevel": 3,
"type": "hidden",
"hideLogPositionForProduction": true
}
}
112 changes: 0 additions & 112 deletions packages/node/configs/example.json

This file was deleted.

5 changes: 0 additions & 5 deletions packages/node/configs/node.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
"/ip4/127.0.0.1/tcp/50000/ws/p2p/12D3KooWC6sm9iwmYbeQJCJipKTRghmABNz1wnpJANvSMabvecwJ"
],
"private_key_seed": "node"
},
"tslog_config": {
"minLevel": 3,
"type": "hidden",
"hideLogPositionForProduction": true
}
}
3 changes: 1 addition & 2 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@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",
"tslog": "^4.9.3"
"commander": "^12.1.0"
}
}
16 changes: 1 addition & 15 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import { TopologyObjectStore, TopologyObjectStoreCallback } 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 {
network_config?: TopologyNetworkNodeConfig;
tslog_config?: ISettingsParam<ILogObj>;
}

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

constructor(config?: TopologyNodeConfig) {
this._config = 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.networkNode = new TopologyNetworkNode(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.23-5";
export const VERSION = "0.0.22";
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

"skipLibCheck": true,
"stripInternal": true,
"resolveJsonModule": true,
"inlineSourceMap": true
"resolveJsonModule": true
}
}