@carno.js/logger 1.0.2 → 1.0.3
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/LICENSE +21 -674
- package/package.json +2 -2
- package/src/LoggerPlugin.ts +56 -56
- package/src/LoggerService.ts +294 -294
- package/src/index.ts +3 -3
package/src/LoggerService.ts
CHANGED
|
@@ -1,294 +1,294 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Log levels.
|
|
3
|
-
*/
|
|
4
|
-
export enum LogLevel {
|
|
5
|
-
DEBUG = 0,
|
|
6
|
-
INFO = 1,
|
|
7
|
-
WARN = 2,
|
|
8
|
-
ERROR = 3,
|
|
9
|
-
FATAL = 4,
|
|
10
|
-
SILENT = 5
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Logger configuration.
|
|
15
|
-
*/
|
|
16
|
-
export interface LoggerConfig {
|
|
17
|
-
/** Minimum log level to output */
|
|
18
|
-
level?: LogLevel | keyof typeof LogLevel;
|
|
19
|
-
|
|
20
|
-
/** Pretty print structured data */
|
|
21
|
-
pretty?: boolean;
|
|
22
|
-
|
|
23
|
-
/** Include timestamp */
|
|
24
|
-
timestamp?: boolean;
|
|
25
|
-
|
|
26
|
-
/** Custom timestamp format function */
|
|
27
|
-
timestampFormat?: () => string;
|
|
28
|
-
|
|
29
|
-
/** Prefix for all messages */
|
|
30
|
-
prefix?: string;
|
|
31
|
-
|
|
32
|
-
/** Async buffer flush interval (ms). 0 = sync */
|
|
33
|
-
flushInterval?: number;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Structured log data.
|
|
38
|
-
*/
|
|
39
|
-
export type LogData = Record<string, any>;
|
|
40
|
-
|
|
41
|
-
// ANSI color codes for beautiful output
|
|
42
|
-
const COLORS = {
|
|
43
|
-
reset: '\x1b[0m',
|
|
44
|
-
bold: '\x1b[1m',
|
|
45
|
-
dim: '\x1b[2m',
|
|
46
|
-
|
|
47
|
-
// Levels
|
|
48
|
-
debug: '\x1b[36m', // Cyan
|
|
49
|
-
info: '\x1b[32m', // Green
|
|
50
|
-
warn: '\x1b[33m', // Yellow
|
|
51
|
-
error: '\x1b[31m', // Red
|
|
52
|
-
fatal: '\x1b[35m', // Magenta
|
|
53
|
-
|
|
54
|
-
// Data
|
|
55
|
-
key: '\x1b[90m', // Gray
|
|
56
|
-
string: '\x1b[33m', // Yellow
|
|
57
|
-
number: '\x1b[36m', // Cyan
|
|
58
|
-
boolean: '\x1b[35m', // Magenta
|
|
59
|
-
null: '\x1b[90m', // Gray
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const LEVEL_LABELS: Record<LogLevel, string> = {
|
|
63
|
-
[LogLevel.DEBUG]: `${COLORS.debug}${COLORS.bold}DEBUG${COLORS.reset}`,
|
|
64
|
-
[LogLevel.INFO]: `${COLORS.info}${COLORS.bold}INFO${COLORS.reset} `,
|
|
65
|
-
[LogLevel.WARN]: `${COLORS.warn}${COLORS.bold}WARN${COLORS.reset} `,
|
|
66
|
-
[LogLevel.ERROR]: `${COLORS.error}${COLORS.bold}ERROR${COLORS.reset}`,
|
|
67
|
-
[LogLevel.FATAL]: `${COLORS.fatal}${COLORS.bold}FATAL${COLORS.reset}`,
|
|
68
|
-
[LogLevel.SILENT]: '',
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const LEVEL_ICONS: Record<LogLevel, string> = {
|
|
72
|
-
[LogLevel.DEBUG]: '🔍',
|
|
73
|
-
[LogLevel.INFO]: '✨',
|
|
74
|
-
[LogLevel.WARN]: '⚠️ ',
|
|
75
|
-
[LogLevel.ERROR]: '❌',
|
|
76
|
-
[LogLevel.FATAL]: '💀',
|
|
77
|
-
[LogLevel.SILENT]: '',
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* High-performance async logger.
|
|
82
|
-
*
|
|
83
|
-
* Features:
|
|
84
|
-
* - Async buffered output (like Pino)
|
|
85
|
-
* - Structured JSON data support
|
|
86
|
-
* - Beautiful colorized output
|
|
87
|
-
* - Zero-allocation hot path
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* ```typescript
|
|
91
|
-
* const logger = new LoggerService({ level: LogLevel.DEBUG });
|
|
92
|
-
*
|
|
93
|
-
* logger.info('User logged in', { userId: 123, ip: '192.168.1.1' });
|
|
94
|
-
* logger.error('Database error', { error: err.message, query: sql });
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
export class LoggerService {
|
|
98
|
-
private level: LogLevel;
|
|
99
|
-
private pretty: boolean;
|
|
100
|
-
private timestamp: boolean;
|
|
101
|
-
private timestampFormat: () => string;
|
|
102
|
-
private prefix: string;
|
|
103
|
-
private buffer: string[] = [];
|
|
104
|
-
private flushTimer: Timer | null = null;
|
|
105
|
-
private flushInterval: number;
|
|
106
|
-
|
|
107
|
-
constructor(config: LoggerConfig = {}) {
|
|
108
|
-
this.level = this.parseLevel(config.level ?? LogLevel.INFO);
|
|
109
|
-
this.pretty = config.pretty ?? true;
|
|
110
|
-
this.timestamp = config.timestamp ?? true;
|
|
111
|
-
this.timestampFormat = config.timestampFormat ?? this.defaultTimestamp;
|
|
112
|
-
this.prefix = config.prefix ?? '';
|
|
113
|
-
this.flushInterval = config.flushInterval ?? 10;
|
|
114
|
-
|
|
115
|
-
if (this.flushInterval > 0) {
|
|
116
|
-
this.flushTimer = setInterval(() => this.flush(), this.flushInterval);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Debug level log.
|
|
122
|
-
*/
|
|
123
|
-
debug(message: string, data?: LogData): void {
|
|
124
|
-
this.log(LogLevel.DEBUG, message, data);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Info level log.
|
|
129
|
-
*/
|
|
130
|
-
info(message: string, data?: LogData): void {
|
|
131
|
-
this.log(LogLevel.INFO, message, data);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Warning level log.
|
|
136
|
-
*/
|
|
137
|
-
warn(message: string, data?: LogData): void {
|
|
138
|
-
this.log(LogLevel.WARN, message, data);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Error level log.
|
|
143
|
-
*/
|
|
144
|
-
error(message: string, data?: LogData): void {
|
|
145
|
-
this.log(LogLevel.ERROR, message, data);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Fatal level log (will also flush immediately).
|
|
150
|
-
*/
|
|
151
|
-
fatal(message: string, data?: LogData): void {
|
|
152
|
-
this.log(LogLevel.FATAL, message, data);
|
|
153
|
-
this.flush(); // Flush immediately for fatal
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Log with custom level.
|
|
158
|
-
*/
|
|
159
|
-
log(level: LogLevel, message: string, data?: LogData): void {
|
|
160
|
-
if (level < this.level) return;
|
|
161
|
-
|
|
162
|
-
const line = this.formatLine(level, message, data);
|
|
163
|
-
|
|
164
|
-
if (this.flushInterval === 0) {
|
|
165
|
-
// Sync mode
|
|
166
|
-
this.write(line, level);
|
|
167
|
-
} else {
|
|
168
|
-
// Async mode
|
|
169
|
-
this.buffer.push(line);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Set minimum log level.
|
|
175
|
-
*/
|
|
176
|
-
setLevel(level: LogLevel | keyof typeof LogLevel): void {
|
|
177
|
-
this.level = this.parseLevel(level);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Flush buffered logs.
|
|
182
|
-
*/
|
|
183
|
-
flush(): void {
|
|
184
|
-
if (this.buffer.length === 0) return;
|
|
185
|
-
|
|
186
|
-
const output = this.buffer.join('\n');
|
|
187
|
-
this.buffer = [];
|
|
188
|
-
|
|
189
|
-
process.stdout.write(output + '\n');
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Close the logger (flush and stop timer).
|
|
194
|
-
*/
|
|
195
|
-
close(): void {
|
|
196
|
-
this.flush();
|
|
197
|
-
if (this.flushTimer) {
|
|
198
|
-
clearInterval(this.flushTimer);
|
|
199
|
-
this.flushTimer = null;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
private write(line: string, level: LogLevel): void {
|
|
204
|
-
if (level >= LogLevel.ERROR) {
|
|
205
|
-
process.stderr.write(line + '\n');
|
|
206
|
-
} else {
|
|
207
|
-
process.stdout.write(line + '\n');
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
private formatLine(level: LogLevel, message: string, data?: LogData): string {
|
|
212
|
-
const parts: string[] = [];
|
|
213
|
-
|
|
214
|
-
// Icon
|
|
215
|
-
parts.push(LEVEL_ICONS[level]);
|
|
216
|
-
|
|
217
|
-
// Timestamp
|
|
218
|
-
if (this.timestamp) {
|
|
219
|
-
parts.push(`${COLORS.dim}${this.timestampFormat()}${COLORS.reset}`);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Level label
|
|
223
|
-
parts.push(LEVEL_LABELS[level]);
|
|
224
|
-
|
|
225
|
-
// Prefix
|
|
226
|
-
if (this.prefix) {
|
|
227
|
-
parts.push(`${COLORS.bold}[${this.prefix}]${COLORS.reset}`);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Message
|
|
231
|
-
parts.push(message);
|
|
232
|
-
|
|
233
|
-
// Data
|
|
234
|
-
if (data && Object.keys(data).length > 0) {
|
|
235
|
-
if (this.pretty) {
|
|
236
|
-
parts.push(this.formatDataPretty(data));
|
|
237
|
-
} else {
|
|
238
|
-
parts.push(`${COLORS.dim}${JSON.stringify(data)}${COLORS.reset}`);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return parts.join(' ');
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
private formatDataPretty(data: LogData): string {
|
|
246
|
-
const formatted: string[] = [];
|
|
247
|
-
|
|
248
|
-
for (const [key, value] of Object.entries(data)) {
|
|
249
|
-
const formattedValue = this.formatValue(value);
|
|
250
|
-
formatted.push(`${COLORS.key}${key}=${COLORS.reset}${formattedValue}`);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return formatted.join(' ');
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
private formatValue(value: any): string {
|
|
257
|
-
if (value === null) {
|
|
258
|
-
return `${COLORS.null}null${COLORS.reset}`;
|
|
259
|
-
}
|
|
260
|
-
if (value === undefined) {
|
|
261
|
-
return `${COLORS.null}undefined${COLORS.reset}`;
|
|
262
|
-
}
|
|
263
|
-
if (typeof value === 'string') {
|
|
264
|
-
return `${COLORS.string}"${value}"${COLORS.reset}`;
|
|
265
|
-
}
|
|
266
|
-
if (typeof value === 'number') {
|
|
267
|
-
return `${COLORS.number}${value}${COLORS.reset}`;
|
|
268
|
-
}
|
|
269
|
-
if (typeof value === 'boolean') {
|
|
270
|
-
return `${COLORS.boolean}${value}${COLORS.reset}`;
|
|
271
|
-
}
|
|
272
|
-
if (Array.isArray(value)) {
|
|
273
|
-
return `${COLORS.dim}[${value.length} items]${COLORS.reset}`;
|
|
274
|
-
}
|
|
275
|
-
if (typeof value === 'object') {
|
|
276
|
-
return `${COLORS.dim}{${Object.keys(value).length} keys}${COLORS.reset}`;
|
|
277
|
-
}
|
|
278
|
-
return String(value);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
private defaultTimestamp(): string {
|
|
282
|
-
const now = new Date();
|
|
283
|
-
const h = String(now.getHours()).padStart(2, '0');
|
|
284
|
-
const m = String(now.getMinutes()).padStart(2, '0');
|
|
285
|
-
const s = String(now.getSeconds()).padStart(2, '0');
|
|
286
|
-
const ms = String(now.getMilliseconds()).padStart(3, '0');
|
|
287
|
-
return `${h}:${m}:${s}.${ms}`;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
private parseLevel(level: LogLevel | keyof typeof LogLevel): LogLevel {
|
|
291
|
-
if (typeof level === 'number') return level;
|
|
292
|
-
return LogLevel[level] ?? LogLevel.INFO;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Log levels.
|
|
3
|
+
*/
|
|
4
|
+
export enum LogLevel {
|
|
5
|
+
DEBUG = 0,
|
|
6
|
+
INFO = 1,
|
|
7
|
+
WARN = 2,
|
|
8
|
+
ERROR = 3,
|
|
9
|
+
FATAL = 4,
|
|
10
|
+
SILENT = 5
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Logger configuration.
|
|
15
|
+
*/
|
|
16
|
+
export interface LoggerConfig {
|
|
17
|
+
/** Minimum log level to output */
|
|
18
|
+
level?: LogLevel | keyof typeof LogLevel;
|
|
19
|
+
|
|
20
|
+
/** Pretty print structured data */
|
|
21
|
+
pretty?: boolean;
|
|
22
|
+
|
|
23
|
+
/** Include timestamp */
|
|
24
|
+
timestamp?: boolean;
|
|
25
|
+
|
|
26
|
+
/** Custom timestamp format function */
|
|
27
|
+
timestampFormat?: () => string;
|
|
28
|
+
|
|
29
|
+
/** Prefix for all messages */
|
|
30
|
+
prefix?: string;
|
|
31
|
+
|
|
32
|
+
/** Async buffer flush interval (ms). 0 = sync */
|
|
33
|
+
flushInterval?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Structured log data.
|
|
38
|
+
*/
|
|
39
|
+
export type LogData = Record<string, any>;
|
|
40
|
+
|
|
41
|
+
// ANSI color codes for beautiful output
|
|
42
|
+
const COLORS = {
|
|
43
|
+
reset: '\x1b[0m',
|
|
44
|
+
bold: '\x1b[1m',
|
|
45
|
+
dim: '\x1b[2m',
|
|
46
|
+
|
|
47
|
+
// Levels
|
|
48
|
+
debug: '\x1b[36m', // Cyan
|
|
49
|
+
info: '\x1b[32m', // Green
|
|
50
|
+
warn: '\x1b[33m', // Yellow
|
|
51
|
+
error: '\x1b[31m', // Red
|
|
52
|
+
fatal: '\x1b[35m', // Magenta
|
|
53
|
+
|
|
54
|
+
// Data
|
|
55
|
+
key: '\x1b[90m', // Gray
|
|
56
|
+
string: '\x1b[33m', // Yellow
|
|
57
|
+
number: '\x1b[36m', // Cyan
|
|
58
|
+
boolean: '\x1b[35m', // Magenta
|
|
59
|
+
null: '\x1b[90m', // Gray
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const LEVEL_LABELS: Record<LogLevel, string> = {
|
|
63
|
+
[LogLevel.DEBUG]: `${COLORS.debug}${COLORS.bold}DEBUG${COLORS.reset}`,
|
|
64
|
+
[LogLevel.INFO]: `${COLORS.info}${COLORS.bold}INFO${COLORS.reset} `,
|
|
65
|
+
[LogLevel.WARN]: `${COLORS.warn}${COLORS.bold}WARN${COLORS.reset} `,
|
|
66
|
+
[LogLevel.ERROR]: `${COLORS.error}${COLORS.bold}ERROR${COLORS.reset}`,
|
|
67
|
+
[LogLevel.FATAL]: `${COLORS.fatal}${COLORS.bold}FATAL${COLORS.reset}`,
|
|
68
|
+
[LogLevel.SILENT]: '',
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const LEVEL_ICONS: Record<LogLevel, string> = {
|
|
72
|
+
[LogLevel.DEBUG]: '🔍',
|
|
73
|
+
[LogLevel.INFO]: '✨',
|
|
74
|
+
[LogLevel.WARN]: '⚠️ ',
|
|
75
|
+
[LogLevel.ERROR]: '❌',
|
|
76
|
+
[LogLevel.FATAL]: '💀',
|
|
77
|
+
[LogLevel.SILENT]: '',
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* High-performance async logger.
|
|
82
|
+
*
|
|
83
|
+
* Features:
|
|
84
|
+
* - Async buffered output (like Pino)
|
|
85
|
+
* - Structured JSON data support
|
|
86
|
+
* - Beautiful colorized output
|
|
87
|
+
* - Zero-allocation hot path
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const logger = new LoggerService({ level: LogLevel.DEBUG });
|
|
92
|
+
*
|
|
93
|
+
* logger.info('User logged in', { userId: 123, ip: '192.168.1.1' });
|
|
94
|
+
* logger.error('Database error', { error: err.message, query: sql });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export class LoggerService {
|
|
98
|
+
private level: LogLevel;
|
|
99
|
+
private pretty: boolean;
|
|
100
|
+
private timestamp: boolean;
|
|
101
|
+
private timestampFormat: () => string;
|
|
102
|
+
private prefix: string;
|
|
103
|
+
private buffer: string[] = [];
|
|
104
|
+
private flushTimer: Timer | null = null;
|
|
105
|
+
private flushInterval: number;
|
|
106
|
+
|
|
107
|
+
constructor(config: LoggerConfig = {}) {
|
|
108
|
+
this.level = this.parseLevel(config.level ?? LogLevel.INFO);
|
|
109
|
+
this.pretty = config.pretty ?? true;
|
|
110
|
+
this.timestamp = config.timestamp ?? true;
|
|
111
|
+
this.timestampFormat = config.timestampFormat ?? this.defaultTimestamp;
|
|
112
|
+
this.prefix = config.prefix ?? '';
|
|
113
|
+
this.flushInterval = config.flushInterval ?? 10;
|
|
114
|
+
|
|
115
|
+
if (this.flushInterval > 0) {
|
|
116
|
+
this.flushTimer = setInterval(() => this.flush(), this.flushInterval);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Debug level log.
|
|
122
|
+
*/
|
|
123
|
+
debug(message: string, data?: LogData): void {
|
|
124
|
+
this.log(LogLevel.DEBUG, message, data);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Info level log.
|
|
129
|
+
*/
|
|
130
|
+
info(message: string, data?: LogData): void {
|
|
131
|
+
this.log(LogLevel.INFO, message, data);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Warning level log.
|
|
136
|
+
*/
|
|
137
|
+
warn(message: string, data?: LogData): void {
|
|
138
|
+
this.log(LogLevel.WARN, message, data);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Error level log.
|
|
143
|
+
*/
|
|
144
|
+
error(message: string, data?: LogData): void {
|
|
145
|
+
this.log(LogLevel.ERROR, message, data);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Fatal level log (will also flush immediately).
|
|
150
|
+
*/
|
|
151
|
+
fatal(message: string, data?: LogData): void {
|
|
152
|
+
this.log(LogLevel.FATAL, message, data);
|
|
153
|
+
this.flush(); // Flush immediately for fatal
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Log with custom level.
|
|
158
|
+
*/
|
|
159
|
+
log(level: LogLevel, message: string, data?: LogData): void {
|
|
160
|
+
if (level < this.level) return;
|
|
161
|
+
|
|
162
|
+
const line = this.formatLine(level, message, data);
|
|
163
|
+
|
|
164
|
+
if (this.flushInterval === 0) {
|
|
165
|
+
// Sync mode
|
|
166
|
+
this.write(line, level);
|
|
167
|
+
} else {
|
|
168
|
+
// Async mode
|
|
169
|
+
this.buffer.push(line);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Set minimum log level.
|
|
175
|
+
*/
|
|
176
|
+
setLevel(level: LogLevel | keyof typeof LogLevel): void {
|
|
177
|
+
this.level = this.parseLevel(level);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Flush buffered logs.
|
|
182
|
+
*/
|
|
183
|
+
flush(): void {
|
|
184
|
+
if (this.buffer.length === 0) return;
|
|
185
|
+
|
|
186
|
+
const output = this.buffer.join('\n');
|
|
187
|
+
this.buffer = [];
|
|
188
|
+
|
|
189
|
+
process.stdout.write(output + '\n');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Close the logger (flush and stop timer).
|
|
194
|
+
*/
|
|
195
|
+
close(): void {
|
|
196
|
+
this.flush();
|
|
197
|
+
if (this.flushTimer) {
|
|
198
|
+
clearInterval(this.flushTimer);
|
|
199
|
+
this.flushTimer = null;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private write(line: string, level: LogLevel): void {
|
|
204
|
+
if (level >= LogLevel.ERROR) {
|
|
205
|
+
process.stderr.write(line + '\n');
|
|
206
|
+
} else {
|
|
207
|
+
process.stdout.write(line + '\n');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private formatLine(level: LogLevel, message: string, data?: LogData): string {
|
|
212
|
+
const parts: string[] = [];
|
|
213
|
+
|
|
214
|
+
// Icon
|
|
215
|
+
parts.push(LEVEL_ICONS[level]);
|
|
216
|
+
|
|
217
|
+
// Timestamp
|
|
218
|
+
if (this.timestamp) {
|
|
219
|
+
parts.push(`${COLORS.dim}${this.timestampFormat()}${COLORS.reset}`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Level label
|
|
223
|
+
parts.push(LEVEL_LABELS[level]);
|
|
224
|
+
|
|
225
|
+
// Prefix
|
|
226
|
+
if (this.prefix) {
|
|
227
|
+
parts.push(`${COLORS.bold}[${this.prefix}]${COLORS.reset}`);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Message
|
|
231
|
+
parts.push(message);
|
|
232
|
+
|
|
233
|
+
// Data
|
|
234
|
+
if (data && Object.keys(data).length > 0) {
|
|
235
|
+
if (this.pretty) {
|
|
236
|
+
parts.push(this.formatDataPretty(data));
|
|
237
|
+
} else {
|
|
238
|
+
parts.push(`${COLORS.dim}${JSON.stringify(data)}${COLORS.reset}`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return parts.join(' ');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private formatDataPretty(data: LogData): string {
|
|
246
|
+
const formatted: string[] = [];
|
|
247
|
+
|
|
248
|
+
for (const [key, value] of Object.entries(data)) {
|
|
249
|
+
const formattedValue = this.formatValue(value);
|
|
250
|
+
formatted.push(`${COLORS.key}${key}=${COLORS.reset}${formattedValue}`);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return formatted.join(' ');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private formatValue(value: any): string {
|
|
257
|
+
if (value === null) {
|
|
258
|
+
return `${COLORS.null}null${COLORS.reset}`;
|
|
259
|
+
}
|
|
260
|
+
if (value === undefined) {
|
|
261
|
+
return `${COLORS.null}undefined${COLORS.reset}`;
|
|
262
|
+
}
|
|
263
|
+
if (typeof value === 'string') {
|
|
264
|
+
return `${COLORS.string}"${value}"${COLORS.reset}`;
|
|
265
|
+
}
|
|
266
|
+
if (typeof value === 'number') {
|
|
267
|
+
return `${COLORS.number}${value}${COLORS.reset}`;
|
|
268
|
+
}
|
|
269
|
+
if (typeof value === 'boolean') {
|
|
270
|
+
return `${COLORS.boolean}${value}${COLORS.reset}`;
|
|
271
|
+
}
|
|
272
|
+
if (Array.isArray(value)) {
|
|
273
|
+
return `${COLORS.dim}[${value.length} items]${COLORS.reset}`;
|
|
274
|
+
}
|
|
275
|
+
if (typeof value === 'object') {
|
|
276
|
+
return `${COLORS.dim}{${Object.keys(value).length} keys}${COLORS.reset}`;
|
|
277
|
+
}
|
|
278
|
+
return String(value);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private defaultTimestamp(): string {
|
|
282
|
+
const now = new Date();
|
|
283
|
+
const h = String(now.getHours()).padStart(2, '0');
|
|
284
|
+
const m = String(now.getMinutes()).padStart(2, '0');
|
|
285
|
+
const s = String(now.getSeconds()).padStart(2, '0');
|
|
286
|
+
const ms = String(now.getMilliseconds()).padStart(3, '0');
|
|
287
|
+
return `${h}:${m}:${s}.${ms}`;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private parseLevel(level: LogLevel | keyof typeof LogLevel): LogLevel {
|
|
291
|
+
if (typeof level === 'number') return level;
|
|
292
|
+
return LogLevel[level] ?? LogLevel.INFO;
|
|
293
|
+
}
|
|
294
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { LoggerService, LogLevel, type LoggerConfig, type LogData } from './LoggerService';
|
|
2
|
-
|
|
3
|
-
export { createCarnoLogger, CarnoLogger, createLogger, type LoggerPluginConfig } from './LoggerPlugin';
|
|
1
|
+
export { LoggerService, LogLevel, type LoggerConfig, type LogData } from './LoggerService';
|
|
2
|
+
|
|
3
|
+
export { createCarnoLogger, CarnoLogger, createLogger, type LoggerPluginConfig } from './LoggerPlugin';
|