@unhingged/logit 0.0.0-stage → 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Zoltán Balogh
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 CHANGED
@@ -1,3 +1,133 @@
1
- # Temporary Holding Version
1
+ # @unhingged/logit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Structured logging for Node.js, Next.js, Bun and the edge, in the mould of Serilog: you write a **message template**, the logger captures the **values** by name, and every sink — console, file, Seq, OTLP, your own — sees the same structured event.
4
+
5
+ ```bash
6
+ npm install @unhingged/logit
7
+ ```
8
+
9
+ ```ts
10
+ import { log } from '@unhingged/logit';
11
+
12
+ log.info('Order {OrderId} shipped to {@Address} in {Elapsed:0.0} ms', 1042, { city: 'Budapest', zip: '1011' }, 83.2);
13
+ ```
14
+
15
+ On a terminal:
16
+
17
+ ```
18
+ 08:12:03.123 INF Order 1042 shipped to { city: "Budapest", zip: "1011" } in 83.2 ms
19
+ ```
20
+
21
+ In a container, on Vercel, or anywhere stdout is not a TTY:
22
+
23
+ ```json
24
+ {"timestamp":"2026-10-08T08:12:03.123Z","level":"info","message":"Order 1042 shipped to { city: \"Budapest\", zip: \"1011\" } in 83.2 ms","template":"Order {OrderId} shipped to {@Address} in {Elapsed:0.0} ms","OrderId":1042,"Address":{"city":"Budapest","zip":"1011"},"Elapsed":83.2}
25
+ ```
26
+
27
+ The template is part of the event, so "every shipment slower than 500 ms" is a query, not a regex.
28
+
29
+ **Docs:** https://logit.unhingged.com/docs · **Roadmap:** https://logit.unhingged.com/roadmap
30
+
31
+ ## What you get
32
+
33
+ - **Message templates** ([messagetemplates.org](https://messagetemplates.org)) with `@` to destructure, `$` to stringify, `:format` and `,alignment`.
34
+ - **Capture rules that fit JS**: plain objects and arrays are data; class instances print their name unless you `@` them; errors are always structured, `cause` chains included. Depth, string and collection limits; key-based **redaction**; transforming policies.
35
+ - **Six levels** (`verbose` … `fatal`), per-source **overrides** (`'next': 'warn'`), a **`LevelSwitch`** you can flip at runtime, `LOGIT_LEVEL` from the environment.
36
+ - **Ambient context**: `LogContext.run({ requestId }, …)` on `AsyncLocalStorage`, `using _ = LogContext.push(…)`, enrichers for fixed, computed and process properties.
37
+ - **Sinks**: console (pretty / JSON / CLEF / output template), rolling file, batching HTTP with retry, Seq, OTLP/HTTP, an OpenTelemetry bridge, memory, callback — plus conditional, restricted, audit and map wrappers, and sub-loggers.
38
+ - **Formats**: Serilog output templates (`{Timestamp:HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}`), flat JSON with configurable keys, CLEF with the `@i` event-type hash.
39
+ - **Request logging** for Next.js route handlers, middleware and `instrumentation.ts`, and for Express — one completion event per request with a diagnostic context, like `UseSerilogRequestLogging`.
40
+ - **Timed operations**: `log.timed('Sync {Count} rows', () => …)` → `Sync 42 rows completed in 12.3 ms`.
41
+ - **Zero dependencies**, ESM + CJS, TypeScript types, isomorphic core (Node ≥ 20.16, Bun, Deno, the Next.js edge runtime, browsers).
42
+
43
+ ## Configure
44
+
45
+ ```ts
46
+ import { configure, consoleSink, seqSink, withEnvironment } from '@unhingged/logit';
47
+ import { fileSink, withMachineName, closeOnExit } from '@unhingged/logit/node';
48
+
49
+ const log = configure({
50
+ minimumLevel: 'debug',
51
+ overrides: { next: 'warn', 'app.db': 'verbose' },
52
+ enrichers: [withEnvironment(), withMachineName()],
53
+ destructuring: { redact: ['password', 'headers.authorization'] },
54
+ sinks: [
55
+ consoleSink(),
56
+ fileSink({ path: 'logs/app.log', rolling: 'day', retainedFileCount: 14 }),
57
+ seqSink({ serverUrl: 'http://localhost:5341' }),
58
+ ],
59
+ });
60
+
61
+ closeOnExit(log);
62
+ ```
63
+
64
+ Or, if you have Serilog in your fingers:
65
+
66
+ ```ts
67
+ import { LoggerConfiguration } from '@unhingged/logit';
68
+
69
+ const log = new LoggerConfiguration()
70
+ .minimumLevel.debug()
71
+ .minimumLevel.override('next', 'warn')
72
+ .enrich.withProperty('Application', 'shop')
73
+ .writeTo.console()
74
+ .createLogger();
75
+ ```
76
+
77
+ ## Next.js
78
+
79
+ ```ts
80
+ // instrumentation.ts
81
+ import { createOnRequestError } from '@unhingged/logit/next';
82
+
83
+ export async function register() {
84
+ // Literal check: Next inlines NEXT_RUNTIME at build time and drops this
85
+ // branch — and its Node-only import — from the edge bundle.
86
+ if (process.env.NEXT_RUNTIME === 'nodejs') {
87
+ const { configure, consoleSink } = await import('@unhingged/logit/node');
88
+ configure({ sinks: [consoleSink()] });
89
+ }
90
+ }
91
+
92
+ export const onRequestError = createOnRequestError();
93
+ ```
94
+
95
+ ```ts
96
+ // app/api/orders/route.ts
97
+ import { log, withLogging } from '@unhingged/logit/next';
98
+
99
+ export const GET = withLogging(async () => {
100
+ log.info('Listing orders');
101
+ return Response.json([]);
102
+ });
103
+ // → HTTP GET /api/orders responded 200 in 1.8321 ms
104
+ ```
105
+
106
+ ## Entry points
107
+
108
+ | Import | For |
109
+ |---|---|
110
+ | `@unhingged/logit` | The logger, templates, context, enrichers, filters, formatters, console / stream / memory / HTTP / Seq sinks. Isomorphic. |
111
+ | `@unhingged/logit/node` | Rolling `fileSink`, `withMachineName`, `withProcessId`, `interceptConsole`, `closeOnExit`. |
112
+ | `@unhingged/logit/next` | `withLogging`, `createOnRequestError`, `requestDiagnostics`. Edge-safe. |
113
+ | `@unhingged/logit/express` | `requestLogger`, `errorLogger`. |
114
+ | `@unhingged/logit/otel` | `otlpHttpSink`, `otelBridgeSink`, `encodeOtlpLogs`. |
115
+
116
+ Every entry re-exports the root, so one import is enough.
117
+
118
+ ## Performance
119
+
120
+ `scripts/bench.mjs` in the repository, Node 20, JSON to a null stream, 200 000 events each (2026-10-08, Apple Silicon laptop):
121
+
122
+ | | logit | pino | winston |
123
+ |---|---|---|---|
124
+ | `info('Order {OrderId} shipped', id)` | 1.48 M events/s | 1.91 M | 0.78 M |
125
+ | …with a nested object and `{Elapsed:0.0}` | 0.59 M events/s | 1.11 M | 0.54 M |
126
+ | Capture only (null sink) | 3.2 M events/s | | |
127
+ | Level disabled | 33 M calls/s | | |
128
+
129
+ pino is faster because it does less per event — no template rendering, no capture rules. logit's capture is cheap; the formatting path is where the difference lives, and in practice both are far ahead of what any file, socket or HTTP sink can absorb.
130
+
131
+ ## License
132
+
133
+ MIT — an [unhingged.com](https://unhingged.com) project.