@feizk/logger 1.7.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.ts CHANGED
@@ -1,86 +1,239 @@
1
- type LogLevel = 'debug' | 'info' | 'warn' | 'error';
2
- type TimestampType = 'iso' | 'locale' | 'custom';
3
- interface TimestampTypes {
4
- ISO: 'iso';
5
- Locale: 'locale';
6
- Custom: 'custom';
1
+ /**
2
+ * Log levels ordered by severity.
3
+ * - trace: Very detailed debugging information
4
+ * - debug: Detailed information for debugging
5
+ * - info: General informational messages
6
+ * - warn: Warning conditions
7
+ * - error: Error conditions
8
+ * - fatal: Critical errors that may terminate the application
9
+ */
10
+ type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
11
+ /**
12
+ * A structured log entry passed to transports and formatters.
13
+ */
14
+ interface LogEntry {
15
+ /** The log level of this entry */
16
+ level: LogLevel;
17
+ /** ISO 8601 formatted timestamp */
18
+ timestamp: string;
19
+ /** Original arguments passed to the log method */
20
+ args: unknown[];
21
+ /** Optional prefix from the logger hierarchy */
22
+ prefix?: string;
23
+ /** Context metadata attached to the logger */
24
+ context: Record<string, unknown>;
7
25
  }
8
- interface DiscordOptions {
9
- enable: boolean;
10
- webhookURL: string;
11
- batchSize?: number;
12
- batchDelay?: number;
13
- maxRetries?: number;
14
- retryDelayBase?: number;
15
- formatEmbed?: (level: LogLevel, timestamp: string, message: string) => Record<string, unknown>;
26
+ /**
27
+ * Pluggable transport interface.
28
+ * Implement this interface to create custom log transports.
29
+ */
30
+ interface Transport {
31
+ /**
32
+ * Called for each log entry.
33
+ * @param entry - The structured log entry
34
+ */
35
+ log(entry: LogEntry): void | Promise<void>;
36
+ /**
37
+ * Optional cleanup method called when the logger is destroyed.
38
+ */
39
+ destroy?(): void | Promise<void>;
40
+ }
41
+ /**
42
+ * Timestamp option: preset string or custom formatter function.
43
+ * - 'iso': ISO 8601 format (default)
44
+ * - 'locale': Localized date/time string
45
+ * - function: Custom formatter
46
+ */
47
+ type TimestampOption = 'iso' | 'locale' | ((date: Date) => string);
48
+ /**
49
+ * Custom formatter function for log output.
50
+ * @param entry - The structured log entry
51
+ * @returns The formatted string to output to console
52
+ */
53
+ type Formatter = (entry: LogEntry) => string;
54
+ /**
55
+ * Options for creating a child logger.
56
+ */
57
+ interface ChildLoggerOptions {
58
+ /** Prefix to prepend to log messages */
59
+ prefix?: string;
60
+ /** Additional context metadata */
61
+ context?: Record<string, unknown>;
62
+ /** Override log level for this child logger */
63
+ level?: LogLevel;
64
+ /** Override silent mode for this child logger */
65
+ silent?: boolean;
16
66
  }
67
+ /**
68
+ * Main logger configuration options.
69
+ */
17
70
  interface LoggerOptions {
71
+ /** Minimum log level to output (default: 'debug') */
18
72
  level?: LogLevel;
73
+ /** Suppress all console output (default: false) */
74
+ silent?: boolean;
75
+ /** Enable colored output (default: true) */
19
76
  enableColors?: boolean;
20
- discord?: DiscordOptions;
21
- formatTimestamp?: (types: TimestampTypes, date?: Date) => [TimestampType, string];
22
- formatLog?: (level: string, timestamp: string, args: unknown[]) => string;
77
+ /** Timestamp format (default: 'iso') */
78
+ timestamp?: TimestampOption;
79
+ /** Custom formatter for log output */
80
+ formatter?: Formatter;
81
+ /** Output logs as JSON (default: false) */
82
+ json?: boolean;
83
+ /** Initial transports to attach */
84
+ transports?: Transport[];
85
+ /** Prefix for all log messages */
86
+ prefix?: string;
87
+ /** Initial context metadata */
88
+ context?: Record<string, unknown>;
23
89
  }
24
90
 
25
91
  /**
26
- * A simple logger with colored outputs and timestamps.
92
+ * A lightweight, pluggable logger with colored outputs, structured logging, and transport support.
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * import { Logger } from '@feizk/logger';
97
+ *
98
+ * const logger = new Logger();
99
+ *
100
+ * logger.info('Hello, world!');
101
+ * logger.warn('This is a warning');
102
+ * logger.error('This is an error');
103
+ * ```
27
104
  */
28
105
  declare class Logger {
29
- private options;
30
- private level;
31
- private discordQueue;
32
- private isProcessing;
33
- private processTimeout?;
106
+ private readonly options;
107
+ private readonly transports;
108
+ private readonly prefix?;
109
+ private readonly context;
110
+ /**
111
+ * Create a new Logger instance.
112
+ * @param options - Configuration options
113
+ */
34
114
  constructor(options?: LoggerOptions);
35
115
  /**
36
- * Sets the minimum log level for filtering messages.
37
- * @param level - The log level to set.
116
+ * Log a trace message (most verbose).
117
+ * @param args - Arguments to log
118
+ */
119
+ trace(...args: unknown[]): void;
120
+ /**
121
+ * Log a debug message.
122
+ * @param args - Arguments to log
123
+ */
124
+ debug(...args: unknown[]): void;
125
+ /**
126
+ * Log an info message.
127
+ * @param args - Arguments to log
128
+ */
129
+ info(...args: unknown[]): void;
130
+ /**
131
+ * Log a warning message.
132
+ * @param args - Arguments to log
133
+ */
134
+ warn(...args: unknown[]): void;
135
+ /**
136
+ * Log an error message.
137
+ * @param args - Arguments to log
138
+ */
139
+ error(...args: unknown[]): void;
140
+ /**
141
+ * Log a fatal message (most severe).
142
+ * @param args - Arguments to log
143
+ */
144
+ fatal(...args: unknown[]): void;
145
+ /**
146
+ * Set the minimum log level.
147
+ * @param level - The log level to set
38
148
  */
39
149
  setLevel(level: LogLevel): void;
40
150
  /**
41
- * Sends a log message to Discord via webhook if configured.
42
- * @param level - The log level.
43
- * @param timestamp - The formatted timestamp.
44
- * @param args - The log arguments.
151
+ * Get the current log level.
152
+ * @returns The current log level
45
153
  */
46
- private sendToDiscord;
154
+ getLevel(): LogLevel;
47
155
  /**
48
- * Processes the Discord queue by sending batches of embeds.
156
+ * Add a transport to the logger.
157
+ * @param transport - The transport to add
49
158
  */
50
- private processQueue;
159
+ addTransport(transport: Transport): void;
51
160
  /**
52
- * Sends a batch of embeds to Discord.
53
- * @param embeds - The embeds to send.
161
+ * Remove a transport from the logger.
162
+ * @param transport - The transport to remove
54
163
  */
55
- private sendBatch;
164
+ removeTransport(transport: Transport): void;
56
165
  /**
57
- * Checks if a log level should be output based on the current log level.
58
- * @param level - The log level to check.
59
- * @returns True if the message should be logged.
166
+ * Create a child logger with additional prefix and context.
167
+ * @param options - Child logger options
168
+ * @returns A new Logger instance
60
169
  */
61
- private shouldLog;
170
+ child(options?: ChildLoggerOptions): Logger;
62
171
  /**
63
- * Logs an info message.
64
- * @param args - The arguments to log.
172
+ * Destroy the logger and all its transports.
173
+ * Calls destroy() on all registered transports.
65
174
  */
66
- info(...args: unknown[]): void;
175
+ destroy(): Promise<void>;
67
176
  /**
68
- * Logs a warning message.
69
- * @param args - The arguments to log.
177
+ * Core logging method - all public methods delegate here.
178
+ * @param level - The log level
179
+ * @param args - The arguments to log
70
180
  */
71
- warn(...args: unknown[]): void;
181
+ private log;
72
182
  /**
73
- * Logs an error message.
74
- * @param args - The arguments to log.
183
+ * Write a log entry to the console.
184
+ * @param level - The log level
185
+ * @param entry - The log entry
75
186
  */
76
- error(...args: unknown[]): void;
187
+ private writeToConsole;
77
188
  /**
78
- * Logs a debug message.
79
- * @param args - The arguments to log.
189
+ * Dispatch a log entry to a transport.
190
+ * @param transport - The transport
191
+ * @param entry - The log entry
80
192
  */
81
- debug(...args: unknown[]): void;
193
+ private dispatchToTransport;
194
+ /**
195
+ * Check if a log level should be output.
196
+ * @param level - The log level to check
197
+ * @returns True if the message should be logged
198
+ */
199
+ private shouldLog;
82
200
  }
83
201
 
84
- declare const TIMESTAMP_TYPES: TimestampTypes;
202
+ /**
203
+ * Log level priorities ordered by severity (lower = less severe).
204
+ */
205
+ declare const LOG_LEVEL_PRIORITIES: Record<LogLevel, number>;
206
+ /**
207
+ * Text labels for log levels.
208
+ */
209
+ declare const LEVEL_LABELS: Record<LogLevel, string>;
210
+
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;
85
238
 
86
- export { type DiscordOptions, type LogLevel, Logger, type LoggerOptions, TIMESTAMP_TYPES, type TimestampType, type TimestampTypes };
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 };