konsole-logger 3.0.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/README.md +284 -0
- package/dist/index.d.ts +605 -0
- package/dist/konsole.js +924 -0
- package/dist/konsole.umd.cjs +110 -0
- package/package.json +72 -0
package/README.md
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# Konsole
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+
**Structured, namespaced logging for browser and Node.js**
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/konsole-logger)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
10
|
+
</div>
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
- **Browser + Node.js** — works everywhere without configuration
|
|
15
|
+
- **Six numeric log levels** — trace / debug / info / warn / error / fatal
|
|
16
|
+
- **Structured output** — consistent JSON schema, compatible with Datadog, Loki, CloudWatch
|
|
17
|
+
- **Beautiful terminal output** — ANSI colors on TTY, NDJSON in pipes, styled badges in DevTools
|
|
18
|
+
- **Child loggers** — attach request-scoped context that flows into every log line
|
|
19
|
+
- **Flexible transports** — HTTP, file, stream, or console; per-transport filter and transform
|
|
20
|
+
- **Circular buffer** — memory-efficient in-process log history
|
|
21
|
+
- **TypeScript first** — full type safety, zero runtime dependencies
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install konsole-logger
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
yarn add konsole-logger
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm add konsole-logger
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { Konsole } from 'konsole-logger';
|
|
41
|
+
|
|
42
|
+
const logger = new Konsole({ namespace: 'MyApp' });
|
|
43
|
+
|
|
44
|
+
logger.info('Server started', { port: 3000 });
|
|
45
|
+
logger.warn('Config file missing, using defaults');
|
|
46
|
+
logger.error(new Error('Database connection failed'));
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**Terminal output (TTY):**
|
|
50
|
+
```
|
|
51
|
+
10:23:45 INF [MyApp] Server started port=3000
|
|
52
|
+
10:23:45 WRN [MyApp] Config file missing, using defaults
|
|
53
|
+
10:23:45 ERR [MyApp] Database connection failed
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**Pipe / CI output (NDJSON):**
|
|
57
|
+
```json
|
|
58
|
+
{"level":30,"levelName":"info","time":"2025-03-16T10:23:45.000Z","namespace":"MyApp","msg":"Server started","port":3000}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Log Levels
|
|
62
|
+
|
|
63
|
+
| Method | Level | Value |
|
|
64
|
+
|--------|-------|-------|
|
|
65
|
+
| `logger.trace()` | trace | 10 |
|
|
66
|
+
| `logger.debug()` | debug | 20 |
|
|
67
|
+
| `logger.info()` / `logger.log()` | info | 30 |
|
|
68
|
+
| `logger.warn()` | warn | 40 |
|
|
69
|
+
| `logger.error()` | error | 50 |
|
|
70
|
+
| `logger.fatal()` | fatal | 60 |
|
|
71
|
+
|
|
72
|
+
Set a minimum threshold — entries below it are discarded entirely:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
const logger = new Konsole({ namespace: 'App', level: 'info' });
|
|
76
|
+
|
|
77
|
+
logger.trace('loop tick'); // dropped — below threshold
|
|
78
|
+
logger.debug('cache miss'); // dropped — below threshold
|
|
79
|
+
logger.info('ready'); // ✅ logged
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Change the threshold at runtime:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
logger.setLevel('debug');
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Calling Conventions
|
|
89
|
+
|
|
90
|
+
All four styles work and produce the same structured `LogEntry`:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
// 1. Simple string
|
|
94
|
+
logger.info('Server started');
|
|
95
|
+
|
|
96
|
+
// 2. String + fields (recommended)
|
|
97
|
+
logger.info('Request received', { method: 'GET', path: '/users', ms: 42 });
|
|
98
|
+
|
|
99
|
+
// 3. Object-first with msg key
|
|
100
|
+
logger.info({ msg: 'Request received', method: 'GET', path: '/users' });
|
|
101
|
+
|
|
102
|
+
// 4. Error — message extracted, error stored in fields.err
|
|
103
|
+
logger.error(new Error('Connection refused'));
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Output Formats
|
|
107
|
+
|
|
108
|
+
The `format` option controls how logs are printed. `'auto'` (default) picks the right one for the environment:
|
|
109
|
+
|
|
110
|
+
| Format | Description |
|
|
111
|
+
|--------|-------------|
|
|
112
|
+
| `'auto'` | Browser → `browser`, Node.js TTY → `pretty`, Node.js pipe → `json` |
|
|
113
|
+
| `'pretty'` | ANSI-colored human-readable output |
|
|
114
|
+
| `'json'` | Newline-delimited JSON — aggregator-friendly |
|
|
115
|
+
| `'text'` | Plain text, no ANSI — for CI or log files |
|
|
116
|
+
| `'browser'` | Styled `%c` badges in DevTools |
|
|
117
|
+
| `'silent'` | No output; logs still stored in the buffer and sent to transports |
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
const logger = new Konsole({ namespace: 'App', format: 'silent' });
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Child Loggers
|
|
124
|
+
|
|
125
|
+
Create a child that automatically injects context into every entry it produces:
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
const logger = new Konsole({ namespace: 'API' });
|
|
129
|
+
|
|
130
|
+
// Per-request child
|
|
131
|
+
const req = logger.child({ requestId: 'req_abc', userId: 42 });
|
|
132
|
+
|
|
133
|
+
req.info('Request started', { path: '/users' });
|
|
134
|
+
// → INF [API] Request started requestId=req_abc userId=42 path=/users
|
|
135
|
+
|
|
136
|
+
// Nest further — bindings accumulate
|
|
137
|
+
const db = req.child({ component: 'postgres' });
|
|
138
|
+
db.debug('Query', { ms: 4 });
|
|
139
|
+
// → DBG [API] Query requestId=req_abc userId=42 component=postgres ms=4
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Child options:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
const child = logger.child(
|
|
146
|
+
{ requestId: 'req_abc' },
|
|
147
|
+
{ namespace: 'API:handler', level: 'warn' }
|
|
148
|
+
);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Children are ephemeral — not registered in `Konsole.instances`, share the parent's buffer.
|
|
152
|
+
|
|
153
|
+
## Transports
|
|
154
|
+
|
|
155
|
+
Ship logs to external destinations alongside (or instead of) console output:
|
|
156
|
+
|
|
157
|
+
### HTTP
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
const logger = new Konsole({
|
|
161
|
+
namespace: 'App',
|
|
162
|
+
transports: [
|
|
163
|
+
{
|
|
164
|
+
name: 'datadog',
|
|
165
|
+
url: 'https://http-intake.logs.datadoghq.com/v1/input',
|
|
166
|
+
headers: { 'DD-API-KEY': process.env.DD_API_KEY },
|
|
167
|
+
batchSize: 50,
|
|
168
|
+
flushInterval: 10000,
|
|
169
|
+
filter: (entry) => entry.levelValue >= 40, // warn+ only
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### File (Node.js)
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
import { Konsole, FileTransport } from 'konsole-logger';
|
|
179
|
+
|
|
180
|
+
const logger = new Konsole({
|
|
181
|
+
namespace: 'App',
|
|
182
|
+
transports: [
|
|
183
|
+
new FileTransport({ path: '/var/log/app.log' }),
|
|
184
|
+
],
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Stream
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
import { StreamTransport } from 'konsole-logger';
|
|
192
|
+
|
|
193
|
+
const logger = new Konsole({
|
|
194
|
+
namespace: 'App',
|
|
195
|
+
transports: [new StreamTransport({ stream: process.stdout, format: 'json' })],
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Add at runtime
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
logger.addTransport(new FileTransport({ path: './debug.log' }));
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Flush before exit (Node.js)
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
process.on('SIGTERM', async () => {
|
|
209
|
+
await logger.flushTransports();
|
|
210
|
+
process.exit(0);
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Configuration
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
new Konsole({
|
|
218
|
+
namespace?: string; // default: 'Global'
|
|
219
|
+
level?: LogLevelName; // default: 'trace' (no filtering)
|
|
220
|
+
format?: KonsoleFormat; // default: 'auto'
|
|
221
|
+
transports?: (Transport | TransportConfig)[];
|
|
222
|
+
maxLogs?: number; // default: 10000 (circular buffer size)
|
|
223
|
+
defaultBatchSize?: number; // default: 100 (viewLogs batch)
|
|
224
|
+
retentionPeriod?: number; // default: 172800000 (48 hours)
|
|
225
|
+
cleanupInterval?: number; // default: 3600000 (1 hour)
|
|
226
|
+
useWorker?: boolean; // default: false (browser only)
|
|
227
|
+
})
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## API Reference
|
|
231
|
+
|
|
232
|
+
### Instance methods
|
|
233
|
+
|
|
234
|
+
| Method | Description |
|
|
235
|
+
|--------|-------------|
|
|
236
|
+
| `trace / debug / info / log / warn / error / fatal` | Log at the given level |
|
|
237
|
+
| `child(bindings, options?)` | Create a child logger with merged bindings |
|
|
238
|
+
| `setLevel(level)` | Change minimum level at runtime |
|
|
239
|
+
| `getLogs()` | Return all entries from the circular buffer |
|
|
240
|
+
| `getLogsAsync()` | Async variant (for Web Worker mode) |
|
|
241
|
+
| `clearLogs()` | Empty the buffer |
|
|
242
|
+
| `viewLogs(batchSize?)` | Print a batch of stored logs to the console |
|
|
243
|
+
| `getStats()` | `{ logCount, capacity }` |
|
|
244
|
+
| `addTransport(transport)` | Attach a transport at runtime |
|
|
245
|
+
| `flushTransports()` | Flush all pending batches |
|
|
246
|
+
| `destroy()` | Flush, stop timers, deregister |
|
|
247
|
+
|
|
248
|
+
### Static methods
|
|
249
|
+
|
|
250
|
+
| Method | Description |
|
|
251
|
+
|--------|-------------|
|
|
252
|
+
| `Konsole.getLogger(namespace)` | Retrieve a registered logger |
|
|
253
|
+
| `Konsole.getNamespaces()` | List all registered namespaces |
|
|
254
|
+
| `Konsole.exposeToWindow()` | Expose `__Konsole` on `window` for browser debugging |
|
|
255
|
+
| `Konsole.enableGlobalPrint(enabled)` | Override output for all loggers |
|
|
256
|
+
| `Konsole.addGlobalTransport(transport)` | Add a transport to all existing loggers |
|
|
257
|
+
|
|
258
|
+
## Browser Debugging
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
// In app init:
|
|
262
|
+
Konsole.exposeToWindow();
|
|
263
|
+
|
|
264
|
+
// Then in DevTools console:
|
|
265
|
+
__Konsole.getLogger('Auth').viewLogs()
|
|
266
|
+
__Konsole.enableGlobalPrint(true) // unsilence all loggers
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Requirements
|
|
270
|
+
|
|
271
|
+
- **Node.js ≥ 18** for server-side use (native `fetch`). Older versions must pass `fetchImpl` to `TransportConfig`.
|
|
272
|
+
- `useWorker: true` is browser-only — silently ignored in Node.js.
|
|
273
|
+
|
|
274
|
+
## License
|
|
275
|
+
|
|
276
|
+
MIT © Sakti Kumar Chourasia
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
<div align="center">
|
|
281
|
+
|
|
282
|
+
🐛 [Report Bug](https://github.com/shakcho/Konsole/issues) · ✨ [Request Feature](https://github.com/shakcho/Konsole/issues)
|
|
283
|
+
|
|
284
|
+
</div>
|