diff --git a/packages/network/package.json b/packages/network/package.json index ee9e7b2f..a2f0bfca 100644 --- a/packages/network/package.json +++ b/packages/network/package.json @@ -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" } } diff --git a/packages/network/src/node.ts b/packages/network/src/node.ts index ac2413c9..cd232173 100644 --- a/packages/network/src/node.ts +++ b/packages/network/src/node.ts @@ -24,6 +24,9 @@ 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 = new Logger(); // snake_casing to match the JSON config export interface TopologyNetworkNodeConfig { @@ -31,6 +34,7 @@ export interface TopologyNetworkNodeConfig { bootstrap?: boolean; bootstrap_peers?: string[]; private_key_seed?: string; + tslog_config?: ISettingsParam; } export class TopologyNetworkNode { @@ -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() { @@ -115,60 +121,47 @@ export class TopologyNetworkNode { this._pubsub = this._node.services.pubsub as PubSub; 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); } } @@ -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); } } @@ -204,11 +194,9 @@ export class TopologyNetworkNode { const 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); } } @@ -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); } } diff --git a/packages/node/configs/bootstrap.json b/packages/node/configs/bootstrap.json index 549106bb..4030b8f1 100644 --- a/packages/node/configs/bootstrap.json +++ b/packages/node/configs/bootstrap.json @@ -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 } } diff --git a/packages/node/configs/example.json b/packages/node/configs/example.json new file mode 100644 index 00000000..9034e71a --- /dev/null +++ b/packages/node/configs/example.json @@ -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 + } + } + + } + \ No newline at end of file diff --git a/packages/node/configs/node.json b/packages/node/configs/node.json index d660844a..fe56312b 100644 --- a/packages/node/configs/node.json +++ b/packages/node/configs/node.json @@ -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 } } diff --git a/packages/node/package.json b/packages/node/package.json index f9951cbd..4cbe69db 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -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" } } diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index 5e49367f..c2b658ae 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -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 { network_config?: TopologyNetworkNodeConfig; + tslog_config?: ISettingsParam; } export class TopologyNode { @@ -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(); } diff --git a/packages/node/src/version.ts b/packages/node/src/version.ts index 85ceb686..2d05f320 100644 --- a/packages/node/src/version.ts +++ b/packages/node/src/version.ts @@ -1 +1 @@ -export const VERSION = "0.0.22"; +export const VERSION = "0.0.23-5"; diff --git a/tsconfig.json b/tsconfig.json index 0d668bc6..1b70efcb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,6 +14,7 @@ "skipLibCheck": true, "stripInternal": true, - "resolveJsonModule": true + "resolveJsonModule": true, + "inlineSourceMap": true } }