@ubercode/chronicler 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 +263 -0
- package/dist/cli.js +6696 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +769 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +427 -0
- package/dist/index.d.ts +427 -0
- package/dist/index.js +759 -0
- package/dist/index.js.map +1 -0
- package/package.json +112 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MichaelLeeHobbs
|
|
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,263 @@
|
|
|
1
|
+
# @ubercode/chronicler
|
|
2
|
+
|
|
3
|
+
> A TypeScript-first, strongly-typed logging toolkit that enforces consistent, documented events with correlations and forks.
|
|
4
|
+
|
|
5
|
+
- Node: 20+ (ES2022)
|
|
6
|
+
- Bundles: ESM + CJS with types
|
|
7
|
+
- Runtime only, framework-agnostic
|
|
8
|
+
|
|
9
|
+
## Why Chronicler?
|
|
10
|
+
|
|
11
|
+
- Define events once with keys, levels, fields, and docs; get type-safe logging everywhere
|
|
12
|
+
- Enforce required/optional fields and flag type issues at runtime
|
|
13
|
+
- Correlate related logs with auto start/complete/fail/timeout events and durations
|
|
14
|
+
- Fork work into sub-operations with hierarchical fork IDs
|
|
15
|
+
- Route events to multiple backends with filter-based routing
|
|
16
|
+
- Auto-generate Markdown or JSON documentation from event definitions via the CLI
|
|
17
|
+
- Structured payloads ready for ingestion (e.g., CloudWatch, ELK, Datadog)
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```powershell
|
|
22
|
+
pnpm add @ubercode/chronicler
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Node 20+ required.
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import {
|
|
31
|
+
createChronicle,
|
|
32
|
+
defineEvent,
|
|
33
|
+
defineEventGroup,
|
|
34
|
+
defineCorrelationGroup,
|
|
35
|
+
field,
|
|
36
|
+
} from '@ubercode/chronicler';
|
|
37
|
+
|
|
38
|
+
// 1) Define events
|
|
39
|
+
const system = defineEventGroup({
|
|
40
|
+
key: 'system',
|
|
41
|
+
type: 'system',
|
|
42
|
+
doc: 'System lifecycle events',
|
|
43
|
+
events: {
|
|
44
|
+
startup: defineEvent({
|
|
45
|
+
key: 'system.startup',
|
|
46
|
+
level: 'info',
|
|
47
|
+
message: 'Application started',
|
|
48
|
+
doc: 'Emitted when the app boots',
|
|
49
|
+
fields: { port: field.number().doc('Listening port') },
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const request = defineCorrelationGroup({
|
|
55
|
+
key: 'api.request',
|
|
56
|
+
type: 'correlation',
|
|
57
|
+
doc: 'HTTP request handling',
|
|
58
|
+
timeout: 30_000, // default 300s if omitted
|
|
59
|
+
events: {
|
|
60
|
+
validated: defineEvent({
|
|
61
|
+
key: 'api.request.validated',
|
|
62
|
+
level: 'info',
|
|
63
|
+
message: 'Request validated',
|
|
64
|
+
doc: 'Validation passed',
|
|
65
|
+
fields: {
|
|
66
|
+
method: field.string().doc('HTTP method'),
|
|
67
|
+
path: field.string().doc('Request path'),
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// 2) Create a chronicle (uses console backend by default)
|
|
74
|
+
const chronicle = createChronicle({
|
|
75
|
+
metadata: { service: 'api', env: 'dev' },
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 3) Emit typed events
|
|
79
|
+
chronicle.event(system.events.startup, { port: 3000 });
|
|
80
|
+
|
|
81
|
+
// 4) Correlate work
|
|
82
|
+
const corr = chronicle.startCorrelation(request, { requestId: 'r-123' });
|
|
83
|
+
corr.event(request.events.validated, { method: 'GET', path: '/' });
|
|
84
|
+
|
|
85
|
+
// Fork parallel steps
|
|
86
|
+
const forkA = corr.fork({ step: 'A' });
|
|
87
|
+
forkA.event(system.events.startup, { port: 0 });
|
|
88
|
+
|
|
89
|
+
// Complete the correlation (emits api.request.complete with duration)
|
|
90
|
+
corr.complete();
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Backends
|
|
94
|
+
|
|
95
|
+
### Console (default)
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { createConsoleBackend } from '@ubercode/chronicler';
|
|
99
|
+
|
|
100
|
+
const backend = createConsoleBackend();
|
|
101
|
+
// Maps: fatal/critical/alert/error → console.error, warn → console.warn,
|
|
102
|
+
// audit/info → console.info, debug/trace → console.debug
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Partial backend with fallbacks
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { createBackend } from '@ubercode/chronicler';
|
|
109
|
+
|
|
110
|
+
// Only provide the levels you care about.
|
|
111
|
+
// Missing levels fall back through a chain (e.g. fatal → critical → error → warn → info),
|
|
112
|
+
// then to console if nothing matches.
|
|
113
|
+
const backend = createBackend({
|
|
114
|
+
error: (msg, payload) => myErrorTracker.capture(msg, payload),
|
|
115
|
+
info: (msg, payload) => myLogger.info(msg, payload),
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Router backend (multiple streams)
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
import { createRouterBackend } from '@ubercode/chronicler';
|
|
123
|
+
|
|
124
|
+
// Route events to different backends based on filters.
|
|
125
|
+
// Events fan out to ALL matching routes (not first-match-wins).
|
|
126
|
+
const backend = createRouterBackend([
|
|
127
|
+
{ backend: auditBackend, filter: (_lvl, p) => p.eventKey.startsWith('admin.') },
|
|
128
|
+
{ backend: httpBackend, filter: (_lvl, p) => p.eventKey.startsWith('http.') },
|
|
129
|
+
{
|
|
130
|
+
backend: mainBackend,
|
|
131
|
+
filter: (_lvl, p) => !p.eventKey.startsWith('admin.') && !p.eventKey.startsWith('http.'),
|
|
132
|
+
},
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
const chronicle = createChronicle({ backend, metadata: { app: 'my-app' } });
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## API highlights
|
|
139
|
+
|
|
140
|
+
### Core
|
|
141
|
+
|
|
142
|
+
- `createChronicle({ backend?, metadata, strict?, minLevel?, limits?, correlationIdGenerator? })`
|
|
143
|
+
- `chronicle.event(eventDef, fields)` — emit a typed event
|
|
144
|
+
- `chronicle.log(level, message, fields?)` — untyped escape hatch
|
|
145
|
+
- `chronicle.addContext(context)` — add metadata to all subsequent events
|
|
146
|
+
- `chronicle.startCorrelation(corrGroup, context?)` — start a correlation
|
|
147
|
+
- `chronicle.fork(context?)` — create an isolated child chronicle
|
|
148
|
+
|
|
149
|
+
### Definitions
|
|
150
|
+
|
|
151
|
+
- `defineEvent({ key, level, message, doc?, fields? })`
|
|
152
|
+
- `defineEventGroup({ key, type: 'system', doc?, events?, groups? })`
|
|
153
|
+
- `defineCorrelationGroup({ key, type: 'correlation', doc?, timeout?, events?, groups? })`
|
|
154
|
+
|
|
155
|
+
### Field builders
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
field.string(); // required string
|
|
159
|
+
field.number().optional(); // optional number
|
|
160
|
+
field.boolean().doc('...'); // required boolean with documentation
|
|
161
|
+
field.error(); // Error | string, serialized to stack trace
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Log levels
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
const LOG_LEVELS = {
|
|
168
|
+
fatal: 0, // System is unusable
|
|
169
|
+
critical: 1, // Critical conditions requiring immediate attention
|
|
170
|
+
alert: 2, // Action must be taken immediately
|
|
171
|
+
error: 3, // Error conditions
|
|
172
|
+
warn: 4, // Warning conditions
|
|
173
|
+
audit: 5, // Audit trail events (compliance, security)
|
|
174
|
+
info: 6, // Informational messages
|
|
175
|
+
debug: 7, // Debug-level messages
|
|
176
|
+
trace: 8, // Trace-level messages (very verbose)
|
|
177
|
+
} as const;
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Filter events with `minLevel`:
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
const chronicle = createChronicle({
|
|
184
|
+
metadata: {},
|
|
185
|
+
minLevel: 'warn', // only fatal, critical, alert, error, warn are emitted
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Strict mode
|
|
190
|
+
|
|
191
|
+
When `strict: true`, Chronicler throws a `ChroniclerError` with code `FIELD_VALIDATION` if events have missing required fields, type mismatches, or invalid values. Useful for CI/CD enforcement and testing.
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
const chronicle = createChronicle({
|
|
195
|
+
metadata: {},
|
|
196
|
+
strict: true, // throws on field validation errors
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Reserved fields
|
|
201
|
+
|
|
202
|
+
These payload field names cannot be used in metadata or context: `eventKey`, `level`, `message`, `correlationId`, `forkId`, `timestamp`, `fields`, `_validation`.
|
|
203
|
+
|
|
204
|
+
### Error serialization
|
|
205
|
+
|
|
206
|
+
Fields declared as `field.error()` accept `Error | string` and are serialized to the stack trace string (or message if no stack). Safe to ship to log sinks.
|
|
207
|
+
|
|
208
|
+
### String sanitization
|
|
209
|
+
|
|
210
|
+
All string field values are automatically sanitized — ANSI escape sequences are stripped and newlines are replaced with `\n`. This prevents log injection attacks.
|
|
211
|
+
|
|
212
|
+
## Using with Winston
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
import winston from 'winston';
|
|
216
|
+
import { createBackend, createChronicle } from '@ubercode/chronicler';
|
|
217
|
+
|
|
218
|
+
const logger = winston.createLogger({
|
|
219
|
+
level: 'info',
|
|
220
|
+
format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
|
|
221
|
+
transports: [new winston.transports.Console()],
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// createBackend handles fallback chains automatically
|
|
225
|
+
const backend = createBackend({
|
|
226
|
+
error: (msg, payload) => {
|
|
227
|
+
logger.error(msg, payload);
|
|
228
|
+
},
|
|
229
|
+
warn: (msg, payload) => {
|
|
230
|
+
logger.warn(msg, payload);
|
|
231
|
+
},
|
|
232
|
+
info: (msg, payload) => {
|
|
233
|
+
logger.info(msg, payload);
|
|
234
|
+
},
|
|
235
|
+
debug: (msg, payload) => {
|
|
236
|
+
logger.debug(msg, payload);
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
const chronicle = createChronicle({
|
|
241
|
+
backend,
|
|
242
|
+
metadata: { service: 'my-app', env: 'production' },
|
|
243
|
+
});
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
See `examples/winston-app` for a full multi-stream setup using `createRouterBackend`.
|
|
247
|
+
|
|
248
|
+
## CLI
|
|
249
|
+
|
|
250
|
+
```powershell
|
|
251
|
+
pnpm exec tsx src/cli/index.ts validate
|
|
252
|
+
pnpm exec tsx src/cli/index.ts docs --format markdown --output docs/events.md
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## Scripts
|
|
256
|
+
|
|
257
|
+
- `pnpm run dev` – watch build via tsup
|
|
258
|
+
- `pnpm run build` – clean & create production bundles
|
|
259
|
+
- `pnpm run lint` – ESLint with TypeScript rules
|
|
260
|
+
- `pnpm run format` – Prettier formatting check
|
|
261
|
+
- `pnpm run test` – Vitest unit/integration tests
|
|
262
|
+
- `pnpm run coverage` – Coverage report
|
|
263
|
+
- `pnpm run check` – lint + typecheck + tests
|