Skip to content
This repository was archived by the owner on Mar 24, 2024. It is now read-only.

Commit ad054eb

Browse files
authored
[Front] BeautifyLogger (#82)
* Add Logger class for logging messages * Add new level "throw" to Logger class * Update version numbers in AdminNet, Database, ShopNet, and Table * Update ObjectStorage and SignalR classes * Update version number in ObjectStorage.ts * Fix FileReader error handling in SignalR.ts
1 parent f5b0d65 commit ad054eb

File tree

11 files changed

+465
-357
lines changed

11 files changed

+465
-357
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"@microsoft/signalr-protocol-msgpack": "^8.0.0",
4141
"ahooks": "^3.7.8",
4242
"dayjs": "^1.11.10",
43-
"debug": "^4.3.4",
4443
"dexie": "^3.2.4",
4544
"lexical": "^0.12.5",
4645
"lodash-es": "^4.17.21",
@@ -51,10 +50,9 @@
5150
"rxjs": "^7.8.1"
5251
},
5352
"devDependencies": {
54-
"@types/debug": "^4.1.12",
5553
"@types/lodash-es": "^4.17.12",
5654
"@types/react": "^18.2.45",
57-
"@types/react-dom": "^18.2.17",
55+
"@types/react-dom": "^18.2.18",
5856
"@vitejs/plugin-react-swc": "^3.5.0",
5957
"typescript": "^5.3.3",
6058
"vite": "^5.0.10"

pnpm-lock.yaml

Lines changed: 255 additions & 287 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Helpers/Logger.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import dayjs from "dayjs";
2+
3+
/**
4+
* @author Aloento
5+
* @since 1.0.0
6+
* @version 1.0.0
7+
*/
8+
const enum ANSI {
9+
black = 30,
10+
black_hl = 90,
11+
balck_bg = 40,
12+
black_bg_hl = 100,
13+
14+
red = 31,
15+
red_hl = 91,
16+
red_bg = 41,
17+
red_bg_hl = 101,
18+
19+
green = 32,
20+
green_hl = 92,
21+
green_bg = 42,
22+
green_bg_hl = 102,
23+
24+
yellow = 33,
25+
yellow_hl = 93,
26+
yellow_bg = 43,
27+
yellow_bg_hl = 103,
28+
29+
blue = 34,
30+
blue_hl = 94,
31+
blue_bg = 44,
32+
blue_bg_hl = 104,
33+
34+
magenta = 35,
35+
magenta_hl = 95,
36+
magenta_bg = 45,
37+
magenta_bg_hl = 105,
38+
39+
cyan = 36,
40+
cyan_hl = 96,
41+
cyan_bg = 46,
42+
cyan_bg_hl = 106,
43+
44+
white = 37,
45+
white_hl = 97,
46+
white_bg = 47,
47+
white_bg_hl = 107,
48+
49+
default = 39,
50+
default_bg = 49,
51+
};
52+
53+
type level = "error" | "warn" | "info" | "debug" | "throw";
54+
55+
/**
56+
* @author Aloento
57+
* @since 1.0.0
58+
* @version 1.0.0
59+
*/
60+
export class Logger {
61+
public readonly namespace;
62+
63+
public readonly info;
64+
public readonly error;
65+
public readonly warn;
66+
public readonly debug;
67+
public readonly throw;
68+
69+
public constructor(...namespace: string[]) {
70+
this.namespace = namespace.join(":");
71+
72+
this.error = console.error.bind(this,
73+
this.baseColor(
74+
ANSI.red_bg_hl,
75+
"error"
76+
), "\n\t");
77+
78+
this.warn = console.warn.bind(this,
79+
this.baseColor(
80+
ANSI.yellow_bg_hl,
81+
"warn"
82+
), "\n\t");
83+
84+
this.info = console.info.bind(this,
85+
this.baseColor(
86+
ANSI.blue_bg_hl,
87+
"info"
88+
), "\n\t");
89+
90+
this.debug = console.debug.bind(this,
91+
this.baseColor(
92+
ANSI.green_bg_hl,
93+
"debug"
94+
), "\n\t");
95+
96+
this.throw = console.log.bind(this,
97+
this.baseColor(
98+
ANSI.magenta_bg_hl,
99+
"throw"
100+
),
101+
"↓ The Following Error is Thrown ↓"
102+
);
103+
}
104+
105+
private baseColor(color: ANSI, level: level): string {
106+
return `\x1b[${color};${ANSI.black};1m ${level.toUpperCase()} `
107+
+ `\x1b[0m\x1b[${ANSI.black_bg_hl};${ANSI.white_hl}m ${dayjs().format("YY-M-D H:m:s")} `
108+
+ `\x1b[1m\x1b[${ANSI.balck_bg};${ANSI.white_hl}m ${this.namespace} `;
109+
}
110+
}

src/ShopNet/Admin/AdminNet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export abstract class AdminNet extends SignalR {
1313
/**
1414
* @author Aloento
1515
* @since 1.0.0
16-
* @version 0.1.1
16+
* @version 0.1.2
1717
*/
1818
public static readonly Hub = new HubConnectionBuilder()
1919
.withUrl(import.meta.env.DEV ? "https://localhost/AdminHub" : "https://awai.azurewebsites.net/AdminHub",
@@ -30,6 +30,7 @@ export abstract class AdminNet extends SignalR {
3030
},
3131
})
3232
.withAutomaticReconnect()
33+
.withStatefulReconnect()
3334
.withHubProtocol(new MessagePackHubProtocol())
3435
.configureLogging(import.meta.env.DEV ? LogLevel.Debug : LogLevel.Information)
3536
.build();

src/ShopNet/Database.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const CartTable = DB.table<Omit<ICartItem, "Name" | "Cover">, never>("Sho
4141
/**
4242
* @author Aloento
4343
* @since 1.0.0
44-
* @version 0.1.1
44+
* @version 0.1.2
4545
*/
4646
export const MSAL = new PublicClientApplication({
4747
auth: {
@@ -55,25 +55,16 @@ export const MSAL = new PublicClientApplication({
5555
},
5656
system: {
5757
loggerOptions: {
58-
loggerCallback(level, message, containsPii) {
59-
if (containsPii)
60-
return;
61-
58+
loggerCallback(level, message) {
6259
switch (level) {
6360
case LogLevel.Error:
6461
console.error(message);
6562
return;
6663
case LogLevel.Warning:
6764
console.warn(message);
6865
return;
69-
case LogLevel.Info:
70-
console.info(message);
71-
return;
72-
case LogLevel.Verbose:
73-
console.debug(message);
74-
return;
7566
default:
76-
console.trace(message);
67+
console.debug(message);
7768
}
7869
}
7970
}

src/ShopNet/INet.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { HubConnection } from "@microsoft/signalr";
2+
import type { Logger } from "~/Helpers/Logger";
3+
4+
/**
5+
* @author Aloento
6+
* @since 1.0.0
7+
* @version 0.2.0
8+
*/
9+
export interface INet {
10+
Log: Logger;
11+
Hub: HubConnection;
12+
13+
EnsureConnected: () => Promise<void>;
14+
Invoke: <T>(methodName: string, ...args: any[]) => Promise<T>;
15+
}

src/ShopNet/ObjectStorage.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { IStreamResult } from "@microsoft/signalr";
2+
import { Logger } from "~/Helpers/Logger";
23
import { Shared } from "./Database";
34
import { ShopNet } from "./ShopNet";
45

56
/**
67
* @author Aloento
78
* @since 1.0.0
8-
* @version 0.1.0
9+
* @version 0.1.1
910
*/
1011
export class ObjectStorage extends ShopNet {
12+
static Log = new Logger("ObjectStorage");
13+
1114
/**
1215
* @author Aloento
1316
* @since 1.0.0
@@ -24,21 +27,22 @@ export class ObjectStorage extends ShopNet {
2427
/**
2528
* @author Aloento
2629
* @since 1.0.0
27-
* @version 0.2.0
30+
* @version 0.2.1
2831
*/
2932
public static GetBySlice(objId: string): Promise<Uint8Array[]> {
3033
const slice: Uint8Array[] = [];
34+
const my = this;
3135

3236
return Shared.GetOrSet(objId, () => new Promise(
3337
(resolve, reject) => {
34-
ObjectStorage.Get(objId).then(x =>
38+
my.Get(objId).then(x =>
3539
x.subscribe({
3640
error(err) {
3741
reject(err);
3842
},
3943
next(value) {
4044
slice.push(value);
41-
console.debug("Received Slice", objId, slice.length);
45+
my.Log.debug("Received Slice", objId, slice.length);
4246
},
4347
complete() {
4448
resolve(slice);

src/ShopNet/ShopNet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export abstract class ShopNet extends SignalR {
1313
/**
1414
* @author Aloento
1515
* @since 1.0.0
16-
* @version 0.1.1
16+
* @version 0.1.2
1717
*/
1818
public static readonly Hub = new HubConnectionBuilder()
1919
.withUrl(import.meta.env.DEV ? "https://localhost/Hub" : "https://awai.azurewebsites.net/Hub",
@@ -30,6 +30,7 @@ export abstract class ShopNet extends SignalR {
3030
},
3131
})
3232
.withAutomaticReconnect()
33+
.withStatefulReconnect()
3334
.withHubProtocol(new MessagePackHubProtocol())
3435
.configureLogging(import.meta.env.DEV ? LogLevel.Debug : LogLevel.Information)
3536
.build();

0 commit comments

Comments
 (0)