doc2vec 1.0.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.
package/dist/logger.js ADDED
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ /**
3
+ * Enhanced Logger for structured and consistent logging
4
+ * Compatible with CommonJS and ESM environments
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.defaultLogger = exports.LogLevel = exports.Logger = void 0;
8
+ /**
9
+ * Logger levels with their corresponding numeric values
10
+ */
11
+ var LogLevel;
12
+ (function (LogLevel) {
13
+ LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
14
+ LogLevel[LogLevel["INFO"] = 1] = "INFO";
15
+ LogLevel[LogLevel["WARN"] = 2] = "WARN";
16
+ LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
17
+ LogLevel[LogLevel["NONE"] = 100] = "NONE";
18
+ })(LogLevel || (exports.LogLevel = LogLevel = {}));
19
+ /**
20
+ * Basic color functions that don't rely on external packages
21
+ */
22
+ const colors = {
23
+ gray: (text) => `\x1b[90m${text}\x1b[0m`,
24
+ blue: (text) => `\x1b[34m${text}\x1b[0m`,
25
+ yellow: (text) => `\x1b[33m${text}\x1b[0m`,
26
+ red: (text) => `\x1b[31m${text}\x1b[0m`,
27
+ green: (text) => `\x1b[32m${text}\x1b[0m`,
28
+ reset: (text) => `\x1b[0m${text}\x1b[0m`
29
+ };
30
+ /**
31
+ * Enhanced Logger class with color-coding, timestamp, and formatting
32
+ */
33
+ class Logger {
34
+ /**
35
+ * Create a new Logger instance
36
+ *
37
+ * @param moduleName Name of the module using this logger
38
+ * @param config Logger configuration options
39
+ */
40
+ constructor(moduleName, config) {
41
+ this.moduleName = moduleName;
42
+ this.config = {
43
+ level: LogLevel.INFO,
44
+ useTimestamp: true,
45
+ useColor: true,
46
+ prettyPrint: true,
47
+ ...config
48
+ };
49
+ }
50
+ /**
51
+ * Format a log message with timestamp, level, and module information
52
+ *
53
+ * @param level Log level for this message
54
+ * @param message The message to log
55
+ * @param args Additional arguments to include
56
+ * @returns Formatted log message
57
+ */
58
+ formatMessage(level, message, args = []) {
59
+ const timestamp = this.config.useTimestamp ?
60
+ `[${new Date().toISOString()}] ` : '';
61
+ const modulePrefix = this.moduleName ?
62
+ `[${this.moduleName}] ` : '';
63
+ const levelFormatted = `[${level.padEnd(5)}]`;
64
+ const formattedMessage = `${timestamp}${levelFormatted} ${modulePrefix}${message}`;
65
+ if (args.length > 0) {
66
+ if (this.config.prettyPrint) {
67
+ return `${formattedMessage}\n${args.map(arg => typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg).join('\n')}`;
68
+ }
69
+ else {
70
+ return `${formattedMessage} ${args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : arg).join(' ')}`;
71
+ }
72
+ }
73
+ return formattedMessage;
74
+ }
75
+ /**
76
+ * Apply color to a message based on log level
77
+ *
78
+ * @param level Log level
79
+ * @param message Message to color
80
+ * @returns Colored message
81
+ */
82
+ colorize(level, message) {
83
+ if (!this.config.useColor)
84
+ return message;
85
+ switch (level) {
86
+ case LogLevel.DEBUG:
87
+ return colors.gray(message);
88
+ case LogLevel.INFO:
89
+ return colors.blue(message);
90
+ case LogLevel.WARN:
91
+ return colors.yellow(message);
92
+ case LogLevel.ERROR:
93
+ return colors.red(message);
94
+ default:
95
+ return message;
96
+ }
97
+ }
98
+ /**
99
+ * Log a debug message
100
+ *
101
+ * @param message Message to log
102
+ * @param args Additional arguments
103
+ */
104
+ debug(message, ...args) {
105
+ if (this.config.level <= LogLevel.DEBUG) {
106
+ const formattedMessage = this.formatMessage('DEBUG', message, args);
107
+ console.log(this.colorize(LogLevel.DEBUG, formattedMessage));
108
+ }
109
+ }
110
+ /**
111
+ * Log an info message
112
+ *
113
+ * @param message Message to log
114
+ * @param args Additional arguments
115
+ */
116
+ info(message, ...args) {
117
+ if (this.config.level <= LogLevel.INFO) {
118
+ const formattedMessage = this.formatMessage('INFO', message, args);
119
+ console.log(this.colorize(LogLevel.INFO, formattedMessage));
120
+ }
121
+ }
122
+ /**
123
+ * Log a warning message
124
+ *
125
+ * @param message Message to log
126
+ * @param args Additional arguments
127
+ */
128
+ warn(message, ...args) {
129
+ if (this.config.level <= LogLevel.WARN) {
130
+ const formattedMessage = this.formatMessage('WARN', message, args);
131
+ console.warn(this.colorize(LogLevel.WARN, formattedMessage));
132
+ }
133
+ }
134
+ /**
135
+ * Log an error message
136
+ *
137
+ * @param message Message to log
138
+ * @param args Additional arguments
139
+ */
140
+ error(message, ...args) {
141
+ if (this.config.level <= LogLevel.ERROR) {
142
+ const formattedMessage = this.formatMessage('ERROR', message, args);
143
+ console.error(this.colorize(LogLevel.ERROR, formattedMessage));
144
+ }
145
+ }
146
+ /**
147
+ * Create a child logger with a more specific module name
148
+ *
149
+ * @param subModule Name of the sub-module
150
+ * @returns New logger instance
151
+ */
152
+ child(subModule) {
153
+ return new Logger(`${this.moduleName}:${subModule}`, this.config);
154
+ }
155
+ /**
156
+ * Format a section header to clearly separate logical parts of execution
157
+ *
158
+ * @param title Section title
159
+ * @returns Logger instance for chaining
160
+ */
161
+ section(title) {
162
+ if (this.config.level <= LogLevel.INFO) {
163
+ const separator = '='.repeat(Math.max(80 - title.length - 4, 10));
164
+ const message = `${separator} ${title} ${separator}`;
165
+ console.log(this.colorize(LogLevel.INFO, message));
166
+ }
167
+ return this;
168
+ }
169
+ /**
170
+ * Create a progress indicator
171
+ *
172
+ * @param title Title of the operation
173
+ * @param total Total number of items to process
174
+ * @returns Object with update and complete methods
175
+ */
176
+ progress(title, total) {
177
+ let current = 0;
178
+ const startTime = Date.now();
179
+ const update = (increment = 1, message) => {
180
+ if (this.config.level > LogLevel.INFO)
181
+ return;
182
+ current += increment;
183
+ const percentage = Math.min(Math.floor((current / total) * 100), 100);
184
+ const elapsed = (Date.now() - startTime) / 1000;
185
+ let rate = current / elapsed;
186
+ let timeRemaining = '';
187
+ if (rate > 0 && current < total) {
188
+ const remainingSecs = (total - current) / rate;
189
+ timeRemaining = `, ETA: ${Math.floor(remainingSecs / 60)}m ${Math.floor(remainingSecs % 60)}s`;
190
+ }
191
+ const progressBar = this.createProgressBar(percentage);
192
+ const statusMsg = message ? ` - ${message}` : '';
193
+ console.log(this.colorize(LogLevel.INFO, this.formatMessage('INFO', `${title}: ${progressBar} ${percentage}% (${current}/${total}${timeRemaining})${statusMsg}`)));
194
+ };
195
+ const complete = (message = 'Completed') => {
196
+ if (this.config.level > LogLevel.INFO)
197
+ return;
198
+ const elapsed = (Date.now() - startTime) / 1000;
199
+ const rate = total / elapsed;
200
+ console.log(this.colorize(LogLevel.INFO, this.formatMessage('INFO', `${title}: ${this.createProgressBar(100)} 100% (${total}/${total}) - ${message} in ${elapsed.toFixed(2)}s (${rate.toFixed(2)} items/sec)`)));
201
+ };
202
+ return { update, complete };
203
+ }
204
+ /**
205
+ * Create a visual progress bar
206
+ *
207
+ * @param percentage Completion percentage
208
+ * @returns Visual progress bar
209
+ */
210
+ createProgressBar(percentage) {
211
+ const width = 20;
212
+ const completeChars = Math.floor((percentage / 100) * width);
213
+ const incompleteChars = width - completeChars;
214
+ let bar = '[';
215
+ if (this.config.useColor) {
216
+ bar += colors.green('='.repeat(completeChars));
217
+ bar += ' '.repeat(incompleteChars);
218
+ }
219
+ else {
220
+ bar += '='.repeat(completeChars);
221
+ bar += ' '.repeat(incompleteChars);
222
+ }
223
+ bar += ']';
224
+ return bar;
225
+ }
226
+ }
227
+ exports.Logger = Logger;
228
+ // Create a default logger instance
229
+ const defaultLogger = new Logger('app');
230
+ exports.defaultLogger = defaultLogger;