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 package #206

Merged
merged 15 commits into from
Oct 29, 2024
31 changes: 31 additions & 0 deletions packages/logger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Topology Logger

This package makes it easy to log messages in a structured way.

## Usage

This package is intended to be used as a dependency for the Topology Protocol. However, you can use it as a standalone package. For that, you can install it using:

```bash
# yarn
yarn add @topology-foundation/logger

# npm
npm install @topology-foundation/logger
```

### Build

To build the package, you can run:

```bash
yarn build
```

### Tests

To run the tests, you can run:

```bash
yarn test
```
33 changes: 33 additions & 0 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@topology-foundation/logger",
"version": "0.2.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/topology-foundation/ts-topology.git"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"scripts": {
"build": "tsc -b",
"clean": "rm -rf dist/ node_modules/",
"prepack": "tsc -b",
"test": "vitest"
},
"dependencies": {
"loglevel": "^1.9.2",
"loglevel-plugin-prefix": "^0.8.4"
}
}
1 change: 1 addition & 0 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./log.js";
28 changes: 28 additions & 0 deletions packages/logger/src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import loglevel from "loglevel";
import prefix from "loglevel-plugin-prefix";

export interface LoggerOptions {
level?: loglevel.LogLevelDesc;
}

export class Logger {
private log: loglevel.Logger;
// biome-ignore lint/suspicious/noExplicitAny: Do this to allow any method to be called on the logger
[key: string]: any;

constructor(context: string, config?: LoggerOptions) {
this.log = loglevel.getLogger(context);
this.log.setLevel(config?.level || "info");
prefix.reg(loglevel);
prefix.apply(this.log, {
template: "%n",
});

for (const method of Object.keys(this.log)) {
const logMethod = this.log[method as keyof loglevel.Logger];
if (typeof logMethod === "function") {
this[method as string] = logMethod.bind(this.log);
}
}
}
}
7 changes: 7 additions & 0 deletions packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src/**/*.ts", "src/**/*.js"]
}
6 changes: 6 additions & 0 deletions packages/logger/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"includeVersion": true,
"entryPoints": ["src/index.ts"],
"readme": "README.md"
}
1 change: 1 addition & 0 deletions packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@libp2p/websockets": "^9.0.7",
"@libp2p/webtransport": "^5.0.9",
"@multiformats/multiaddr": "^12.3.1",
"@topology-foundation/logger": "^0.2.0",
"it-length-prefixed": "^9.1.0",
"it-map": "^3.1.1",
"it-pipe": "^3.0.1",
Expand Down
47 changes: 21 additions & 26 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ import { webSockets } from "@libp2p/websockets";
import * as filters from "@libp2p/websockets/filters";
import { webTransport } from "@libp2p/webtransport";
import { multiaddr } from "@multiformats/multiaddr";
import { Logger, type LoggerOptions } from "@topology-foundation/logger";
import { type Libp2p, createLibp2p } from "libp2p";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
import { Message } from "./proto/messages_pb.js";
import { uint8ArrayToStream } from "./stream.js";

export * from "./stream.js";

let log: Logger;

// snake_casing to match the JSON config
export interface TopologyNetworkNodeConfig {
addresses?: string[];
bootstrap?: boolean;
bootstrap_peers?: string[];
browser_metrics?: boolean;
private_key_seed?: string;
log_config?: LoggerOptions;
}

export class TopologyNetworkNode {
Expand All @@ -52,6 +56,7 @@ export class TopologyNetworkNode {

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

async start() {
Expand Down Expand Up @@ -130,62 +135,52 @@ 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",
log.info(
"::start: Successfuly started topology network w/ peer_id",
this.peerId,
);

this._node.addEventListener("peer:connect", (e) =>
console.log("::start::peer::connect", e.detail),
log.info("::start::peer::connect", e.detail),
);
this._node.addEventListener("peer:discovery", (e) => {
// current bug in v11.0.0 requires manual dial (https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/issues/149)
for (const ma of e.detail.multiaddrs) {
this._node?.dial(ma);
}
console.log("::start::peer::discovery", e.detail);
log.info("::start::peer::discovery", e.detail);
});
this._node.addEventListener("peer:identify", (e) =>
console.log("::start::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 @@ -206,12 +201,12 @@ export class TopologyNetworkNode {
const messageBuffer = Message.encode(message).finish();
await this._pubsub?.publish(topic, messageBuffer);

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

Expand All @@ -222,7 +217,7 @@ export class TopologyNetworkNode {
const messageBuffer = Message.encode(message).finish();
uint8ArrayToStream(stream, messageBuffer);
} catch (e) {
console.error("topology::network::sendMessage:", e);
log.error("::sendMessage:", e);
}
}

Expand All @@ -241,7 +236,7 @@ export class TopologyNetworkNode {
const messageBuffer = Message.encode(message).finish();
uint8ArrayToStream(stream, messageBuffer);
} catch (e) {
console.error("topology::network::sendMessageRandomTopicPeer:", e);
log.error("::sendMessageRandomTopicPeer:", e);
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/network/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"compilerOptions": {
"outDir": "dist"
},
"references": [
{
"path": "../logger"
}
],
"include": ["src/**/*.ts"]
}
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@topology-foundation/blueprints": "0.2.0",
"@topology-foundation/network": "0.2.0",
"@topology-foundation/object": "0.2.0",
"@topology-foundation/logger": "0.2.0",
"commander": "^12.1.0",
"google-protobuf": "^3.21.2"
}
Expand Down
8 changes: 7 additions & 1 deletion packages/node/src/rpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as grpc from "@grpc/grpc-js";

import type { ServerUnaryCall, sendUnaryData } from "@grpc/grpc-js";
import { Logger } from "@topology-foundation/logger";
import type { TopologyNode } from "../index.js";
import { TopologyRpcService } from "../proto/rpc_grpc_pb.js";
import type {
Expand All @@ -13,6 +14,11 @@ import type {
} from "../proto/rpc_pb.js";

export function init(node: TopologyNode) {
const log = new Logger(
"topology::rpc",
node.config?.network_config?.log_config,
d-roak marked this conversation as resolved.
Show resolved Hide resolved
);

function subscribeCro(
call: ServerUnaryCall<SubscribeCroRequest, SubscribeCroResponse>,
callback: sendUnaryData<SubscribeCroResponse>,
Expand Down Expand Up @@ -80,7 +86,7 @@ export function init(node: TopologyNode) {
"0.0.0.0:6969",
grpc.ServerCredentials.createInsecure(),
(_error, _port) => {
console.log("running grpc in port:", _port);
log.info("running grpc in port:", _port);
},
);
}
3 changes: 3 additions & 0 deletions packages/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
},
{
"path": "../object"
},
{
"path": "../logger"
}
],
"include": ["src/**/*.ts", "src/**/*.js"]
Expand Down
1 change: 1 addition & 0 deletions packages/object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@bufbuild/protobuf": "^2.0.0",
"@topology-foundation/logger": "^0.2.0",
"ts-proto": "^2.2.4"
}
}
11 changes: 7 additions & 4 deletions packages/object/src/wasm/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
- ABI
*/
import * as fs from "node:fs";
import { Logger } from "@topology-foundation/logger";
import asc from "assemblyscript/asc";

export async function compileWasm(path: string) {
console.log("Compiling", path);
const log = new Logger("topology::wasm", { level: "info" });

log.info("Compiling", path);
const { error, stderr } = await asc.main(
[path, "--bindings=esm", "--outFile=/tmp/dist.wasm"],
{
Expand All @@ -29,8 +32,8 @@ export async function compileWasm(path: string) {
);

if (error) {
console.log(`Compilation failed: ${error}`);
console.log(stderr.toString());
log.info(`Compilation failed: ${error}`);
log.info(stderr.toString());
return new Uint8Array();
}

Expand All @@ -39,6 +42,6 @@ export async function compileWasm(path: string) {
fs.readFileSync("/tmp/dist.wasm"),
);
// fs.unlinkSync('dist/tmp.wasm');
console.log("Compilation successful", bytecode);
log.info("Compilation successful", bytecode);
return bytecode;
}
5 changes: 5 additions & 0 deletions packages/object/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"compilerOptions": {
"outDir": "dist"
},
"references": [
{
"path": "../logger"
}
],
"include": ["src/**/*.ts"]
}