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.
- package/LICENSE +21 -0
- package/README.md +333 -0
- package/dist/index.d.mts +272 -0
- package/dist/index.d.ts +272 -0
- package/dist/index.js +530 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +482 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 crisplogs contributors
|
|
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/README.md
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
# crisplogs
|
|
2
|
+
|
|
3
|
+
Beautiful, colored, and boxed logging for Node.js. One function call to set up production-ready logs with colors, box decorations, and file output.
|
|
4
|
+
|
|
5
|
+
**Zero runtime dependencies.** Works with Node.js 16+. Ships with full TypeScript types.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install crisplogs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { setupLogging } from "crisplogs";
|
|
17
|
+
|
|
18
|
+
const logger = setupLogging();
|
|
19
|
+
|
|
20
|
+
logger.debug("This is a debug message");
|
|
21
|
+
logger.info("Server started successfully");
|
|
22
|
+
logger.warning("Disk usage at 85%");
|
|
23
|
+
logger.error("Failed to connect to database");
|
|
24
|
+
logger.critical("System is shutting down");
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Styles
|
|
28
|
+
|
|
29
|
+
### Colored (default)
|
|
30
|
+
|
|
31
|
+
Plain colored output. Each log level gets its own color for easy scanning.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const logger = setupLogging({ colored: true });
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
INFO 2025-09-08 12:30:45 [root] /app/main.ts:25 - Server started
|
|
39
|
+
ERROR 2025-09-08 12:31:12 [root] /app/db.ts:45 - Connection failed
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Short Fixed Box
|
|
43
|
+
|
|
44
|
+
Fixed-width box with left border only. Good for short messages.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const logger = setupLogging({ style: "short-fixed" });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
┌──────────────────────────────────────────────────────────────────────────────────
|
|
52
|
+
│ INFO 2025-09-08 12:30:45 [root] /app/main.ts:25 - Server started
|
|
53
|
+
└──────────────────────────────────────────────────────────────────────────────────
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Short Dynamic Box
|
|
57
|
+
|
|
58
|
+
Dynamic-width box with full border. Width adjusts to fit the message.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const logger = setupLogging({ style: "short-dynamic" });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
┌──────────────────────────────────────────────────────────────┐
|
|
66
|
+
│ INFO 2025-09-08 12:30:45 [root] - Server started │
|
|
67
|
+
└──────────────────────────────────────────────────────────────┘
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Long Boxed
|
|
71
|
+
|
|
72
|
+
Word-wrapped box with left border. Best for long messages and extra fields.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const logger = setupLogging({ style: "long-boxed" });
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
┌──────────────────────────────────────────────────────────────────────────────────
|
|
80
|
+
│ INFO 2025-09-08 12:34:56 [root] - This is a long message that wraps neatly
|
|
81
|
+
│ across multiple lines without breaking words in half. [user_id=42 action=login]
|
|
82
|
+
└──────────────────────────────────────────────────────────────────────────────────
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
To pass extra fields:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
logger.info("User logged in", { userId: 42, action: "login" });
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## API Reference
|
|
92
|
+
|
|
93
|
+
### `setupLogging(options?)`
|
|
94
|
+
|
|
95
|
+
Configure logging with colors and optional box formatting in one call. Returns a `Logger` instance.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const logger = setupLogging({
|
|
99
|
+
colored: true,
|
|
100
|
+
style: "long-boxed",
|
|
101
|
+
level: "DEBUG",
|
|
102
|
+
width: 100,
|
|
103
|
+
datefmt: "%Y-%m-%d %H:%M:%S",
|
|
104
|
+
logColors: { INFO: "bold_green" },
|
|
105
|
+
extraFormat: "json",
|
|
106
|
+
file: "app.log",
|
|
107
|
+
fileLevel: "WARNING",
|
|
108
|
+
name: "myapp",
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### `getLogger(name?)`
|
|
113
|
+
|
|
114
|
+
Retrieve a previously configured logger by name. If no logger exists for that name, one is created inheriting the root logger's handlers.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
setupLogging(); // configure root logger
|
|
118
|
+
const logger = getLogger("myapp"); // inherits root config
|
|
119
|
+
logger.info("works");
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Logger Methods
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
logger.debug(message, extra?)
|
|
126
|
+
logger.info(message, extra?)
|
|
127
|
+
logger.warning(message, extra?)
|
|
128
|
+
logger.warn(message, extra?) // alias for warning
|
|
129
|
+
logger.error(message, extra?)
|
|
130
|
+
logger.critical(message, extra?)
|
|
131
|
+
logger.log(level, message, extra?) // explicit level
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The `extra` parameter is an optional `Record<string, unknown>`. It appears in the log output for the default (no style) and `"long-boxed"` styles. Format is controlled by the `extraFormat` option.
|
|
135
|
+
|
|
136
|
+
## Options
|
|
137
|
+
|
|
138
|
+
### `colored` (boolean, default: `true`)
|
|
139
|
+
|
|
140
|
+
Enable or disable colored console output. Set to `false` for CI, piped output, or plain terminals.
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
setupLogging({ colored: false });
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### `style` (string or null, default: `null`)
|
|
147
|
+
|
|
148
|
+
Box style. One of `"short-fixed"`, `"short-dynamic"`, `"long-boxed"`, or `null` for no box.
|
|
149
|
+
|
|
150
|
+
You can combine `colored` and `style` independently:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
setupLogging({ colored: true, style: "long-boxed" }); // colored + boxed
|
|
154
|
+
setupLogging({ colored: false, style: "short-fixed" }); // plain + boxed
|
|
155
|
+
setupLogging({ colored: true, style: null }); // colored, no box
|
|
156
|
+
setupLogging({ colored: false, style: null }); // plain, no box
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### `level` (string, default: `"DEBUG"`)
|
|
160
|
+
|
|
161
|
+
Minimum log level for console output. One of `"DEBUG"`, `"INFO"`, `"WARNING"`, `"ERROR"`, `"CRITICAL"`.
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
setupLogging({ level: "INFO" }); // hides DEBUG messages on console
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### `width` (number, default: `100`)
|
|
168
|
+
|
|
169
|
+
Box width in characters. Only applies to `"short-fixed"` and `"long-boxed"` styles.
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
setupLogging({ style: "long-boxed", width: 120 });
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### `datefmt` (string, default: `"%Y-%m-%d %H:%M:%S"`)
|
|
176
|
+
|
|
177
|
+
Date/time format using Python-compatible `strftime` tokens.
|
|
178
|
+
|
|
179
|
+
Supported tokens: `%Y`, `%m`, `%d`, `%H`, `%M`, `%S`, `%I`, `%p`, `%f`, `%j`, `%a`, `%A`, `%b`, `%B`, `%%`.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
setupLogging({ datefmt: "%H:%M:%S" }); // short: 12:30:45
|
|
183
|
+
setupLogging({ datefmt: "%d/%m/%Y %H:%M" }); // European: 08/09/2025 12:30
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### `logColors` (object, default: built-in scheme)
|
|
187
|
+
|
|
188
|
+
Override colors for specific log levels. Uses color strings compatible with Python's `colorlog`:
|
|
189
|
+
|
|
190
|
+
| Color | Example |
|
|
191
|
+
|-------|---------|
|
|
192
|
+
| Basic | `"red"`, `"green"`, `"cyan"`, `"yellow"`, `"blue"`, `"white"`, `"black"` |
|
|
193
|
+
| Bold | `"bold_red"`, `"bold_green"` |
|
|
194
|
+
| Dim | `"thin_white"`, `"dim_white"` |
|
|
195
|
+
| Background | `"bg_red"`, `"bg_white"` |
|
|
196
|
+
| Combined | `"bold_red,bg_white"` |
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
setupLogging({
|
|
200
|
+
logColors: {
|
|
201
|
+
DEBUG: "thin_white",
|
|
202
|
+
INFO: "bold_green",
|
|
203
|
+
WARNING: "bold_yellow",
|
|
204
|
+
ERROR: "bold_red,bg_white",
|
|
205
|
+
CRITICAL: "bold_white,bg_red",
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Default color scheme:
|
|
211
|
+
|
|
212
|
+
| Level | Color |
|
|
213
|
+
|-------|-------|
|
|
214
|
+
| DEBUG | `cyan` |
|
|
215
|
+
| INFO | `green` |
|
|
216
|
+
| WARNING | `yellow` |
|
|
217
|
+
| ERROR | `red` |
|
|
218
|
+
| CRITICAL | `bold_red` |
|
|
219
|
+
|
|
220
|
+
### `file` (string or null, default: `null`)
|
|
221
|
+
|
|
222
|
+
Path to a log file. ANSI color codes are automatically stripped so the file stays clean and readable.
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
setupLogging({ file: "logs/app.log" });
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### `fileLevel` (string or null, default: same as `level`)
|
|
229
|
+
|
|
230
|
+
Separate minimum level for the file handler. Useful when you want verbose console output but only important messages in the file.
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
setupLogging({ level: "DEBUG", file: "app.log", fileLevel: "WARNING" });
|
|
234
|
+
// Console shows everything, file only gets WARNING and above.
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### `name` (string, default: `""`)
|
|
238
|
+
|
|
239
|
+
Logger name. Empty string configures the root logger (affects all loggers retrieved via `getLogger`). Pass a name for a specific logger.
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
const logger = setupLogging({ name: "myapp" });
|
|
243
|
+
logger.info("from myapp"); // shows [myapp] in output
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### `extraFormat` (string, default: `"inline"`)
|
|
247
|
+
|
|
248
|
+
How `extra` fields are rendered in the log output. Only applies to the default (no style) and `"long-boxed"` outputs.
|
|
249
|
+
|
|
250
|
+
| Value | Output |
|
|
251
|
+
|-------|--------|
|
|
252
|
+
| `"inline"` | `[userId=42 action=login]` |
|
|
253
|
+
| `"json"` | `{"userId":42,"action":"login"}` |
|
|
254
|
+
| `"pretty"` | Formatted multi-line JSON |
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
setupLogging({ extraFormat: "json" });
|
|
258
|
+
// INFO 2025-09-08 12:30:45 [root] app.ts:10 - User logged in {"userId":42}
|
|
259
|
+
|
|
260
|
+
setupLogging({ style: "long-boxed", extraFormat: "pretty" });
|
|
261
|
+
// ┌──────────────────────────────────────
|
|
262
|
+
// │ INFO ... - User logged in
|
|
263
|
+
// │ {
|
|
264
|
+
// │ "userId": 42
|
|
265
|
+
// │ }
|
|
266
|
+
// └──────────────────────────────────────
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Full Example
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
import { setupLogging } from "crisplogs";
|
|
273
|
+
|
|
274
|
+
// Colored + boxed console, warnings+ to file
|
|
275
|
+
const logger = setupLogging({
|
|
276
|
+
colored: true,
|
|
277
|
+
style: "long-boxed",
|
|
278
|
+
level: "DEBUG",
|
|
279
|
+
width: 110,
|
|
280
|
+
datefmt: "%H:%M:%S",
|
|
281
|
+
file: "app.log",
|
|
282
|
+
fileLevel: "WARNING",
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
logger.debug("Loading configuration...");
|
|
286
|
+
logger.info("Server started on port 8000");
|
|
287
|
+
logger.warning("Cache miss rate is high", { rate: "45%" });
|
|
288
|
+
logger.error("Payment processing failed", { orderId: 9912 });
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## CommonJS Usage
|
|
292
|
+
|
|
293
|
+
```js
|
|
294
|
+
const { setupLogging } = require("crisplogs");
|
|
295
|
+
|
|
296
|
+
const logger = setupLogging();
|
|
297
|
+
logger.info("Hello from CommonJS!");
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## Exported Types
|
|
301
|
+
|
|
302
|
+
All types are available for TypeScript users:
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
import type {
|
|
306
|
+
Style, // "short-fixed" | "short-dynamic" | "long-boxed"
|
|
307
|
+
Level, // "DEBUG" | "INFO" | "WARNING" | "ERROR" | "CRITICAL"
|
|
308
|
+
ExtraFormat, // "inline" | "json" | "pretty"
|
|
309
|
+
SetupLoggingOptions,
|
|
310
|
+
LogRecord,
|
|
311
|
+
Formatter,
|
|
312
|
+
Handler,
|
|
313
|
+
} from "crisplogs";
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Exports
|
|
317
|
+
|
|
318
|
+
| Export | Description |
|
|
319
|
+
|--------|-------------|
|
|
320
|
+
| `setupLogging` | Main entry point - configure logging in one call |
|
|
321
|
+
| `getLogger` | Retrieve a logger by name |
|
|
322
|
+
| `Logger` | Logger class |
|
|
323
|
+
| `LogFormatter` | Configurable formatter — covers all styles via options |
|
|
324
|
+
| `ConsoleHandler` | Stdout handler |
|
|
325
|
+
| `CleanFileHandler` | File handler with ANSI stripping |
|
|
326
|
+
| `stripAnsi` | Remove ANSI escape sequences from a string |
|
|
327
|
+
| `DEFAULT_LOG_COLORS` | Default color scheme |
|
|
328
|
+
| `LEVEL_VALUES` | Numeric values for each log level |
|
|
329
|
+
| `VERSION` | Package version string |
|
|
330
|
+
|
|
331
|
+
## License
|
|
332
|
+
|
|
333
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -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 };
|