|
@@ -0,0 +1,158 @@
|
|
|
+import fs from "fs";
|
|
|
+import "source-map-support/register";
|
|
|
+
|
|
|
+import chalk from 'chalk'
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+function applyColor(rgb: [number, number, number], isBackground: boolean = false) {
|
|
|
+ const [r, g, b] = rgb;
|
|
|
+ return isBackground ? chalk.bgRgb(r, g, b) : chalk.rgb(r, g, b);
|
|
|
+}
|
|
|
+
|
|
|
+const logColors: Record<string, [number, number, number]> = {
|
|
|
+ base: [69, 64, 74],
|
|
|
+ managers: [128, 20, 217],
|
|
|
+ transmission: [0, 106, 255],
|
|
|
+ adapter: [51, 130, 68],
|
|
|
+ transport: [173, 9, 0],
|
|
|
+ error: [212, 32, 0],
|
|
|
+ util: [200, 204, 177],
|
|
|
+ details: [255, 255, 97],
|
|
|
+ retransmission: [186, 87, 0],
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+class ConsoleLogger {
|
|
|
+ private categoryPath: string[] = []
|
|
|
+ private settings: Record<string, any>;
|
|
|
+ private className!: string
|
|
|
+ constructor(className: string, categoryPath: string[]) {
|
|
|
+ this.className = className
|
|
|
+ let configPath = "./logSetting.json"
|
|
|
+ this.settings = this.loadSettings(configPath);
|
|
|
+ this.categoryPath = categoryPath
|
|
|
+ }
|
|
|
+
|
|
|
+ private loadSettings(configPath: string): Record<string, any> {
|
|
|
+ try {
|
|
|
+ const config = fs.readFileSync(configPath, "utf-8");
|
|
|
+ return JSON.parse(config);
|
|
|
+ } catch (error) {
|
|
|
+ console.error("Failed to load log settings:", error);
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private isCategoryEnabled(categoryPath: string[]): boolean {
|
|
|
+ let currentLevel = this.settings;
|
|
|
+
|
|
|
+ for (const part of categoryPath) {
|
|
|
+ if (currentLevel[part] === undefined) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (typeof currentLevel[part] === "boolean") {
|
|
|
+ return currentLevel[part];
|
|
|
+ }
|
|
|
+ currentLevel = currentLevel[part];
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ log(message: { message: string, details?: any }): void {
|
|
|
+ if (!this.isCategoryEnabled(this.categoryPath)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const category = this.categoryPath.join(" -> ").toUpperCase();
|
|
|
+ const location = this.getLogLocation();
|
|
|
+
|
|
|
+ const primaryCategory = this.categoryPath[0];
|
|
|
+ const rgb = logColors[primaryCategory] || [255, 255, 255];
|
|
|
+ const categoryStyle = applyColor(rgb, true);
|
|
|
+ const locationStyle = applyColor(rgb);
|
|
|
+
|
|
|
+ const formattedCategory = categoryStyle(`[${category}]`);
|
|
|
+ const formattedClassName = categoryStyle(`${this.className}`);
|
|
|
+ const formattedLocation = locationStyle(` ${location}`);
|
|
|
+
|
|
|
+ const formattedMessage = `${formattedClassName}${formattedLocation}: ${message.message}`;
|
|
|
+ console.log(formattedMessage, message.details ? applyColor([255, 255, 97])(message.details) : '');
|
|
|
+
|
|
|
+ if (message.details && this.isCategoryEnabled(["details"])) {
|
|
|
+ console.log(applyColor([255, 255, 97])('Details: '), message.details);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ error(message: { message: string, details?: any }): void {
|
|
|
+ if (!this.isCategoryEnabled(this.categoryPath)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const category = this.categoryPath.join(" -> ").toUpperCase();
|
|
|
+ const location = this.getLogLocation();
|
|
|
+
|
|
|
+ const primaryCategory = this.categoryPath[0];
|
|
|
+ const rgb = logColors[primaryCategory] || [255, 255, 255];
|
|
|
+ const categoryStyle = applyColor(rgb, true);
|
|
|
+ const locationStyle = applyColor(rgb);
|
|
|
+ const messageStyle = applyColor([224, 0, 0])
|
|
|
+ const formattedCategory = categoryStyle(`[${category}]`);
|
|
|
+ const formattedClassName = categoryStyle(`${this.className}`);
|
|
|
+ const formattedLocation = locationStyle(`${location}`);
|
|
|
+ const formattedErrorMessage = messageStyle(`${message.message}`)
|
|
|
+
|
|
|
+ const formattedMessage = `${formattedClassName} ${formattedLocation}: ${formattedErrorMessage}`;
|
|
|
+ console.log(formattedMessage, message.details ? applyColor([224, 0, 0])(message.details) : '');
|
|
|
+
|
|
|
+ if (message.details && this.isCategoryEnabled(["details"])) {
|
|
|
+ console.log(applyColor([224, 0, 0])('Details: '), message.details);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ reloadSettings(configPath: string): void {
|
|
|
+ this.settings = this.loadSettings(configPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ private getLogLocation(): string {
|
|
|
+ if (!this.isCategoryEnabled(["location"])) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ const error = new Error();
|
|
|
+
|
|
|
+ Error.captureStackTrace(error, this.getLogLocation);
|
|
|
+
|
|
|
+ const stack = error.stack?.split("\n") || [];
|
|
|
+ const callerLine = stack[2];
|
|
|
+
|
|
|
+
|
|
|
+ const match = callerLine?.match(/:(\d+):(\d+)\)/);
|
|
|
+ if (match) {
|
|
|
+ const [, line, column] = match;
|
|
|
+
|
|
|
+ return `at line ${line}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ return "at unknown location";
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default ConsoleLogger;
|
|
|
+
|