@carno.js/logger 1.1.1 → 1.1.2
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 -21
- package/package.json +2 -2
- package/src/LoggerPlugin.ts +56 -56
- package/src/LoggerService.ts +305 -305
- package/src/index.ts +3 -3
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 carno.js
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 carno.js
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carno.js/logger",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "High-performance async logger for carno.js",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
"publishConfig": {
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "4239bdaf06a9c741e69639e996d91b8852ae3cd1"
|
|
26
26
|
}
|
package/src/LoggerPlugin.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { Carno } from '@carno.js/core';
|
|
2
|
-
import { LoggerService, LoggerConfig, LogLevel } from './LoggerService';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Logger plugin configuration.
|
|
6
|
-
*/
|
|
7
|
-
export interface LoggerPluginConfig extends LoggerConfig {
|
|
8
|
-
/** Auto-register as singleton in DI */
|
|
9
|
-
autoRegister?: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Create a logger plugin for Turbo.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* const app = new Turbo();
|
|
18
|
-
* app.use(createLoggerPlugin({ level: LogLevel.DEBUG }));
|
|
19
|
-
*
|
|
20
|
-
* // Now LoggerService is available for injection
|
|
21
|
-
* @Controller()
|
|
22
|
-
* class MyController {
|
|
23
|
-
* constructor(private logger: LoggerService) {}
|
|
24
|
-
*
|
|
25
|
-
* @Get('/')
|
|
26
|
-
* index() {
|
|
27
|
-
* this.logger.info('Request received', { path: '/' });
|
|
28
|
-
* return 'Hello';
|
|
29
|
-
* }
|
|
30
|
-
* }
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export function createCarnoLogger(config: LoggerPluginConfig = {}) {
|
|
34
|
-
const logger = new LoggerService(config);
|
|
35
|
-
return new Carno()
|
|
36
|
-
.services([
|
|
37
|
-
{
|
|
38
|
-
token: LoggerService,
|
|
39
|
-
useValue: logger
|
|
40
|
-
}
|
|
41
|
-
])
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export const CarnoLogger = createCarnoLogger();
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Create a standalone logger (without Turbo).
|
|
48
|
-
*/
|
|
49
|
-
export function createLogger(config: LoggerConfig = {}): LoggerService {
|
|
50
|
-
return new LoggerService(config);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Re-export everything
|
|
54
|
-
export { LoggerService, LogLevel } from './LoggerService';
|
|
55
|
-
export type { LoggerConfig } from './LoggerService';
|
|
56
|
-
export type { LogData } from './LoggerService';
|
|
1
|
+
import { Carno } from '@carno.js/core';
|
|
2
|
+
import { LoggerService, LoggerConfig, LogLevel } from './LoggerService';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Logger plugin configuration.
|
|
6
|
+
*/
|
|
7
|
+
export interface LoggerPluginConfig extends LoggerConfig {
|
|
8
|
+
/** Auto-register as singleton in DI */
|
|
9
|
+
autoRegister?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create a logger plugin for Turbo.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const app = new Turbo();
|
|
18
|
+
* app.use(createLoggerPlugin({ level: LogLevel.DEBUG }));
|
|
19
|
+
*
|
|
20
|
+
* // Now LoggerService is available for injection
|
|
21
|
+
* @Controller()
|
|
22
|
+
* class MyController {
|
|
23
|
+
* constructor(private logger: LoggerService) {}
|
|
24
|
+
*
|
|
25
|
+
* @Get('/')
|
|
26
|
+
* index() {
|
|
27
|
+
* this.logger.info('Request received', { path: '/' });
|
|
28
|
+
* return 'Hello';
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function createCarnoLogger(config: LoggerPluginConfig = {}) {
|
|
34
|
+
const logger = new LoggerService(config);
|
|
35
|
+
return new Carno()
|
|
36
|
+
.services([
|
|
37
|
+
{
|
|
38
|
+
token: LoggerService,
|
|
39
|
+
useValue: logger
|
|
40
|
+
}
|
|
41
|
+
])
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const CarnoLogger = createCarnoLogger();
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create a standalone logger (without Turbo).
|
|
48
|
+
*/
|
|
49
|
+
export function createLogger(config: LoggerConfig = {}): LoggerService {
|
|
50
|
+
return new LoggerService(config);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Re-export everything
|
|
54
|
+
export { LoggerService, LogLevel } from './LoggerService';
|
|
55
|
+
export type { LoggerConfig } from './LoggerService';
|
|
56
|
+
export type { LogData } from './LoggerService';
|
package/src/LoggerService.ts
CHANGED
|
@@ -1,305 +1,305 @@
|
|
|
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
|
-
import { OnApplicationShutdown } from '@carno.js/core';
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Structured log data.
|
|
40
|
-
*/
|
|
41
|
-
export type LogData = Record<string, any>;
|
|
42
|
-
|
|
43
|
-
// ANSI color codes for beautiful output
|
|
44
|
-
const COLORS = {
|
|
45
|
-
reset: '\x1b[0m',
|
|
46
|
-
bold: '\x1b[1m',
|
|
47
|
-
dim: '\x1b[2m',
|
|
48
|
-
|
|
49
|
-
// Levels
|
|
50
|
-
debug: '\x1b[36m', // Cyan
|
|
51
|
-
info: '\x1b[32m', // Green
|
|
52
|
-
warn: '\x1b[33m', // Yellow
|
|
53
|
-
error: '\x1b[31m', // Red
|
|
54
|
-
fatal: '\x1b[35m', // Magenta
|
|
55
|
-
|
|
56
|
-
// Data
|
|
57
|
-
key: '\x1b[90m', // Gray
|
|
58
|
-
string: '\x1b[33m', // Yellow
|
|
59
|
-
number: '\x1b[36m', // Cyan
|
|
60
|
-
boolean: '\x1b[35m', // Magenta
|
|
61
|
-
null: '\x1b[90m', // Gray
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const LEVEL_LABELS: Record<LogLevel, string> = {
|
|
65
|
-
[LogLevel.DEBUG]: `${COLORS.debug}${COLORS.bold}DEBUG${COLORS.reset}`,
|
|
66
|
-
[LogLevel.INFO]: `${COLORS.info}${COLORS.bold}INFO${COLORS.reset} `,
|
|
67
|
-
[LogLevel.WARN]: `${COLORS.warn}${COLORS.bold}WARN${COLORS.reset} `,
|
|
68
|
-
[LogLevel.ERROR]: `${COLORS.error}${COLORS.bold}ERROR${COLORS.reset}`,
|
|
69
|
-
[LogLevel.FATAL]: `${COLORS.fatal}${COLORS.bold}FATAL${COLORS.reset}`,
|
|
70
|
-
[LogLevel.SILENT]: '',
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const LEVEL_ICONS: Record<LogLevel, string> = {
|
|
74
|
-
[LogLevel.DEBUG]: '🔍',
|
|
75
|
-
[LogLevel.INFO]: '✨',
|
|
76
|
-
[LogLevel.WARN]: '⚠️ ',
|
|
77
|
-
[LogLevel.ERROR]: '❌',
|
|
78
|
-
[LogLevel.FATAL]: '💀',
|
|
79
|
-
[LogLevel.SILENT]: '',
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* High-performance async logger.
|
|
84
|
-
*
|
|
85
|
-
* Features:
|
|
86
|
-
* - Async buffered output (like Pino)
|
|
87
|
-
* - Structured JSON data support
|
|
88
|
-
* - Beautiful colorized output
|
|
89
|
-
* - Zero-allocation hot path
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```typescript
|
|
93
|
-
* const logger = new LoggerService({ level: LogLevel.DEBUG });
|
|
94
|
-
*
|
|
95
|
-
* logger.info('User logged in', { userId: 123, ip: '192.168.1.1' });
|
|
96
|
-
* logger.error('Database error', { error: err.message, query: sql });
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
export class LoggerService {
|
|
100
|
-
private level: LogLevel;
|
|
101
|
-
private pretty: boolean;
|
|
102
|
-
private timestamp: boolean;
|
|
103
|
-
private timestampFormat: () => string;
|
|
104
|
-
private prefix: string;
|
|
105
|
-
private buffer: string[] = [];
|
|
106
|
-
private flushTimer: Timer | null = null;
|
|
107
|
-
private flushInterval: number;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
constructor(config: LoggerConfig = {}) {
|
|
111
|
-
this.level = this.parseLevel(config.level ?? LogLevel.INFO);
|
|
112
|
-
this.pretty = config.pretty ?? true;
|
|
113
|
-
this.timestamp = config.timestamp ?? true;
|
|
114
|
-
this.timestampFormat = config.timestampFormat ?? this.defaultTimestamp;
|
|
115
|
-
this.prefix = config.prefix ?? '';
|
|
116
|
-
this.flushInterval = config.flushInterval ?? 10;
|
|
117
|
-
|
|
118
|
-
if (this.flushInterval > 0) {
|
|
119
|
-
this.flushTimer = setInterval(() => this.flush(), this.flushInterval);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Ensure logs are flushed when the process exits
|
|
123
|
-
process.on('exit', () => this.flush());
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
@OnApplicationShutdown()
|
|
127
|
-
shutdown(): void {
|
|
128
|
-
this.flush();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Debug level log.
|
|
133
|
-
*/
|
|
134
|
-
debug(message: string, data?: LogData): void {
|
|
135
|
-
this.log(LogLevel.DEBUG, message, data);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Info level log.
|
|
140
|
-
*/
|
|
141
|
-
info(message: string, data?: LogData): void {
|
|
142
|
-
this.log(LogLevel.INFO, message, data);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Warning level log.
|
|
147
|
-
*/
|
|
148
|
-
warn(message: string, data?: LogData): void {
|
|
149
|
-
this.log(LogLevel.WARN, message, data);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Error level log.
|
|
154
|
-
*/
|
|
155
|
-
error(message: string, data?: LogData): void {
|
|
156
|
-
this.log(LogLevel.ERROR, message, data);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Fatal level log (will also flush immediately).
|
|
161
|
-
*/
|
|
162
|
-
fatal(message: string, data?: LogData): void {
|
|
163
|
-
this.log(LogLevel.FATAL, message, data);
|
|
164
|
-
this.flush(); // Flush immediately for fatal
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Log with custom level.
|
|
169
|
-
*/
|
|
170
|
-
log(level: LogLevel, message: string, data?: LogData): void {
|
|
171
|
-
if (level < this.level) return;
|
|
172
|
-
|
|
173
|
-
const line = this.formatLine(level, message, data);
|
|
174
|
-
|
|
175
|
-
if (this.flushInterval === 0) {
|
|
176
|
-
// Sync mode
|
|
177
|
-
this.write(line, level);
|
|
178
|
-
} else {
|
|
179
|
-
// Async mode
|
|
180
|
-
this.buffer.push(line);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Set minimum log level.
|
|
186
|
-
*/
|
|
187
|
-
setLevel(level: LogLevel | keyof typeof LogLevel): void {
|
|
188
|
-
this.level = this.parseLevel(level);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Flush buffered logs.
|
|
193
|
-
*/
|
|
194
|
-
flush(): void {
|
|
195
|
-
if (this.buffer.length === 0) return;
|
|
196
|
-
|
|
197
|
-
const output = this.buffer.join('\n');
|
|
198
|
-
this.buffer = [];
|
|
199
|
-
|
|
200
|
-
process.stdout.write(output + '\n');
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Close the logger (flush and stop timer).
|
|
205
|
-
*/
|
|
206
|
-
close(): void {
|
|
207
|
-
this.flush();
|
|
208
|
-
if (this.flushTimer) {
|
|
209
|
-
clearInterval(this.flushTimer);
|
|
210
|
-
this.flushTimer = null;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
private write(line: string, level: LogLevel): void {
|
|
215
|
-
if (level >= LogLevel.ERROR) {
|
|
216
|
-
process.stderr.write(line + '\n');
|
|
217
|
-
} else {
|
|
218
|
-
process.stdout.write(line + '\n');
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
private formatLine(level: LogLevel, message: string, data?: LogData): string {
|
|
223
|
-
const parts: string[] = [];
|
|
224
|
-
|
|
225
|
-
// Icon
|
|
226
|
-
parts.push(LEVEL_ICONS[level]);
|
|
227
|
-
|
|
228
|
-
// Timestamp
|
|
229
|
-
if (this.timestamp) {
|
|
230
|
-
parts.push(`${COLORS.dim}${this.timestampFormat()}${COLORS.reset}`);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Level label
|
|
234
|
-
parts.push(LEVEL_LABELS[level]);
|
|
235
|
-
|
|
236
|
-
// Prefix
|
|
237
|
-
if (this.prefix) {
|
|
238
|
-
parts.push(`${COLORS.bold}[${this.prefix}]${COLORS.reset}`);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// Message
|
|
242
|
-
parts.push(message);
|
|
243
|
-
|
|
244
|
-
// Data
|
|
245
|
-
if (data && Object.keys(data).length > 0) {
|
|
246
|
-
if (this.pretty) {
|
|
247
|
-
parts.push(this.formatDataPretty(data));
|
|
248
|
-
} else {
|
|
249
|
-
parts.push(`${COLORS.dim}${JSON.stringify(data)}${COLORS.reset}`);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return parts.join(' ');
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
private formatDataPretty(data: LogData): string {
|
|
257
|
-
const formatted: string[] = [];
|
|
258
|
-
|
|
259
|
-
for (const [key, value] of Object.entries(data)) {
|
|
260
|
-
const formattedValue = this.formatValue(value);
|
|
261
|
-
formatted.push(`${COLORS.key}${key}=${COLORS.reset}${formattedValue}`);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return formatted.join(' ');
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
private formatValue(value: any): string {
|
|
268
|
-
if (value === null) {
|
|
269
|
-
return `${COLORS.null}null${COLORS.reset}`;
|
|
270
|
-
}
|
|
271
|
-
if (value === undefined) {
|
|
272
|
-
return `${COLORS.null}undefined${COLORS.reset}`;
|
|
273
|
-
}
|
|
274
|
-
if (typeof value === 'string') {
|
|
275
|
-
return `${COLORS.string}"${value}"${COLORS.reset}`;
|
|
276
|
-
}
|
|
277
|
-
if (typeof value === 'number') {
|
|
278
|
-
return `${COLORS.number}${value}${COLORS.reset}`;
|
|
279
|
-
}
|
|
280
|
-
if (typeof value === 'boolean') {
|
|
281
|
-
return `${COLORS.boolean}${value}${COLORS.reset}`;
|
|
282
|
-
}
|
|
283
|
-
if (Array.isArray(value)) {
|
|
284
|
-
return `${COLORS.dim}[${value.length} items]${COLORS.reset}`;
|
|
285
|
-
}
|
|
286
|
-
if (typeof value === 'object') {
|
|
287
|
-
return `${COLORS.dim}{${Object.keys(value).length} keys}${COLORS.reset}`;
|
|
288
|
-
}
|
|
289
|
-
return String(value);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
private defaultTimestamp(): string {
|
|
293
|
-
const now = new Date();
|
|
294
|
-
const h = String(now.getHours()).padStart(2, '0');
|
|
295
|
-
const m = String(now.getMinutes()).padStart(2, '0');
|
|
296
|
-
const s = String(now.getSeconds()).padStart(2, '0');
|
|
297
|
-
const ms = String(now.getMilliseconds()).padStart(3, '0');
|
|
298
|
-
return `${h}:${m}:${s}.${ms}`;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
private parseLevel(level: LogLevel | keyof typeof LogLevel): LogLevel {
|
|
302
|
-
if (typeof level === 'number') return level;
|
|
303
|
-
return LogLevel[level] ?? LogLevel.INFO;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
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
|
+
import { OnApplicationShutdown } from '@carno.js/core';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Structured log data.
|
|
40
|
+
*/
|
|
41
|
+
export type LogData = Record<string, any>;
|
|
42
|
+
|
|
43
|
+
// ANSI color codes for beautiful output
|
|
44
|
+
const COLORS = {
|
|
45
|
+
reset: '\x1b[0m',
|
|
46
|
+
bold: '\x1b[1m',
|
|
47
|
+
dim: '\x1b[2m',
|
|
48
|
+
|
|
49
|
+
// Levels
|
|
50
|
+
debug: '\x1b[36m', // Cyan
|
|
51
|
+
info: '\x1b[32m', // Green
|
|
52
|
+
warn: '\x1b[33m', // Yellow
|
|
53
|
+
error: '\x1b[31m', // Red
|
|
54
|
+
fatal: '\x1b[35m', // Magenta
|
|
55
|
+
|
|
56
|
+
// Data
|
|
57
|
+
key: '\x1b[90m', // Gray
|
|
58
|
+
string: '\x1b[33m', // Yellow
|
|
59
|
+
number: '\x1b[36m', // Cyan
|
|
60
|
+
boolean: '\x1b[35m', // Magenta
|
|
61
|
+
null: '\x1b[90m', // Gray
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const LEVEL_LABELS: Record<LogLevel, string> = {
|
|
65
|
+
[LogLevel.DEBUG]: `${COLORS.debug}${COLORS.bold}DEBUG${COLORS.reset}`,
|
|
66
|
+
[LogLevel.INFO]: `${COLORS.info}${COLORS.bold}INFO${COLORS.reset} `,
|
|
67
|
+
[LogLevel.WARN]: `${COLORS.warn}${COLORS.bold}WARN${COLORS.reset} `,
|
|
68
|
+
[LogLevel.ERROR]: `${COLORS.error}${COLORS.bold}ERROR${COLORS.reset}`,
|
|
69
|
+
[LogLevel.FATAL]: `${COLORS.fatal}${COLORS.bold}FATAL${COLORS.reset}`,
|
|
70
|
+
[LogLevel.SILENT]: '',
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const LEVEL_ICONS: Record<LogLevel, string> = {
|
|
74
|
+
[LogLevel.DEBUG]: '🔍',
|
|
75
|
+
[LogLevel.INFO]: '✨',
|
|
76
|
+
[LogLevel.WARN]: '⚠️ ',
|
|
77
|
+
[LogLevel.ERROR]: '❌',
|
|
78
|
+
[LogLevel.FATAL]: '💀',
|
|
79
|
+
[LogLevel.SILENT]: '',
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* High-performance async logger.
|
|
84
|
+
*
|
|
85
|
+
* Features:
|
|
86
|
+
* - Async buffered output (like Pino)
|
|
87
|
+
* - Structured JSON data support
|
|
88
|
+
* - Beautiful colorized output
|
|
89
|
+
* - Zero-allocation hot path
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const logger = new LoggerService({ level: LogLevel.DEBUG });
|
|
94
|
+
*
|
|
95
|
+
* logger.info('User logged in', { userId: 123, ip: '192.168.1.1' });
|
|
96
|
+
* logger.error('Database error', { error: err.message, query: sql });
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export class LoggerService {
|
|
100
|
+
private level: LogLevel;
|
|
101
|
+
private pretty: boolean;
|
|
102
|
+
private timestamp: boolean;
|
|
103
|
+
private timestampFormat: () => string;
|
|
104
|
+
private prefix: string;
|
|
105
|
+
private buffer: string[] = [];
|
|
106
|
+
private flushTimer: Timer | null = null;
|
|
107
|
+
private flushInterval: number;
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
constructor(config: LoggerConfig = {}) {
|
|
111
|
+
this.level = this.parseLevel(config.level ?? LogLevel.INFO);
|
|
112
|
+
this.pretty = config.pretty ?? true;
|
|
113
|
+
this.timestamp = config.timestamp ?? true;
|
|
114
|
+
this.timestampFormat = config.timestampFormat ?? this.defaultTimestamp;
|
|
115
|
+
this.prefix = config.prefix ?? '';
|
|
116
|
+
this.flushInterval = config.flushInterval ?? 10;
|
|
117
|
+
|
|
118
|
+
if (this.flushInterval > 0) {
|
|
119
|
+
this.flushTimer = setInterval(() => this.flush(), this.flushInterval);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Ensure logs are flushed when the process exits
|
|
123
|
+
process.on('exit', () => this.flush());
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@OnApplicationShutdown()
|
|
127
|
+
shutdown(): void {
|
|
128
|
+
this.flush();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Debug level log.
|
|
133
|
+
*/
|
|
134
|
+
debug(message: string, data?: LogData): void {
|
|
135
|
+
this.log(LogLevel.DEBUG, message, data);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Info level log.
|
|
140
|
+
*/
|
|
141
|
+
info(message: string, data?: LogData): void {
|
|
142
|
+
this.log(LogLevel.INFO, message, data);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Warning level log.
|
|
147
|
+
*/
|
|
148
|
+
warn(message: string, data?: LogData): void {
|
|
149
|
+
this.log(LogLevel.WARN, message, data);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Error level log.
|
|
154
|
+
*/
|
|
155
|
+
error(message: string, data?: LogData): void {
|
|
156
|
+
this.log(LogLevel.ERROR, message, data);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Fatal level log (will also flush immediately).
|
|
161
|
+
*/
|
|
162
|
+
fatal(message: string, data?: LogData): void {
|
|
163
|
+
this.log(LogLevel.FATAL, message, data);
|
|
164
|
+
this.flush(); // Flush immediately for fatal
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Log with custom level.
|
|
169
|
+
*/
|
|
170
|
+
log(level: LogLevel, message: string, data?: LogData): void {
|
|
171
|
+
if (level < this.level) return;
|
|
172
|
+
|
|
173
|
+
const line = this.formatLine(level, message, data);
|
|
174
|
+
|
|
175
|
+
if (this.flushInterval === 0) {
|
|
176
|
+
// Sync mode
|
|
177
|
+
this.write(line, level);
|
|
178
|
+
} else {
|
|
179
|
+
// Async mode
|
|
180
|
+
this.buffer.push(line);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Set minimum log level.
|
|
186
|
+
*/
|
|
187
|
+
setLevel(level: LogLevel | keyof typeof LogLevel): void {
|
|
188
|
+
this.level = this.parseLevel(level);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Flush buffered logs.
|
|
193
|
+
*/
|
|
194
|
+
flush(): void {
|
|
195
|
+
if (this.buffer.length === 0) return;
|
|
196
|
+
|
|
197
|
+
const output = this.buffer.join('\n');
|
|
198
|
+
this.buffer = [];
|
|
199
|
+
|
|
200
|
+
process.stdout.write(output + '\n');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Close the logger (flush and stop timer).
|
|
205
|
+
*/
|
|
206
|
+
close(): void {
|
|
207
|
+
this.flush();
|
|
208
|
+
if (this.flushTimer) {
|
|
209
|
+
clearInterval(this.flushTimer);
|
|
210
|
+
this.flushTimer = null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private write(line: string, level: LogLevel): void {
|
|
215
|
+
if (level >= LogLevel.ERROR) {
|
|
216
|
+
process.stderr.write(line + '\n');
|
|
217
|
+
} else {
|
|
218
|
+
process.stdout.write(line + '\n');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private formatLine(level: LogLevel, message: string, data?: LogData): string {
|
|
223
|
+
const parts: string[] = [];
|
|
224
|
+
|
|
225
|
+
// Icon
|
|
226
|
+
parts.push(LEVEL_ICONS[level]);
|
|
227
|
+
|
|
228
|
+
// Timestamp
|
|
229
|
+
if (this.timestamp) {
|
|
230
|
+
parts.push(`${COLORS.dim}${this.timestampFormat()}${COLORS.reset}`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Level label
|
|
234
|
+
parts.push(LEVEL_LABELS[level]);
|
|
235
|
+
|
|
236
|
+
// Prefix
|
|
237
|
+
if (this.prefix) {
|
|
238
|
+
parts.push(`${COLORS.bold}[${this.prefix}]${COLORS.reset}`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Message
|
|
242
|
+
parts.push(message);
|
|
243
|
+
|
|
244
|
+
// Data
|
|
245
|
+
if (data && Object.keys(data).length > 0) {
|
|
246
|
+
if (this.pretty) {
|
|
247
|
+
parts.push(this.formatDataPretty(data));
|
|
248
|
+
} else {
|
|
249
|
+
parts.push(`${COLORS.dim}${JSON.stringify(data)}${COLORS.reset}`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return parts.join(' ');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private formatDataPretty(data: LogData): string {
|
|
257
|
+
const formatted: string[] = [];
|
|
258
|
+
|
|
259
|
+
for (const [key, value] of Object.entries(data)) {
|
|
260
|
+
const formattedValue = this.formatValue(value);
|
|
261
|
+
formatted.push(`${COLORS.key}${key}=${COLORS.reset}${formattedValue}`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return formatted.join(' ');
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private formatValue(value: any): string {
|
|
268
|
+
if (value === null) {
|
|
269
|
+
return `${COLORS.null}null${COLORS.reset}`;
|
|
270
|
+
}
|
|
271
|
+
if (value === undefined) {
|
|
272
|
+
return `${COLORS.null}undefined${COLORS.reset}`;
|
|
273
|
+
}
|
|
274
|
+
if (typeof value === 'string') {
|
|
275
|
+
return `${COLORS.string}"${value}"${COLORS.reset}`;
|
|
276
|
+
}
|
|
277
|
+
if (typeof value === 'number') {
|
|
278
|
+
return `${COLORS.number}${value}${COLORS.reset}`;
|
|
279
|
+
}
|
|
280
|
+
if (typeof value === 'boolean') {
|
|
281
|
+
return `${COLORS.boolean}${value}${COLORS.reset}`;
|
|
282
|
+
}
|
|
283
|
+
if (Array.isArray(value)) {
|
|
284
|
+
return `${COLORS.dim}[${value.length} items]${COLORS.reset}`;
|
|
285
|
+
}
|
|
286
|
+
if (typeof value === 'object') {
|
|
287
|
+
return `${COLORS.dim}{${Object.keys(value).length} keys}${COLORS.reset}`;
|
|
288
|
+
}
|
|
289
|
+
return String(value);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private defaultTimestamp(): string {
|
|
293
|
+
const now = new Date();
|
|
294
|
+
const h = String(now.getHours()).padStart(2, '0');
|
|
295
|
+
const m = String(now.getMinutes()).padStart(2, '0');
|
|
296
|
+
const s = String(now.getSeconds()).padStart(2, '0');
|
|
297
|
+
const ms = String(now.getMilliseconds()).padStart(3, '0');
|
|
298
|
+
return `${h}:${m}:${s}.${ms}`;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private parseLevel(level: LogLevel | keyof typeof LogLevel): LogLevel {
|
|
302
|
+
if (typeof level === 'number') return level;
|
|
303
|
+
return LogLevel[level] ?? LogLevel.INFO;
|
|
304
|
+
}
|
|
305
|
+
}
|
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';
|