logosaur 0.2.1 → 0.2.2
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 +150 -97
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,62 +1,51 @@
|
|
|
1
1
|
# logosaur 🦖
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/logosaur)
|
|
4
|
+
[](https://jsr.io/@0c71/logosaur)
|
|
5
|
+
[](https://jsr.io/@0c71/logosaur/score)
|
|
4
6
|
[](https://github.com/0c71/logosaur/actions/workflows/ci.yml)
|
|
5
7
|

|
|
6
8
|

|
|
7
|
-

|
|
8
9
|
|
|
9
|
-
A tiny, colorful, structured logger for Bun and
|
|
10
|
+
A tiny, colorful, structured logger for Bun, Node and Deno, with
|
|
10
11
|
**zero dependencies**. Short Go-style levels, `key=value` fields, and nothing
|
|
11
12
|
else dragged into your `node_modules`. Rawr.
|
|
12
13
|
|
|
13
|
-

|
|
14
|
-
|
|
15
|
-
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
- **Zero dependencies**: one package of about 9 kB, nothing to audit but itself
|
|
17
|
+
- **Fast**: the quickest import of the popular loggers, text three times as
|
|
18
|
+
fast as tslog, JSON as fast as pino on Bun ([how it compares](#how-it-compares))
|
|
19
|
+
- **Readable**: colored `INF`/`WRN`/`ERR` lines with `key=value` fields, or
|
|
20
|
+
JSON and logfmt for machines
|
|
21
|
+
- **Request logging** for Hono and Elysia, one line per request
|
|
22
|
+
|
|
23
|
+
## Contents
|
|
24
|
+
|
|
25
|
+
- [Quick start](#quick-start)
|
|
26
|
+
- [Guide](#guide)
|
|
27
|
+
- [Levels](#levels)
|
|
28
|
+
- [Fields](#fields)
|
|
29
|
+
- [Hatchlings: sub-loggers](#hatchlings-sub-loggers)
|
|
30
|
+
- [Your own logger](#your-own-logger)
|
|
31
|
+
- [Formats](#formats)
|
|
32
|
+
- [Colors](#colors)
|
|
33
|
+
- [Log files](#log-files)
|
|
34
|
+
- [Reshaping fields](#reshaping-fields)
|
|
35
|
+
- [Request logging](#request-logging)
|
|
36
|
+
- [How it compares](#how-it-compares)
|
|
37
|
+
- [License](#license)
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
16
40
|
|
|
17
41
|
```bash
|
|
18
|
-
bun add logosaur
|
|
42
|
+
bun add logosaur # Bun
|
|
43
|
+
npm install logosaur # Node
|
|
44
|
+
deno add jsr:@0c71/logosaur # Deno
|
|
19
45
|
```
|
|
20
46
|
|
|
21
|
-
It works in Node and Deno too:
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
npm install logosaur
|
|
25
|
-
deno add jsr:@0c71/logosaur
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
That's the whole install. logosaur has **zero dependencies**, so it's one small
|
|
29
|
-
package (about 8 kB) and nothing else: no tree of transitive installs, nothing
|
|
30
|
-
to audit but the one file. 🦴
|
|
31
|
-
|
|
32
|
-
## How it compares
|
|
33
|
-
|
|
34
|
-
| | logosaur | pino | winston | tslog | consola |
|
|
35
|
-
| --------------------- | --------- | -------- | -------- | -------- | -------- |
|
|
36
|
-
| Packages installed | **1** | 13 | 28 | 1 | 1 |
|
|
37
|
-
| Install size | **19 kB** | 1.2 MB | 1.9 MB | 980 kB | 310 kB |
|
|
38
|
-
| Import, Node / Bun | **4 / 6 ms** | 18 / 18 ms | 31 / 29 ms | 20 / 12 ms | 14 / 10 ms |
|
|
39
|
-
| JSON lines/s, Node / Bun | 1.1M / **2.6M** | **2.7M / 2.6M** | 290k / 530k | 550k / 700k | |
|
|
40
|
-
| Text lines/s, Node / Bun | **830k / 1.2M** | | | 280k / 320k | 100k / 120k |
|
|
41
|
-
|
|
42
|
-
logosaur is the smallest install by far and the quickest to import. It writes
|
|
43
|
-
text lines three times as fast as tslog and eight to ten times as fast as
|
|
44
|
-
consola. On Bun it writes JSON as fast as pino; on Node pino is about two and a
|
|
45
|
-
half times faster, so if raw JSON throughput on Node is what you need, pick pino.
|
|
46
|
-
|
|
47
|
-
Each line has a message and four fields, written to a sink that throws it away.
|
|
48
|
-
Every logger runs in its own process, on Node 24 and Bun 1.4, on one Windows
|
|
49
|
-
desktop. tslog's code-position lookup is off, so it's formatting against
|
|
50
|
-
formatting. Run it yourself:
|
|
51
|
-
|
|
52
|
-
```bash
|
|
53
|
-
cd bench && bun install && bun run size && bun run import && bun run speed
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Usage
|
|
57
|
-
|
|
58
47
|
```ts
|
|
59
|
-
import { log } from "logosaur";
|
|
48
|
+
import { log } from "logosaur"; // in Deno: from "@0c71/logosaur"
|
|
60
49
|
|
|
61
50
|
log.info("🦖 hatched", { name: "rex", mood: "very hungry" });
|
|
62
51
|
log.warn("meteor spotted", { distance: "very close" });
|
|
@@ -69,44 +58,50 @@ log.error("lunch escaped", { species: "raptor", speed: "fast" });
|
|
|
69
58
|
14:02:06 ERR lunch escaped species=raptor speed=fast
|
|
70
59
|
```
|
|
71
60
|
|
|
72
|
-
|
|
61
|
+
`log` is ready to use: info and up, to stderr, in your local time. For anything
|
|
62
|
+
else, make [your own logger](#your-own-logger). In Deno, run with `--allow-env`
|
|
63
|
+
so logosaur can read `NO_COLOR` and `FORCE_COLOR`.
|
|
64
|
+
|
|
65
|
+
## Guide
|
|
66
|
+
|
|
67
|
+
### Levels
|
|
73
68
|
|
|
74
69
|
Three letters each, like Go's loggers.
|
|
75
70
|
|
|
76
|
-
| Method | Label | Notes
|
|
77
|
-
| ----------- | ----- |
|
|
78
|
-
| `log.debug` | `DBG` | hidden unless the level is `debug`
|
|
79
|
-
| `log.info` | `INF` | the default level
|
|
80
|
-
| `log.warn` | `WRN` |
|
|
81
|
-
| `log.error` | `ERR` |
|
|
71
|
+
| Method | Label | Notes |
|
|
72
|
+
| ----------- | ----- | ---------------------------------------------- |
|
|
73
|
+
| `log.debug` | `DBG` | hidden unless the level is `debug` |
|
|
74
|
+
| `log.info` | `INF` | the default level |
|
|
75
|
+
| `log.warn` | `WRN` | |
|
|
76
|
+
| `log.error` | `ERR` | |
|
|
82
77
|
| `log.fatal` | `FTL` | logs, then goes extinct (exits with code 1) 🦴 |
|
|
83
78
|
|
|
84
79
|
```ts
|
|
85
80
|
log.setLevel("debug");
|
|
86
81
|
```
|
|
87
82
|
|
|
88
|
-
|
|
83
|
+
### Fields
|
|
89
84
|
|
|
90
|
-
|
|
91
|
-
spaces get quotes.
|
|
85
|
+
Every method takes a message and an object of fields. Nested objects are
|
|
86
|
+
flattened, empty values are skipped, and strings with spaces get quotes. Errors
|
|
87
|
+
log as their message, and dates as ISO strings.
|
|
92
88
|
|
|
93
89
|
```ts
|
|
94
90
|
log.info("dinner served", {
|
|
95
91
|
pen: { id: 7, fence: "electric" },
|
|
96
92
|
guests: ["rex", "blue"],
|
|
97
93
|
note: null,
|
|
94
|
+
err: new Error("fence down"),
|
|
98
95
|
});
|
|
99
96
|
```
|
|
100
97
|
|
|
101
98
|
```
|
|
102
|
-
14:02:03 INF dinner served pen.id=7 pen.fence=electric guests=[rex blue]
|
|
99
|
+
14:02:03 INF dinner served pen.id=7 pen.fence=electric guests=[rex blue] err="fence down"
|
|
103
100
|
```
|
|
104
101
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
## Hatchlings
|
|
102
|
+
### Hatchlings: sub-loggers
|
|
108
103
|
|
|
109
|
-
Sub-loggers
|
|
104
|
+
Sub-loggers carry fields or a prefix onto every line:
|
|
110
105
|
|
|
111
106
|
```ts
|
|
112
107
|
const pen = log.with({ pen: 7 });
|
|
@@ -118,6 +113,81 @@ cave.info("echo");
|
|
|
118
113
|
// 14:02:03 INF cave: echo
|
|
119
114
|
```
|
|
120
115
|
|
|
116
|
+
### Your own logger
|
|
117
|
+
|
|
118
|
+
`createLogger` makes a logger with its own settings:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { createLogger } from "logosaur";
|
|
122
|
+
|
|
123
|
+
const log = createLogger({
|
|
124
|
+
level: "debug",
|
|
125
|
+
prefix: "park",
|
|
126
|
+
fields: { island: "nublar" },
|
|
127
|
+
timeZone: "America/New_York",
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
log.info("gates open");
|
|
131
|
+
// 14:02:03 INF park: gates open island=nublar
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
| Option | Default | What it does |
|
|
135
|
+
| ---------- | ---------------- | ---------------------------------------------------------------- |
|
|
136
|
+
| `level` | `"info"` | The quietest level that gets through |
|
|
137
|
+
| `prefix` | | Shown before every message, as `prefix:` |
|
|
138
|
+
| `fields` | `{}` | Fields added to every line |
|
|
139
|
+
| `time` | `true` | Show the time on text lines |
|
|
140
|
+
| `timeZone` | local | Any IANA time zone, like `"Europe/London"` |
|
|
141
|
+
| `format` | `"text"` | `"text"`, `"json"` or `"logfmt"` ([formats](#formats)) |
|
|
142
|
+
| `color` | auto | Force colors on or off ([colors](#colors)) |
|
|
143
|
+
| `output` | `process.stderr` | Anything with a `write(text)` method |
|
|
144
|
+
| `file` | | Also append plain lines to a file ([log files](#log-files)) |
|
|
145
|
+
| `replacer` | | `(key, value) => value` ([reshaping fields](#reshaping-fields)) |
|
|
146
|
+
|
|
147
|
+
### Formats
|
|
148
|
+
|
|
149
|
+
Text is for people; JSON and logfmt are for log collectors. Both always carry
|
|
150
|
+
the full time, in UTC.
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
createLogger({ format: "json" }).info("hatched", { egg: { size: "large" } });
|
|
154
|
+
// {"time":"2026-11-17T19:02:03.000Z","level":"info","msg":"hatched","egg":{"size":"large"}}
|
|
155
|
+
|
|
156
|
+
createLogger({ format: "logfmt" }).info("hatched", { name: "rex" });
|
|
157
|
+
// time=2026-11-17T19:02:03.000Z level=info msg=hatched name=rex
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Colors
|
|
161
|
+
|
|
162
|
+
Colors turn on by themselves in a terminal. `NO_COLOR` turns them off,
|
|
163
|
+
`FORCE_COLOR` turns them on, or pass `color: true` or `false`. Files never get
|
|
164
|
+
colors.
|
|
165
|
+
|
|
166
|
+
### Log files
|
|
167
|
+
|
|
168
|
+
`file` appends every line, without colors, to a file as well as the output. Give
|
|
169
|
+
it a function to start a new file each day; the day is a `YYYY-MM-DD` string in
|
|
170
|
+
the logger's time zone. Missing folders are made for you.
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
createLogger({ file: "logs/park.log" });
|
|
174
|
+
createLogger({ file: (day) => `logs/${day}.log` }); // logs/2026-11-17.log
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Reshaping fields
|
|
178
|
+
|
|
179
|
+
A `replacer` sees every field, at any depth, before it is logged. Return
|
|
180
|
+
something else to change it, like hiding a secret:
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
const log = createLogger({
|
|
184
|
+
replacer: (key, value) => (key === "password" ? "***" : value),
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
log.info("keeper signed in", { user: "muldoon", password: "clever-girl" });
|
|
188
|
+
// 14:02:03 INF keeper signed in user=muldoon password=***
|
|
189
|
+
```
|
|
190
|
+
|
|
121
191
|
## Request logging
|
|
122
192
|
|
|
123
193
|
One line per request, for [Hono](https://hono.dev) and
|
|
@@ -143,53 +213,36 @@ const app = new Elysia().use(logosaur());
|
|
|
143
213
|
```
|
|
144
214
|
|
|
145
215
|
Pass your own logger to set its level, format or fields:
|
|
146
|
-
`logosaur(log.with({ service: "park" }))`.
|
|
216
|
+
`logosaur(log.with({ service: "park" }))`. Hono and Elysia are optional:
|
|
217
|
+
installing logosaur never pulls them in.
|
|
147
218
|
|
|
148
|
-
##
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
import { createLogger } from "logosaur";
|
|
152
|
-
|
|
153
|
-
const log = createLogger({
|
|
154
|
-
level: "debug",
|
|
155
|
-
prefix: "park",
|
|
156
|
-
timeZone: "America/New_York",
|
|
157
|
-
file: (day) => `logs/${day}.log`,
|
|
158
|
-
});
|
|
159
|
-
```
|
|
219
|
+
## How it compares
|
|
160
220
|
|
|
161
|
-
|
|
|
162
|
-
|
|
|
163
|
-
|
|
|
164
|
-
|
|
|
165
|
-
|
|
|
166
|
-
|
|
|
167
|
-
|
|
|
168
|
-
| `format` | `"text"` | `"text"`, `"json"` or `"logfmt"` |
|
|
169
|
-
| `color` | auto | Force colors on or off |
|
|
170
|
-
| `output` | `process.stderr` | Anything with a `write(text)` method |
|
|
171
|
-
| `file` | | Also append plain lines to a file, or one file per day |
|
|
172
|
-
| `replacer` | | `(key, value) => value`, to reshape fields at any depth |
|
|
221
|
+
| | logosaur | pino | winston | tslog | consola |
|
|
222
|
+
| ------------------------ | --------------- | --------------- | ----------- | ----------- | ----------- |
|
|
223
|
+
| Packages installed | **1** | 13 | 28 | 1 | 1 |
|
|
224
|
+
| Install size | **22 kB** | 1.2 MB | 1.9 MB | 980 kB | 310 kB |
|
|
225
|
+
| Import, Node / Bun | **4 / 6 ms** | 18 / 18 ms | 31 / 29 ms | 20 / 12 ms | 14 / 10 ms |
|
|
226
|
+
| JSON lines/s, Node / Bun | 1.1M / **2.6M** | **2.7M / 2.6M** | 290k / 530k | 550k / 700k | |
|
|
227
|
+
| Text lines/s, Node / Bun | **830k / 1.2M** | | | 280k / 320k | 100k / 120k |
|
|
173
228
|
|
|
174
|
-
|
|
229
|
+
logosaur is the smallest install by far and the quickest to import. It writes
|
|
230
|
+
text lines three times as fast as tslog and eight to ten times as fast as
|
|
231
|
+
consola. On Bun it writes JSON as fast as pino; on Node pino is about two and a
|
|
232
|
+
half times faster, so if raw JSON throughput on Node is what you need, pick pino.
|
|
175
233
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
234
|
+
Each line has a message and four fields, written to a sink that throws it away.
|
|
235
|
+
Every logger runs in its own process, on Node 24 and Bun 1.4, on one Windows
|
|
236
|
+
desktop. tslog's code-position lookup is off, so it's formatting against
|
|
237
|
+
formatting. Run it yourself:
|
|
179
238
|
|
|
180
|
-
|
|
181
|
-
|
|
239
|
+
```bash
|
|
240
|
+
cd bench && bun install && bun run size && bun run import && bun run speed
|
|
182
241
|
```
|
|
183
242
|
|
|
184
|
-
## Colors
|
|
185
|
-
|
|
186
|
-
Colors turn on by themselves in a terminal. `NO_COLOR` turns them off,
|
|
187
|
-
`FORCE_COLOR` turns them on, or pass `color: true` or `false`. Files never get
|
|
188
|
-
colors.
|
|
189
|
-
|
|
190
243
|
## License
|
|
191
244
|
|
|
192
|
-
[MIT](LICENSE)
|
|
245
|
+
[MIT](https://github.com/0c71/logosaur/blob/main/LICENSE)
|
|
193
246
|
|
|
194
247
|
---
|
|
195
248
|
|