123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import fs from "fs";
- import "source-map-support/register";
- // const chalk = require('chalk');
- import chalk from 'chalk'
- // const logColors: Record<string, (text: string) => string> = {
- // base: chalk.bgRgb(69, 64, 74),
- // managers: chalk.bgRgb(128, 20, 217),
- // transmission: chalk.bgRgb(0, 106, 255),
- // adapter: chalk.bgRgb(51, 130, 68),
- // transport: chalk.bgRgb(173, 9, 0),
- // error: chalk.rgb(212, 32, 0),
- // util: chalk.rgb(200, 204, 177),
- // details: chalk.rgb(255, 255, 97),
- // location: chalk.rgb(241, 112, 255),
- // retransmission: chalk.bgRgb(186, 87, 0)
- // };
- 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; // Category or subcategory does not exist
- }
- 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; // Skip logging if the category is disabled
- }
- const category = this.categoryPath.join(" -> ").toUpperCase();
- const location = this.getLogLocation();
- const primaryCategory = this.categoryPath[0];
- const rgb = logColors[primaryCategory] || [255, 255, 255]; // Default to white
- const categoryStyle = applyColor(rgb, true); // Use bgRgb for category
- const locationStyle = applyColor(rgb); // Use rgb for location
- 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; // Skip logging if the category is disabled
- }
- const category = this.categoryPath.join(" -> ").toUpperCase();
- const location = this.getLogLocation();
- const primaryCategory = this.categoryPath[0];
- const rgb = logColors[primaryCategory] || [255, 255, 255]; // Default to white
- const categoryStyle = applyColor(rgb, true); // Use bgRgb for category
- const locationStyle = applyColor(rgb); // Use rgb for location
- 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 ""; // Don't display location if the category is disabled
- }
- const error = new Error();
- // Captures the current stack trace
- Error.captureStackTrace(error, this.getLogLocation);
- const stack = error.stack?.split("\n") || [];
- const callerLine = stack[2]; // Adjust index to get the correct caller line (this may vary based on environment)
- // Extract only line and column numbers using regex
- const match = callerLine?.match(/:(\d+):(\d+)\)/);
- if (match) {
- const [, line, column] = match;
- // return `line ${line}, column ${column}`;
- return `at line ${line}`;
- }
- return "at unknown location";
- }
- }
- export default ConsoleLogger;
|