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
```
24 changes: 24 additions & 0 deletions packages/logger/asconfig.json
winprn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"entries": ["./src/index.asc.ts"],
"targets": {
"debug": {
"outFile": "dist/asc/debug.wasm",
"textFile": "dist/asc/debug.wat",
"sourceMap": true,
"debug": true
},
"release": {
"outFile": "dist/asc/release.wasm",
"textFile": "dist/asc/release.wat",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": false
}
},
"options": {
"bindings": "esm",
"disableWarning": [235]
}
}
43 changes: 43 additions & 0 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"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"
},
"./wasm": {
"types": "./dist/src/index.d.ts",
"import": "./src/index.asc.ts"
}
winprn marked this conversation as resolved.
Show resolved Hide resolved
},
"scripts": {
"asbuild": "yarn asbuild:debug && yarn asbuild:release",
"asbuild:debug": "asc --config asconfig.json --target debug",
"asbuild:release": "asc --config asconfig.json --target release",
winprn marked this conversation as resolved.
Show resolved Hide resolved
"build": "tsc -b",
"clean": "rm -rf dist/ node_modules/",
"prepack": "tsc -b",
"test": "vitest"
},
"devDependencies": {
"assemblyscript": "^0.27.29"
},
winprn marked this conversation as resolved.
Show resolved Hide resolved
"dependencies": {
"winston": "^3.15.0",
"winston-transport": "^4.8.0"
}
}
51 changes: 51 additions & 0 deletions packages/logger/src/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type winston from "winston";
import TransportStream from "winston-transport";

export class BrowserTransport extends TransportStream {
private methods = {
debug: "debug",
error: "error",
info: "info",
warn: "warn",
};

constructor(opts?: TransportStream.TransportStreamOptions) {
super(opts);

if (opts?.level && opts.level in Level) {
this.level = opts.level;
}
}

public log(entry: winston.LogEntry, next: () => void): void {
setImmediate(() => {
this.emit("logged", entry);
});

const { message, level } = entry;
const mappedMethod = this.methods[
level as keyof typeof this.methods
] as keyof Console;

if (Object.getOwnPropertySymbols(entry).length >= 2) {
// @ts-ignore
winprn marked this conversation as resolved.
Show resolved Hide resolved
let args = entry[Object.getOwnPropertySymbols(entry)[1]];
args = args.length >= 1 ? args[0] : args;
// @ts-ignore
if (args) console[mappedMethod](message, args);
// @ts-ignore
else console[mappedMethod](message);
} else {
// @ts-ignore
console[mappedMethod](message);
}
next();
}
}

enum Level {
error = 0,
warn = 1,
info = 2,
debug = 4,
}
Empty file added packages/logger/src/index.ts
Empty file.
13 changes: 13 additions & 0 deletions packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist"
},
"references": [
{
"path": "../object"
}
],
winprn marked this conversation as resolved.
Show resolved Hide resolved
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.asc.ts"]
winprn marked this conversation as resolved.
Show resolved Hide resolved
}
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"
}