vecbox 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,125 @@
1
+ export enum LogLevel {
2
+ DEBUG = 0,
3
+ INFO = 1,
4
+ WARN = 2,
5
+ ERROR = 3,
6
+ }
7
+
8
+ export interface LogEntry {
9
+ level: LogLevel;
10
+ message: string;
11
+ module: string;
12
+ timestamp: Date;
13
+ }
14
+
15
+ export class Logger {
16
+ private static instance: Logger;
17
+ private currentLevel: LogLevel;
18
+ private moduleName: string;
19
+
20
+ // ANSI color codes - simplified for better readability
21
+ private static readonly COLORS = {
22
+ RESET: '\x1b[0m',
23
+ DEBUG: '\x1b[36m', // Cyan
24
+ INFO: '\x1b[32m', // Green
25
+ WARN: '\x1b[33m', // Yellow
26
+ ERROR: '\x1b[31m', // Red
27
+ };
28
+
29
+ private static readonly LEVEL_NAMES = {
30
+ [LogLevel.DEBUG]: 'DEBUG',
31
+ [LogLevel.INFO]: 'INFO',
32
+ [LogLevel.WARN]: 'WARN',
33
+ [LogLevel.ERROR]: 'ERROR',
34
+ };
35
+
36
+ constructor(moduleName: string = 'embedbox', level: LogLevel = LogLevel.INFO) {
37
+ this.moduleName = moduleName;
38
+ this.currentLevel = level;
39
+ }
40
+
41
+ static getInstance(moduleName?: string, level?: LogLevel): Logger {
42
+ if (!Logger.instance) {
43
+ Logger.instance = new Logger(moduleName || 'embedbox', level);
44
+ }
45
+ return Logger.instance;
46
+ }
47
+
48
+ setLevel(level: LogLevel): void {
49
+ this.currentLevel = level;
50
+ }
51
+
52
+ getLevel(): LogLevel {
53
+ return this.currentLevel;
54
+ }
55
+
56
+ private formatMessage(level: LogLevel, message: string): string {
57
+ const levelName = Logger.LEVEL_NAMES[level];
58
+ const color = Logger.COLORS[levelName as keyof typeof Logger.COLORS];
59
+ const reset = Logger.COLORS.RESET;
60
+
61
+ return `${color}[${levelName}(${this.moduleName})]${reset} ${message}\n\n`;
62
+ }
63
+
64
+ private log(level: LogLevel, message: string): void {
65
+ if (level < this.currentLevel) {
66
+ return;
67
+ }
68
+
69
+ const formattedMessage = this.formatMessage(level, message);
70
+ process.stdout.write(formattedMessage);
71
+ }
72
+
73
+ debug(message: string): void {
74
+ this.log(LogLevel.DEBUG, message);
75
+ }
76
+
77
+ info(message: string): void {
78
+ this.log(LogLevel.INFO, message);
79
+ }
80
+
81
+ warn(message: string): void {
82
+ this.log(LogLevel.WARN, message);
83
+ }
84
+
85
+ error(message: string): void {
86
+ this.log(LogLevel.ERROR, message);
87
+ }
88
+
89
+ // Static methods for quick access
90
+ static debug(message: string, moduleName?: string): void {
91
+ const logger = new Logger(moduleName || 'embedbox');
92
+ logger.debug(message);
93
+ }
94
+
95
+ static info(message: string, moduleName?: string): void {
96
+ const logger = new Logger(moduleName || 'embedbox');
97
+ logger.info(message);
98
+ }
99
+
100
+ static warn(message: string, moduleName?: string): void {
101
+ const logger = new Logger(moduleName || 'embedbox');
102
+ logger.warn(message);
103
+ }
104
+
105
+ static error(message: string, moduleName?: string): void {
106
+ const logger = new Logger(moduleName || 'embedbox');
107
+ logger.error(message);
108
+ }
109
+
110
+ // Method to create a logger instance for a specific module
111
+ static createModuleLogger(moduleName: string, level?: LogLevel): Logger {
112
+ return new Logger(`embedbox:${moduleName}`, level);
113
+ }
114
+ }
115
+
116
+ // Export a default logger instance
117
+ export const logger = Logger.getInstance();
118
+
119
+ // Export convenience functions
120
+ export const log = {
121
+ debug: (message: string, moduleName?: string) => Logger.debug(message, moduleName),
122
+ info: (message: string, moduleName?: string) => Logger.info(message, moduleName),
123
+ warn: (message: string, moduleName?: string) => Logger.warn(message, moduleName),
124
+ error: (message: string, moduleName?: string) => Logger.error(message, moduleName),
125
+ };