logosaur 0.2.0 → 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 CHANGED
@@ -1,62 +1,51 @@
1
1
  # logosaur 🦖
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/logosaur?color=af5fd7)](https://www.npmjs.com/package/logosaur)
4
+ [![JSR](https://jsr.io/badges/@0c71/logosaur)](https://jsr.io/@0c71/logosaur)
5
+ [![JSR score](https://jsr.io/badges/@0c71/logosaur/score)](https://jsr.io/@0c71/logosaur/score)
4
6
  [![CI](https://github.com/0c71/logosaur/actions/workflows/ci.yml/badge.svg)](https://github.com/0c71/logosaur/actions/workflows/ci.yml)
5
7
  ![License: MIT](https://img.shields.io/badge/license-MIT-5fffd7)
6
8
  ![Dependencies: zero](https://img.shields.io/badge/dependencies-zero-d7ff87)
7
- ![Built for Bun](https://img.shields.io/badge/built%20for-bun-ff5f87)
8
9
 
9
- A tiny, colorful, structured logger for Bun and TypeScript, with
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
- ![logosaur output in a terminal](assets/demo.svg)
14
-
15
- ## Install
14
+ ![logosaur output in a terminal](https://raw.githubusercontent.com/0c71/logosaur/main/assets/demo.svg)
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
- ## Levels
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
- ## Fields
83
+ ### Fields
89
84
 
90
- Nested objects are flattened, empty values are skipped, and strings with
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
- Errors log as their message, and dates as ISO strings.
106
-
107
- ## Hatchlings
102
+ ### Hatchlings: sub-loggers
108
103
 
109
- Sub-loggers that carry fields or a prefix on every line:
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
- ## Options
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
- | Option | Default | What it does |
162
- | ---------- | ---------------- | ------------------------------------------------------- |
163
- | `level` | `"info"` | The quietest level that gets through |
164
- | `prefix` | | Shown before every message, as `prefix:` |
165
- | `fields` | `{}` | Fields added to every line |
166
- | `time` | `true` | Show the time on text lines |
167
- | `timeZone` | local | Any IANA time zone, like `"Europe/London"` |
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
- ## Formats
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
- ```ts
177
- createLogger({ format: "json" }).info("hatched", { egg: { size: "large" } });
178
- // {"time":"2026-11-17T19:02:03.000Z","level":"info","msg":"hatched","egg":{"size":"large"}}
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
- createLogger({ format: "logfmt" }).info("hatched", { name: "rex" });
181
- // time=2026-11-17T19:02:03.000Z level=info msg=hatched name=rex
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
 
package/dist/elysia.d.ts CHANGED
@@ -1,4 +1,24 @@
1
+ /**
2
+ * Request logging for Elysia: one line per request, with its method, path,
3
+ * status and time taken; info, warn for a 4xx, error for a 5xx.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { Elysia } from "elysia";
8
+ * import { logosaur } from "@0c71/logosaur/elysia";
9
+ *
10
+ * const app = new Elysia().use(logosaur());
11
+ * // 14:02:03 INF request method=GET path=/eggs status=200 ms=1.2
12
+ * ```
13
+ *
14
+ * @module
15
+ */
1
16
  import { Elysia } from "elysia";
2
17
  import { type Logger } from "./index.ts";
18
+ /**
19
+ * Elysia plugin: one line per request, with its method, path, status and
20
+ * time taken (`app.use(logosaur())`); pass a logger to set its level, format
21
+ * or fields, else it uses the ready-made `log`
22
+ */
3
23
  declare const logosaur: (log?: Logger) => Elysia;
4
24
  export { logosaur };
package/dist/hono.d.ts CHANGED
@@ -1,4 +1,24 @@
1
+ /**
2
+ * Request logging for Hono: one line per request, with its method, path,
3
+ * status and time taken; info, warn for a 4xx, error for a 5xx.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { Hono } from "hono";
8
+ * import { logosaur } from "@0c71/logosaur/hono";
9
+ *
10
+ * const app = new Hono().use(logosaur());
11
+ * // 14:02:03 INF request method=GET path=/eggs status=200 ms=1.2
12
+ * ```
13
+ *
14
+ * @module
15
+ */
1
16
  import type { MiddlewareHandler } from "hono";
2
17
  import { type Logger } from "./index.ts";
18
+ /**
19
+ * Hono middleware: one line per request, with its method, path, status and
20
+ * time taken (`app.use(logosaur())`); pass a logger to set its level, format
21
+ * or fields, else it uses the ready-made `log`
22
+ */
3
23
  declare const logosaur: (log?: Logger) => MiddlewareHandler;
4
24
  export { logosaur };
package/dist/index.d.ts CHANGED
@@ -1,64 +1,83 @@
1
+ /** how each line is written: `"text"`, `"json"` or `"logfmt"` */
1
2
  type Format = "text" | "json" | "logfmt";
3
+ /** anything with a write method, like `process.stderr` */
2
4
  interface Writer {
5
+ /** takes one line, with its newline */
3
6
  write(chunk: string): unknown;
7
+ /** a terminal: colors turn on by themselves */
4
8
  isTTY?: boolean;
5
9
  }
10
+ /** reshapes a field before it is logged, at any depth */
6
11
  type Replacer = (key: string, value: unknown) => unknown;
12
+ /** a level, quietest first */
13
+ type Level = "debug" | "info" | "warn" | "error" | "fatal";
14
+ /** how a logger from {@link createLogger} writes its lines */
7
15
  interface Options {
16
+ /** the quietest level that gets through (default `"info"`) */
8
17
  level?: Level;
18
+ /** shown before every message, as `prefix:` */
9
19
  prefix?: string;
20
+ /** added to every line */
10
21
  fields?: object;
22
+ /** show the time on text lines (default `true`) */
11
23
  time?: boolean;
24
+ /** an IANA time zone for the clock, like `"America/New_York"` (default: local) */
12
25
  timeZone?: string;
26
+ /** `"text"` (default), `"json"` or `"logfmt"` */
13
27
  format?: Format;
28
+ /** force colors on or off (default: on for a terminal, off with `NO_COLOR`) */
14
29
  color?: boolean;
30
+ /** where lines go (default `process.stderr`) */
15
31
  output?: Writer;
32
+ /** also append every line, without colors, to this file or to each day's file */
16
33
  file?: string | ((day: string) => string);
34
+ /** reshapes fields before they are logged */
17
35
  replacer?: Replacer;
36
+ /** the clock, handy for tests */
18
37
  now?: () => Date;
19
38
  }
39
+ /** logs a message at one level, with optional fields */
20
40
  type Log = (msg: string, fields?: object) => void;
41
+ /** a logger: a method per level, each taking a message and optional fields */
21
42
  interface Logger {
43
+ /** `DBG`: hidden unless the level is `"debug"` */
22
44
  debug: Log;
45
+ /** `INF`: the default level */
23
46
  info: Log;
47
+ /** `WRN` */
24
48
  warn: Log;
49
+ /** `ERR` */
25
50
  error: Log;
51
+ /** `FTL`: logs, then exits with code 1 */
26
52
  fatal: (msg: string, fields?: object) => never;
53
+ /** a new logger that adds these fields to every line */
27
54
  with: (fields: object) => Logger;
55
+ /** a new logger with this prefix */
28
56
  withPrefix: (prefix: string) => Logger;
57
+ /** changes the quietest level that gets through */
29
58
  setLevel: (level: Level) => void;
59
+ /** the quietest level that gets through */
30
60
  readonly level: Level;
31
61
  }
32
- declare const levels: {
33
- readonly debug: {
34
- readonly rank: 0;
35
- readonly label: "DBG";
36
- readonly color: 63;
37
- };
38
- readonly info: {
39
- readonly rank: 1;
40
- readonly label: "INF";
41
- readonly color: 86;
42
- };
43
- readonly warn: {
44
- readonly rank: 2;
45
- readonly label: "WRN";
46
- readonly color: 192;
47
- };
48
- readonly error: {
49
- readonly rank: 3;
50
- readonly label: "ERR";
51
- readonly color: 204;
52
- };
53
- readonly fatal: {
54
- readonly rank: 4;
55
- readonly label: "FTL";
56
- readonly color: 134;
57
- };
58
- };
59
- type Level = keyof typeof levels;
62
+ /** a value as text: quoted when it is empty or has spaces, `=` or quotes */
60
63
  declare const formatValue: (value: unknown) => string;
64
+ /**
65
+ * the fields as `[key, value]` pairs: nested objects flattened (`a.b=1`) and
66
+ * empty values left out
67
+ */
61
68
  declare const pairs: (fields: object, replacer?: Replacer) => [string, unknown][];
69
+ /**
70
+ * a new logger
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const log = createLogger({ level: "debug", timeZone: "America/New_York" });
75
+ *
76
+ * log.info("🦖 hatched", { name: "rex" });
77
+ * // 14:02:03 INF 🦖 hatched name=rex
78
+ * ```
79
+ */
62
80
  declare const createLogger: (options?: Options) => Logger;
81
+ /** a ready-made logger: info and up, to stderr */
63
82
  declare const log: Logger;
64
- export { createLogger, log, formatValue, pairs, type Format, type Level, type Logger, type Options, type Replacer, type Writer, };
83
+ export { createLogger, log, formatValue, pairs, type Format, type Level, type Log, type Logger, type Options, type Replacer, type Writer, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logosaur",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "A tiny, colorful, zero-dependency structured logger for Bun and TypeScript. Rawr.",
5
5
  "keywords": [
6
6
  "log",