crisplogs 0.1.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.
@@ -0,0 +1,272 @@
1
+ /** Box decoration style for console output. */
2
+ type Style = "short-fixed" | "short-dynamic" | "long-boxed";
3
+ /**
4
+ * How extra fields are serialized in the log output.
5
+ * - `"inline"` (default): `[key=value key2=value2]`
6
+ * - `"json"`: compact single-line JSON — `{"key":"value","key2":"value2"}`
7
+ * - `"pretty"`: formatted multi-line JSON (best paired with `"long-boxed"` style)
8
+ */
9
+ type ExtraFormat = "inline" | "json" | "pretty";
10
+ /** Supported log levels, matching Python's logging module hierarchy. */
11
+ type Level = "DEBUG" | "INFO" | "WARNING" | "ERROR" | "CRITICAL";
12
+ /** Numeric values for each log level (matches Python logging). */
13
+ declare const LEVEL_VALUES: Record<Level, number>;
14
+ /** A single log record containing all metadata for one log event. */
15
+ interface LogRecord {
16
+ levelName: Level;
17
+ levelNo: number;
18
+ message: string;
19
+ timestamp: Date;
20
+ name: string;
21
+ pathname: string;
22
+ lineno: number;
23
+ extra?: Record<string, unknown>;
24
+ }
25
+ /** Options accepted by {@link setupLogging}. */
26
+ interface SetupLoggingOptions {
27
+ /** Enable colored output on the console. Default: `true`. */
28
+ colored?: boolean;
29
+ /** Box style for console output. Default: `null` (no box). */
30
+ style?: Style | null;
31
+ /** Minimum log level for the console handler. Default: `"DEBUG"`. */
32
+ level?: Level;
33
+ /** Box width in characters (for `"short-fixed"` and `"long-boxed"`). Default: `100`. */
34
+ width?: number;
35
+ /** Date/time format string using `strftime` tokens. Default: `"%Y-%m-%d %H:%M:%S"`. */
36
+ datefmt?: string;
37
+ /** Override the default color for each log level. */
38
+ logColors?: Partial<Record<Level, string>>;
39
+ /** Path to a log file. ANSI codes are stripped automatically. Default: `null`. */
40
+ file?: string | null;
41
+ /** Minimum log level for the file handler. Defaults to the console `level`. */
42
+ fileLevel?: Level | null;
43
+ /** Logger name. `""` for root logger. Default: `""`. */
44
+ name?: string;
45
+ /**
46
+ * Capture file path and line number of the caller on each log call.
47
+ * Disable for higher throughput in production. Default: `true`.
48
+ */
49
+ captureCallerInfo?: boolean;
50
+ /**
51
+ * How `extra` fields are rendered in the log output.
52
+ * Only applies to no-style (plain/colored) and `"long-boxed"` outputs.
53
+ * Default: `"inline"`.
54
+ */
55
+ extraFormat?: ExtraFormat;
56
+ }
57
+ /** Interface that all formatters implement. */
58
+ interface Formatter {
59
+ format(record: LogRecord): string;
60
+ }
61
+ /** Interface that all handlers implement. */
62
+ interface Handler {
63
+ readonly level: number;
64
+ formatter: Formatter;
65
+ emit(record: LogRecord): void;
66
+ /** Release any resources held by this handler (e.g. file streams). */
67
+ close(): void;
68
+ }
69
+
70
+ /**
71
+ * Logger class for crisplogs.
72
+ *
73
+ * Mirrors Python's `logging.Logger` API with level-specific methods
74
+ * and automatic caller-info capture.
75
+ */
76
+
77
+ declare class Logger {
78
+ readonly name: string;
79
+ private _level;
80
+ private _handlers;
81
+ private _captureCallerInfo;
82
+ constructor(name: string, level?: number, captureCallerInfo?: boolean);
83
+ get level(): number;
84
+ set level(val: number);
85
+ get handlers(): readonly Handler[];
86
+ addHandler(handler: Handler): void;
87
+ removeHandler(handler: Handler): boolean;
88
+ clearHandlers(): void;
89
+ isEnabledFor(level: Level): boolean;
90
+ private _log;
91
+ debug(message: string, extra?: Record<string, unknown>): void;
92
+ info(message: string, extra?: Record<string, unknown>): void;
93
+ warning(message: string, extra?: Record<string, unknown>): void;
94
+ /** Alias for {@link warning} to match Node.js conventions. */
95
+ warn(message: string, extra?: Record<string, unknown>): void;
96
+ error(message: string, extra?: Record<string, unknown>): void;
97
+ critical(message: string, extra?: Record<string, unknown>): void;
98
+ /**
99
+ * Log with an explicit level.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * logger.log("INFO", "Server started");
104
+ * ```
105
+ */
106
+ log(level: Level, message: string, extra?: Record<string, unknown>): void;
107
+ }
108
+
109
+ /**
110
+ * Formatter for crisplogs.
111
+ *
112
+ * A single LogFormatter class covers all output styles via options.
113
+ */
114
+
115
+ /** Options accepted by {@link LogFormatter}. */
116
+ interface FormatterOptions {
117
+ datefmt: string;
118
+ logColors: Record<string, string>;
119
+ colored: boolean;
120
+ /** How extra fields are rendered. Only applies when `box` is false or `wordWrap` is true. */
121
+ extraFormat?: ExtraFormat;
122
+ /** Draw a box around each log entry. Default: `false`. */
123
+ box?: boolean;
124
+ /**
125
+ * Full border (`┌─┐ │ └─┘`) instead of left-border only (`┌─ │ └─`).
126
+ * Only applies when `box` is `true`. Default: `false`.
127
+ */
128
+ fullBorder?: boolean;
129
+ /**
130
+ * Box width in characters, or `"auto"` to size to the longest line.
131
+ * Only applies when `box` is `true`. Default: `100`.
132
+ */
133
+ width?: number | "auto";
134
+ /**
135
+ * Word-wrap long lines within the box.
136
+ * Only applies when `box` is `true`. Default: `false`.
137
+ */
138
+ wordWrap?: boolean;
139
+ }
140
+ /**
141
+ * Single configurable log formatter.
142
+ *
143
+ * Covers all output styles through constructor options:
144
+ *
145
+ * | Equivalent old class | Options |
146
+ * |-----------------------------|--------------------------------------------------|
147
+ * | `ColoredLogFormatter` | `{ box: false }` |
148
+ * | `ShortFixedBoxFormatter` | `{ box: true, width: N }` |
149
+ * | `ShortDynamicBoxFormatter` | `{ box: true, fullBorder: true, width: "auto" }` |
150
+ * | `LongBoxedFormatter` | `{ box: true, wordWrap: true, width: N }` |
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * // Colored, no box (default style)
155
+ * new LogFormatter({ datefmt, logColors, colored: true });
156
+ *
157
+ * // Full border, auto width
158
+ * new LogFormatter({ datefmt, logColors, colored: true, box: true, fullBorder: true, width: "auto" });
159
+ *
160
+ * // Word-wrapped box with JSON extras
161
+ * new LogFormatter({ datefmt, logColors, colored: true, box: true, wordWrap: true, width: 100, extraFormat: "json" });
162
+ * ```
163
+ */
164
+ declare class LogFormatter implements Formatter {
165
+ private opts;
166
+ constructor(opts: FormatterOptions);
167
+ format(record: LogRecord): string;
168
+ }
169
+
170
+ /**
171
+ * Custom log handlers for crisplogs.
172
+ *
173
+ * Provides handlers that produce clean output for both console and file destinations.
174
+ */
175
+
176
+ /**
177
+ * Console handler that writes formatted log records to stdout.
178
+ */
179
+ declare class ConsoleHandler implements Handler {
180
+ readonly level: number;
181
+ formatter: Formatter;
182
+ constructor(level: number, formatter: Formatter);
183
+ emit(record: LogRecord): void;
184
+ close(): void;
185
+ }
186
+ /**
187
+ * File handler that strips ANSI color codes before writing.
188
+ *
189
+ * Logs written to files should be plain text for readability in editors,
190
+ * log aggregators, and CI systems. This handler removes all ANSI escape
191
+ * sequences before writing each record.
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * const handler = new CleanFileHandler("app.log", LEVEL_VALUES.WARNING, formatter);
196
+ * ```
197
+ */
198
+ declare class CleanFileHandler implements Handler {
199
+ readonly level: number;
200
+ formatter: Formatter;
201
+ private stream;
202
+ constructor(filename: string, level: number, formatter: Formatter);
203
+ emit(record: LogRecord): void;
204
+ close(): void;
205
+ }
206
+
207
+ /**
208
+ * Internal utilities for crisplogs.
209
+ */
210
+ /**
211
+ * Remove all ANSI escape sequences from a string.
212
+ *
213
+ * @example
214
+ * stripAnsi("\x1b[32mHello\x1b[0m") // "Hello"
215
+ */
216
+ declare function stripAnsi(text: string): string;
217
+
218
+ /** Default color scheme applied to each log level. */
219
+ declare const DEFAULT_LOG_COLORS: Record<Level, string>;
220
+
221
+ /**
222
+ * crisplogs - Beautiful, colored, and boxed logging for Node.js.
223
+ *
224
+ * @example
225
+ * ```ts
226
+ * import { setupLogging } from "crisplogs";
227
+ *
228
+ * const logger = setupLogging();
229
+ * logger.info("Hello from crisplogs!");
230
+ * ```
231
+ *
232
+ * @example
233
+ * ```ts
234
+ * const logger = setupLogging({ style: "long-boxed", file: "app.log" });
235
+ * logger.warning("Disk usage high", { usage: "85%" });
236
+ * ```
237
+ */
238
+
239
+ declare const VERSION: string;
240
+ /**
241
+ * Configure logging with colors and optional box formatting in one call.
242
+ *
243
+ * This is the main entry point for crisplogs. Call it once at application
244
+ * startup to configure the root (or named) logger.
245
+ *
246
+ * @returns The configured {@link Logger} instance.
247
+ */
248
+ declare function setupLogging(options?: SetupLoggingOptions): Logger;
249
+ /**
250
+ * Tear down all loggers, closing their handlers and clearing the registry.
251
+ * Useful in tests or when reconfiguring logging at runtime.
252
+ */
253
+ declare function resetLogging(): void;
254
+ /**
255
+ * Remove a single logger from the registry by name.
256
+ * Returns `true` if the logger existed and was removed.
257
+ */
258
+ declare function removeLogger(name: string): boolean;
259
+ /**
260
+ * Retrieve a previously configured logger by name, or create one
261
+ * that inherits the root logger's handlers.
262
+ *
263
+ * @example
264
+ * ```ts
265
+ * setupLogging(); // configure root
266
+ * const logger = getLogger("myapp"); // inherits root handlers
267
+ * logger.info("works");
268
+ * ```
269
+ */
270
+ declare function getLogger(name?: string): Logger;
271
+
272
+ export { CleanFileHandler, ConsoleHandler, DEFAULT_LOG_COLORS, type ExtraFormat, type Formatter, type FormatterOptions, type Handler, LEVEL_VALUES, type Level, LogFormatter, type LogRecord, Logger, type SetupLoggingOptions, type Style, VERSION, getLogger, removeLogger, resetLogging, setupLogging, stripAnsi };