@feizk/logger 2.0.0 → 2.0.1
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/index.d.mts +29 -1
- package/dist/index.d.ts +29 -1
- package/dist/index.js +47 -32
- package/dist/index.mjs +42 -31
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -208,4 +208,32 @@ declare const LOG_LEVEL_PRIORITIES: Record<LogLevel, number>;
|
|
|
208
208
|
*/
|
|
209
209
|
declare const LEVEL_LABELS: Record<LogLevel, string>;
|
|
210
210
|
|
|
211
|
-
|
|
211
|
+
/**
|
|
212
|
+
* Get the colored label for a log level.
|
|
213
|
+
* Results are cached for performance.
|
|
214
|
+
* @param level - The log level
|
|
215
|
+
* @param enableColors - Whether to apply colors
|
|
216
|
+
* @returns The label string (colored if enabled)
|
|
217
|
+
*/
|
|
218
|
+
declare function getColoredLabel(level: LogLevel, enableColors: boolean): string;
|
|
219
|
+
/**
|
|
220
|
+
* Format a timestamp based on the provided option.
|
|
221
|
+
* @param option - Preset string or custom formatter
|
|
222
|
+
* @param date - Date to format (default: current date)
|
|
223
|
+
* @returns Formatted timestamp string
|
|
224
|
+
*/
|
|
225
|
+
declare function formatTimestamp(option: TimestampOption, date?: Date): string;
|
|
226
|
+
/**
|
|
227
|
+
* Format a log entry as JSON for structured logging.
|
|
228
|
+
* @param entry - The log entry to format
|
|
229
|
+
* @returns JSON string representation
|
|
230
|
+
*/
|
|
231
|
+
declare function formatJson(entry: LogEntry): string;
|
|
232
|
+
/**
|
|
233
|
+
* Build the message string from log arguments.
|
|
234
|
+
* @param args - The log arguments
|
|
235
|
+
* @returns Formatted message string
|
|
236
|
+
*/
|
|
237
|
+
declare function buildMessage(args: unknown[]): string;
|
|
238
|
+
|
|
239
|
+
export { type ChildLoggerOptions, type Formatter, LEVEL_LABELS, LOG_LEVEL_PRIORITIES, type LogEntry, type LogLevel, Logger, type LoggerOptions, type TimestampOption, type Transport, buildMessage, formatJson, formatTimestamp, getColoredLabel };
|
package/dist/index.d.ts
CHANGED
|
@@ -208,4 +208,32 @@ declare const LOG_LEVEL_PRIORITIES: Record<LogLevel, number>;
|
|
|
208
208
|
*/
|
|
209
209
|
declare const LEVEL_LABELS: Record<LogLevel, string>;
|
|
210
210
|
|
|
211
|
-
|
|
211
|
+
/**
|
|
212
|
+
* Get the colored label for a log level.
|
|
213
|
+
* Results are cached for performance.
|
|
214
|
+
* @param level - The log level
|
|
215
|
+
* @param enableColors - Whether to apply colors
|
|
216
|
+
* @returns The label string (colored if enabled)
|
|
217
|
+
*/
|
|
218
|
+
declare function getColoredLabel(level: LogLevel, enableColors: boolean): string;
|
|
219
|
+
/**
|
|
220
|
+
* Format a timestamp based on the provided option.
|
|
221
|
+
* @param option - Preset string or custom formatter
|
|
222
|
+
* @param date - Date to format (default: current date)
|
|
223
|
+
* @returns Formatted timestamp string
|
|
224
|
+
*/
|
|
225
|
+
declare function formatTimestamp(option: TimestampOption, date?: Date): string;
|
|
226
|
+
/**
|
|
227
|
+
* Format a log entry as JSON for structured logging.
|
|
228
|
+
* @param entry - The log entry to format
|
|
229
|
+
* @returns JSON string representation
|
|
230
|
+
*/
|
|
231
|
+
declare function formatJson(entry: LogEntry): string;
|
|
232
|
+
/**
|
|
233
|
+
* Build the message string from log arguments.
|
|
234
|
+
* @param args - The log arguments
|
|
235
|
+
* @returns Formatted message string
|
|
236
|
+
*/
|
|
237
|
+
declare function buildMessage(args: unknown[]): string;
|
|
238
|
+
|
|
239
|
+
export { type ChildLoggerOptions, type Formatter, LEVEL_LABELS, LOG_LEVEL_PRIORITIES, type LogEntry, type LogLevel, Logger, type LoggerOptions, type TimestampOption, type Transport, buildMessage, formatJson, formatTimestamp, getColoredLabel };
|
package/dist/index.js
CHANGED
|
@@ -22,7 +22,11 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
LEVEL_LABELS: () => LEVEL_LABELS,
|
|
24
24
|
LOG_LEVEL_PRIORITIES: () => LOG_LEVEL_PRIORITIES,
|
|
25
|
-
Logger: () => Logger
|
|
25
|
+
Logger: () => Logger,
|
|
26
|
+
buildMessage: () => buildMessage,
|
|
27
|
+
formatJson: () => formatJson,
|
|
28
|
+
formatTimestamp: () => formatTimestamp,
|
|
29
|
+
getColoredLabel: () => getColoredLabel
|
|
26
30
|
});
|
|
27
31
|
module.exports = __toCommonJS(index_exports);
|
|
28
32
|
|
|
@@ -102,14 +106,7 @@ function formatTimestamp(option, date = /* @__PURE__ */ new Date()) {
|
|
|
102
106
|
return preset(date);
|
|
103
107
|
}
|
|
104
108
|
function formatJson(entry) {
|
|
105
|
-
const message = entry.args.map((
|
|
106
|
-
if (typeof arg === "string") return arg;
|
|
107
|
-
try {
|
|
108
|
-
return JSON.stringify(arg);
|
|
109
|
-
} catch {
|
|
110
|
-
return String(arg);
|
|
111
|
-
}
|
|
112
|
-
}).join(" ");
|
|
109
|
+
const message = entry.args.map(formatArg).join(" ");
|
|
113
110
|
const output = {
|
|
114
111
|
level: entry.level,
|
|
115
112
|
timestamp: entry.timestamp,
|
|
@@ -123,15 +120,47 @@ function formatJson(entry) {
|
|
|
123
120
|
}
|
|
124
121
|
return JSON.stringify(output);
|
|
125
122
|
}
|
|
126
|
-
function
|
|
127
|
-
|
|
128
|
-
|
|
123
|
+
function formatArg(arg) {
|
|
124
|
+
if (arg === null) return "null";
|
|
125
|
+
if (arg === void 0) return "undefined";
|
|
126
|
+
if (typeof arg === "string") return arg;
|
|
127
|
+
if (typeof arg === "number") return String(arg);
|
|
128
|
+
if (typeof arg === "boolean") return String(arg);
|
|
129
|
+
if (typeof arg === "bigint") return `${arg}n`;
|
|
130
|
+
if (typeof arg === "symbol") return arg.toString();
|
|
131
|
+
if (arg instanceof Error) {
|
|
132
|
+
const parts = [arg.message];
|
|
133
|
+
if (arg.name && arg.name !== "Error") parts.unshift(arg.name);
|
|
134
|
+
if (arg.stack) parts.push(arg.stack);
|
|
135
|
+
return parts.join(": ");
|
|
136
|
+
}
|
|
137
|
+
if (Array.isArray(arg)) {
|
|
129
138
|
try {
|
|
130
139
|
return JSON.stringify(arg);
|
|
131
140
|
} catch {
|
|
132
141
|
return String(arg);
|
|
133
142
|
}
|
|
134
|
-
}
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
return JSON.stringify(arg, getCircularReplacer());
|
|
146
|
+
} catch {
|
|
147
|
+
return String(arg);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function getCircularReplacer() {
|
|
151
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
152
|
+
return (_key, value) => {
|
|
153
|
+
if (typeof value === "object" && value !== null) {
|
|
154
|
+
if (seen.has(value)) {
|
|
155
|
+
return "[Circular]";
|
|
156
|
+
}
|
|
157
|
+
seen.add(value);
|
|
158
|
+
}
|
|
159
|
+
return value;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function buildMessage(args) {
|
|
163
|
+
return args.map(formatArg).join(" ");
|
|
135
164
|
}
|
|
136
165
|
|
|
137
166
|
// src/logger.ts
|
|
@@ -156,9 +185,6 @@ var Logger = class _Logger {
|
|
|
156
185
|
this.prefix = this.options.prefix;
|
|
157
186
|
this.context = this.options.context;
|
|
158
187
|
}
|
|
159
|
-
// ============================================================================
|
|
160
|
-
// Public Log Methods
|
|
161
|
-
// ============================================================================
|
|
162
188
|
/**
|
|
163
189
|
* Log a trace message (most verbose).
|
|
164
190
|
* @param args - Arguments to log
|
|
@@ -201,9 +227,6 @@ var Logger = class _Logger {
|
|
|
201
227
|
fatal(...args) {
|
|
202
228
|
this.log("fatal", args);
|
|
203
229
|
}
|
|
204
|
-
// ============================================================================
|
|
205
|
-
// Level Management
|
|
206
|
-
// ============================================================================
|
|
207
230
|
/**
|
|
208
231
|
* Set the minimum log level.
|
|
209
232
|
* @param level - The log level to set
|
|
@@ -218,9 +241,6 @@ var Logger = class _Logger {
|
|
|
218
241
|
getLevel() {
|
|
219
242
|
return this.options.level;
|
|
220
243
|
}
|
|
221
|
-
// ============================================================================
|
|
222
|
-
// Transport Management
|
|
223
|
-
// ============================================================================
|
|
224
244
|
/**
|
|
225
245
|
* Add a transport to the logger.
|
|
226
246
|
* @param transport - The transport to add
|
|
@@ -238,9 +258,6 @@ var Logger = class _Logger {
|
|
|
238
258
|
this.transports.splice(index, 1);
|
|
239
259
|
}
|
|
240
260
|
}
|
|
241
|
-
// ============================================================================
|
|
242
|
-
// Child Logger
|
|
243
|
-
// ============================================================================
|
|
244
261
|
/**
|
|
245
262
|
* Create a child logger with additional prefix and context.
|
|
246
263
|
* @param options - Child logger options
|
|
@@ -264,9 +281,6 @@ var Logger = class _Logger {
|
|
|
264
281
|
context: combinedContext
|
|
265
282
|
});
|
|
266
283
|
}
|
|
267
|
-
// ============================================================================
|
|
268
|
-
// Cleanup
|
|
269
|
-
// ============================================================================
|
|
270
284
|
/**
|
|
271
285
|
* Destroy the logger and all its transports.
|
|
272
286
|
* Calls destroy() on all registered transports.
|
|
@@ -280,9 +294,6 @@ var Logger = class _Logger {
|
|
|
280
294
|
await Promise.all(destroyPromises);
|
|
281
295
|
this.transports.length = 0;
|
|
282
296
|
}
|
|
283
|
-
// ============================================================================
|
|
284
|
-
// Private Methods
|
|
285
|
-
// ============================================================================
|
|
286
297
|
/**
|
|
287
298
|
* Core logging method - all public methods delegate here.
|
|
288
299
|
* @param level - The log level
|
|
@@ -352,5 +363,9 @@ var Logger = class _Logger {
|
|
|
352
363
|
0 && (module.exports = {
|
|
353
364
|
LEVEL_LABELS,
|
|
354
365
|
LOG_LEVEL_PRIORITIES,
|
|
355
|
-
Logger
|
|
366
|
+
Logger,
|
|
367
|
+
buildMessage,
|
|
368
|
+
formatJson,
|
|
369
|
+
formatTimestamp,
|
|
370
|
+
getColoredLabel
|
|
356
371
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -74,14 +74,7 @@ function formatTimestamp(option, date = /* @__PURE__ */ new Date()) {
|
|
|
74
74
|
return preset(date);
|
|
75
75
|
}
|
|
76
76
|
function formatJson(entry) {
|
|
77
|
-
const message = entry.args.map((
|
|
78
|
-
if (typeof arg === "string") return arg;
|
|
79
|
-
try {
|
|
80
|
-
return JSON.stringify(arg);
|
|
81
|
-
} catch {
|
|
82
|
-
return String(arg);
|
|
83
|
-
}
|
|
84
|
-
}).join(" ");
|
|
77
|
+
const message = entry.args.map(formatArg).join(" ");
|
|
85
78
|
const output = {
|
|
86
79
|
level: entry.level,
|
|
87
80
|
timestamp: entry.timestamp,
|
|
@@ -95,15 +88,47 @@ function formatJson(entry) {
|
|
|
95
88
|
}
|
|
96
89
|
return JSON.stringify(output);
|
|
97
90
|
}
|
|
98
|
-
function
|
|
99
|
-
|
|
100
|
-
|
|
91
|
+
function formatArg(arg) {
|
|
92
|
+
if (arg === null) return "null";
|
|
93
|
+
if (arg === void 0) return "undefined";
|
|
94
|
+
if (typeof arg === "string") return arg;
|
|
95
|
+
if (typeof arg === "number") return String(arg);
|
|
96
|
+
if (typeof arg === "boolean") return String(arg);
|
|
97
|
+
if (typeof arg === "bigint") return `${arg}n`;
|
|
98
|
+
if (typeof arg === "symbol") return arg.toString();
|
|
99
|
+
if (arg instanceof Error) {
|
|
100
|
+
const parts = [arg.message];
|
|
101
|
+
if (arg.name && arg.name !== "Error") parts.unshift(arg.name);
|
|
102
|
+
if (arg.stack) parts.push(arg.stack);
|
|
103
|
+
return parts.join(": ");
|
|
104
|
+
}
|
|
105
|
+
if (Array.isArray(arg)) {
|
|
101
106
|
try {
|
|
102
107
|
return JSON.stringify(arg);
|
|
103
108
|
} catch {
|
|
104
109
|
return String(arg);
|
|
105
110
|
}
|
|
106
|
-
}
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
return JSON.stringify(arg, getCircularReplacer());
|
|
114
|
+
} catch {
|
|
115
|
+
return String(arg);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function getCircularReplacer() {
|
|
119
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
120
|
+
return (_key, value) => {
|
|
121
|
+
if (typeof value === "object" && value !== null) {
|
|
122
|
+
if (seen.has(value)) {
|
|
123
|
+
return "[Circular]";
|
|
124
|
+
}
|
|
125
|
+
seen.add(value);
|
|
126
|
+
}
|
|
127
|
+
return value;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function buildMessage(args) {
|
|
131
|
+
return args.map(formatArg).join(" ");
|
|
107
132
|
}
|
|
108
133
|
|
|
109
134
|
// src/logger.ts
|
|
@@ -128,9 +153,6 @@ var Logger = class _Logger {
|
|
|
128
153
|
this.prefix = this.options.prefix;
|
|
129
154
|
this.context = this.options.context;
|
|
130
155
|
}
|
|
131
|
-
// ============================================================================
|
|
132
|
-
// Public Log Methods
|
|
133
|
-
// ============================================================================
|
|
134
156
|
/**
|
|
135
157
|
* Log a trace message (most verbose).
|
|
136
158
|
* @param args - Arguments to log
|
|
@@ -173,9 +195,6 @@ var Logger = class _Logger {
|
|
|
173
195
|
fatal(...args) {
|
|
174
196
|
this.log("fatal", args);
|
|
175
197
|
}
|
|
176
|
-
// ============================================================================
|
|
177
|
-
// Level Management
|
|
178
|
-
// ============================================================================
|
|
179
198
|
/**
|
|
180
199
|
* Set the minimum log level.
|
|
181
200
|
* @param level - The log level to set
|
|
@@ -190,9 +209,6 @@ var Logger = class _Logger {
|
|
|
190
209
|
getLevel() {
|
|
191
210
|
return this.options.level;
|
|
192
211
|
}
|
|
193
|
-
// ============================================================================
|
|
194
|
-
// Transport Management
|
|
195
|
-
// ============================================================================
|
|
196
212
|
/**
|
|
197
213
|
* Add a transport to the logger.
|
|
198
214
|
* @param transport - The transport to add
|
|
@@ -210,9 +226,6 @@ var Logger = class _Logger {
|
|
|
210
226
|
this.transports.splice(index, 1);
|
|
211
227
|
}
|
|
212
228
|
}
|
|
213
|
-
// ============================================================================
|
|
214
|
-
// Child Logger
|
|
215
|
-
// ============================================================================
|
|
216
229
|
/**
|
|
217
230
|
* Create a child logger with additional prefix and context.
|
|
218
231
|
* @param options - Child logger options
|
|
@@ -236,9 +249,6 @@ var Logger = class _Logger {
|
|
|
236
249
|
context: combinedContext
|
|
237
250
|
});
|
|
238
251
|
}
|
|
239
|
-
// ============================================================================
|
|
240
|
-
// Cleanup
|
|
241
|
-
// ============================================================================
|
|
242
252
|
/**
|
|
243
253
|
* Destroy the logger and all its transports.
|
|
244
254
|
* Calls destroy() on all registered transports.
|
|
@@ -252,9 +262,6 @@ var Logger = class _Logger {
|
|
|
252
262
|
await Promise.all(destroyPromises);
|
|
253
263
|
this.transports.length = 0;
|
|
254
264
|
}
|
|
255
|
-
// ============================================================================
|
|
256
|
-
// Private Methods
|
|
257
|
-
// ============================================================================
|
|
258
265
|
/**
|
|
259
266
|
* Core logging method - all public methods delegate here.
|
|
260
267
|
* @param level - The log level
|
|
@@ -323,5 +330,9 @@ var Logger = class _Logger {
|
|
|
323
330
|
export {
|
|
324
331
|
LEVEL_LABELS,
|
|
325
332
|
LOG_LEVEL_PRIORITIES,
|
|
326
|
-
Logger
|
|
333
|
+
Logger,
|
|
334
|
+
buildMessage,
|
|
335
|
+
formatJson,
|
|
336
|
+
formatTimestamp,
|
|
337
|
+
getColoredLabel
|
|
327
338
|
};
|
package/package.json
CHANGED