logosaur 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 0c71
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,140 @@
1
- # Temporary Holding Version
1
+ # logosaur 🦖
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
+ [![npm](https://img.shields.io/npm/v/logosaur?color=af5fd7)](https://www.npmjs.com/package/logosaur)
4
+ [![CI](https://github.com/0c71/logosaur/actions/workflows/ci.yml/badge.svg)](https://github.com/0c71/logosaur/actions/workflows/ci.yml)
5
+ ![License: MIT](https://img.shields.io/badge/license-MIT-5fffd7)
6
+ ![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
+ A tiny, colorful, structured logger for Bun and TypeScript, with
10
+ **zero dependencies**. Short Go-style levels, `key=value` fields, and nothing
11
+ else dragged into your `node_modules`. Rawr.
12
+
13
+ ![logosaur output in a terminal](assets/demo.svg)
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ bun add logosaur
19
+ ```
20
+
21
+ It works in Node too: `npm install logosaur`.
22
+
23
+ That's the whole install. logosaur has **zero dependencies**, so it's one small
24
+ package (about 5 kB) and nothing else: no tree of transitive installs, nothing
25
+ to audit but the one file. 🦴
26
+
27
+ ## Usage
28
+
29
+ ```ts
30
+ import { log } from "logosaur";
31
+
32
+ log.info("🦖 hatched", { name: "rex", mood: "very hungry" });
33
+ log.warn("meteor spotted", { distance: "very close" });
34
+ log.error("lunch escaped", { species: "raptor", speed: "fast" });
35
+ ```
36
+
37
+ ```
38
+ 14:02:03 INF 🦖 hatched name=rex mood="very hungry"
39
+ 14:02:05 WRN meteor spotted distance="very close"
40
+ 14:02:06 ERR lunch escaped species=raptor speed=fast
41
+ ```
42
+
43
+ ## Levels
44
+
45
+ Three letters each, like Go's loggers.
46
+
47
+ | Method | Label | Notes |
48
+ | ----------- | ----- | ------------------------------------------- |
49
+ | `log.debug` | `DBG` | hidden unless the level is `debug` |
50
+ | `log.info` | `INF` | the default level |
51
+ | `log.warn` | `WRN` | |
52
+ | `log.error` | `ERR` | |
53
+ | `log.fatal` | `FTL` | logs, then goes extinct (exits with code 1) 🦴 |
54
+
55
+ ```ts
56
+ log.setLevel("debug");
57
+ ```
58
+
59
+ ## Fields
60
+
61
+ Nested objects are flattened, empty values are skipped, and strings with
62
+ spaces get quotes.
63
+
64
+ ```ts
65
+ log.info("dinner served", {
66
+ pen: { id: 7, fence: "electric" },
67
+ guests: ["rex", "blue"],
68
+ note: null,
69
+ });
70
+ ```
71
+
72
+ ```
73
+ 14:02:03 INF dinner served pen.id=7 pen.fence=electric guests=[rex blue]
74
+ ```
75
+
76
+ Errors log as their message, and dates as ISO strings.
77
+
78
+ ## Hatchlings
79
+
80
+ Sub-loggers that carry fields or a prefix on every line:
81
+
82
+ ```ts
83
+ const pen = log.with({ pen: 7 });
84
+ pen.warn("fence flickering");
85
+ // 14:02:03 WRN fence flickering pen=7
86
+
87
+ const cave = log.withPrefix("cave");
88
+ cave.info("echo");
89
+ // 14:02:03 INF cave: echo
90
+ ```
91
+
92
+ ## Options
93
+
94
+ ```ts
95
+ import { createLogger } from "logosaur";
96
+
97
+ const log = createLogger({
98
+ level: "debug",
99
+ prefix: "park",
100
+ timeZone: "America/New_York",
101
+ file: (day) => `logs/${day}.log`,
102
+ });
103
+ ```
104
+
105
+ | Option | Default | What it does |
106
+ | ---------- | ---------------- | ------------------------------------------------------- |
107
+ | `level` | `"info"` | The quietest level that gets through |
108
+ | `prefix` | | Shown before every message, as `prefix:` |
109
+ | `fields` | `{}` | Fields added to every line |
110
+ | `time` | `true` | Show the time on text lines |
111
+ | `timeZone` | local | Any IANA time zone, like `"Europe/London"` |
112
+ | `format` | `"text"` | `"text"`, `"json"` or `"logfmt"` |
113
+ | `color` | auto | Force colors on or off |
114
+ | `output` | `process.stderr` | Anything with a `write(text)` method |
115
+ | `file` | | Also append plain lines to a file, or one file per day |
116
+ | `replacer` | | `(key, value) => value`, to reshape fields at any depth |
117
+
118
+ ## Formats
119
+
120
+ ```ts
121
+ createLogger({ format: "json" }).info("hatched", { egg: { size: "large" } });
122
+ // {"time":"2026-11-17T19:02:03.000Z","level":"info","msg":"hatched","egg":{"size":"large"}}
123
+
124
+ createLogger({ format: "logfmt" }).info("hatched", { name: "rex" });
125
+ // time=2026-11-17T19:02:03.000Z level=info msg=hatched name=rex
126
+ ```
127
+
128
+ ## Colors
129
+
130
+ Colors turn on by themselves in a terminal. `NO_COLOR` turns them off,
131
+ `FORCE_COLOR` turns them on, or pass `color: true` or `false`. Files never get
132
+ colors.
133
+
134
+ ## License
135
+
136
+ [MIT](LICENSE)
137
+
138
+ ---
139
+
140
+ Made with 🦴 and a very small brain. Rawr.
@@ -0,0 +1,64 @@
1
+ type Format = "text" | "json" | "logfmt";
2
+ interface Writer {
3
+ write(chunk: string): unknown;
4
+ isTTY?: boolean;
5
+ }
6
+ type Replacer = (key: string, value: unknown) => unknown;
7
+ interface Options {
8
+ level?: Level;
9
+ prefix?: string;
10
+ fields?: object;
11
+ time?: boolean;
12
+ timeZone?: string;
13
+ format?: Format;
14
+ color?: boolean;
15
+ output?: Writer;
16
+ file?: string | ((day: string) => string);
17
+ replacer?: Replacer;
18
+ now?: () => Date;
19
+ }
20
+ type Log = (msg: string, fields?: object) => void;
21
+ interface Logger {
22
+ debug: Log;
23
+ info: Log;
24
+ warn: Log;
25
+ error: Log;
26
+ fatal: (msg: string, fields?: object) => never;
27
+ with: (fields: object) => Logger;
28
+ withPrefix: (prefix: string) => Logger;
29
+ setLevel: (level: Level) => void;
30
+ readonly level: Level;
31
+ }
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;
60
+ declare const formatValue: (value: unknown) => string;
61
+ declare const pairs: (fields: object, replacer?: Replacer) => [string, unknown][];
62
+ declare const createLogger: (options?: Options) => Logger;
63
+ declare const log: Logger;
64
+ export { createLogger, log, formatValue, pairs, type Format, type Level, type Logger, type Options, type Replacer, type Writer, };
package/dist/index.js ADDED
@@ -0,0 +1,191 @@
1
+ // src/index.ts
2
+ import { appendFileSync, mkdirSync } from "node:fs";
3
+ import { dirname } from "node:path";
4
+ var levels = {
5
+ debug: { rank: 0, label: "DBG", color: 63 },
6
+ info: { rank: 1, label: "INF", color: 86 },
7
+ warn: { rank: 2, label: "WRN", color: 192 },
8
+ error: { rank: 3, label: "ERR", color: 204 },
9
+ fatal: { rank: 4, label: "FTL", color: 134 }
10
+ };
11
+ var bold = "\x1B[1m";
12
+ var faint = "\x1B[2m";
13
+ var reset = "\x1B[0m";
14
+ var fg = (code) => `\x1B[38;5;${code}m`;
15
+ var paint = (on, text, ...styles) => on ? `${styles.join("")}${text}${reset}` : text;
16
+ var supportsColor = (output) => {
17
+ const { NO_COLOR, FORCE_COLOR } = process.env;
18
+ if (NO_COLOR)
19
+ return false;
20
+ if (FORCE_COLOR)
21
+ return FORCE_COLOR !== "0" && FORCE_COLOR !== "false";
22
+ return output.isTTY === true;
23
+ };
24
+ var normalize = (value) => {
25
+ if (value instanceof Error)
26
+ return value.message;
27
+ if (value instanceof Date)
28
+ return value.toISOString();
29
+ if (typeof value === "bigint")
30
+ return value.toString();
31
+ return value;
32
+ };
33
+ var shaper = (replacer) => (key, value) => normalize(replacer ? replacer(key, value) : value);
34
+ var formatValue = (value) => {
35
+ if (value === null || value === undefined)
36
+ return "<nil>";
37
+ if (Array.isArray(value))
38
+ return `[${value.map(formatValue).join(" ")}]`;
39
+ if (typeof value === "string") {
40
+ return value === "" || /[\s="]/.test(value) ? JSON.stringify(value) : value;
41
+ }
42
+ if (typeof value === "object") {
43
+ try {
44
+ return JSON.stringify(value);
45
+ } catch {
46
+ return "[unserializable]";
47
+ }
48
+ }
49
+ return String(value);
50
+ };
51
+ var pairs = (fields, replacer) => {
52
+ const shape = shaper(replacer);
53
+ const out = [];
54
+ const parents = new Set;
55
+ const walk = (node, path) => {
56
+ parents.add(node);
57
+ for (const [key, raw] of Object.entries(node)) {
58
+ const value = shape(key, raw);
59
+ const name = `${path}${key}`;
60
+ if (value == null)
61
+ continue;
62
+ if (Array.isArray(value)) {
63
+ out.push([name, value.filter((v) => v != null).map(normalize)]);
64
+ } else if (typeof value !== "object") {
65
+ out.push([name, value]);
66
+ } else if (parents.has(value)) {
67
+ out.push([name, "[circular]"]);
68
+ } else {
69
+ walk(value, `${name}.`);
70
+ }
71
+ }
72
+ parents.delete(node);
73
+ };
74
+ walk(fields, "");
75
+ return out;
76
+ };
77
+ var makeClock = (timeZone) => {
78
+ const format = new Intl.DateTimeFormat("en-US", {
79
+ timeZone,
80
+ hourCycle: "h23",
81
+ year: "numeric",
82
+ month: "2-digit",
83
+ day: "2-digit",
84
+ hour: "2-digit",
85
+ minute: "2-digit",
86
+ second: "2-digit"
87
+ });
88
+ return (date) => {
89
+ const p = Object.fromEntries(format.formatToParts(date).map(({ type, value }) => [type, value]));
90
+ return {
91
+ day: `${p.year}-${p.month}-${p.day}`,
92
+ time: `${p.hour}:${p.minute}:${p.second}`
93
+ };
94
+ };
95
+ };
96
+ var createLogger = (options = {}) => {
97
+ const {
98
+ prefix,
99
+ fields = {},
100
+ time = true,
101
+ timeZone,
102
+ format = "text",
103
+ output = process.stderr,
104
+ file,
105
+ replacer,
106
+ now = () => new Date
107
+ } = options;
108
+ let threshold = options.level ?? "info";
109
+ const color = options.color ?? supportsColor(output);
110
+ const clock = makeClock(timeZone);
111
+ const shape = shaper(replacer);
112
+ const madeDirs = new Set;
113
+ const toData = (merged) => {
114
+ try {
115
+ return JSON.parse(JSON.stringify(merged, (key, value) => key === "" ? value : shape(key, value)));
116
+ } catch {
117
+ return Object.fromEntries(pairs(merged, replacer));
118
+ }
119
+ };
120
+ const render = (level, msg, extra, date, colored) => {
121
+ const merged = { ...fields, ...extra };
122
+ const meta = {
123
+ time: date.toISOString(),
124
+ level,
125
+ ...prefix ? { prefix } : {},
126
+ msg
127
+ };
128
+ if (format === "json") {
129
+ return JSON.stringify({ ...meta, ...toData(merged) });
130
+ }
131
+ const context = pairs(merged, replacer);
132
+ if (format === "logfmt") {
133
+ return [...Object.entries(meta), ...context].map(([key, value]) => `${key}=${formatValue(value)}`).join(" ");
134
+ }
135
+ const { label, color: code } = levels[level];
136
+ return [
137
+ time && clock(date).time,
138
+ paint(colored, label, bold, fg(code)),
139
+ prefix && paint(colored, `${prefix}:`, bold),
140
+ msg,
141
+ ...context.map(([key, value]) => `${paint(colored, key, faint)}=${formatValue(value)}`)
142
+ ].filter(Boolean).join(" ");
143
+ };
144
+ const append = (path, line) => {
145
+ const dir = dirname(path);
146
+ if (!madeDirs.has(dir)) {
147
+ mkdirSync(dir, { recursive: true });
148
+ madeDirs.add(dir);
149
+ }
150
+ appendFileSync(path, `${line}
151
+ `);
152
+ };
153
+ const write = (level, msg, extra) => {
154
+ if (levels[level].rank < levels[threshold].rank)
155
+ return;
156
+ const date = now();
157
+ output.write(`${render(level, msg, extra, date, color)}
158
+ `);
159
+ if (file) {
160
+ const path = typeof file === "function" ? file(clock(date).day) : file;
161
+ append(path, render(level, msg, extra, date, false));
162
+ }
163
+ };
164
+ const at = (level) => (msg, extra) => write(level, msg, extra);
165
+ const child = (overrides) => createLogger({ ...options, level: threshold, ...overrides });
166
+ return {
167
+ debug: at("debug"),
168
+ info: at("info"),
169
+ warn: at("warn"),
170
+ error: at("error"),
171
+ fatal: (msg, extra) => {
172
+ write("fatal", msg, extra);
173
+ return process.exit(1);
174
+ },
175
+ with: (more) => child({ fields: { ...fields, ...more } }),
176
+ withPrefix: (next) => child({ prefix: next }),
177
+ setLevel: (level) => {
178
+ threshold = level;
179
+ },
180
+ get level() {
181
+ return threshold;
182
+ }
183
+ };
184
+ };
185
+ var log = createLogger();
186
+ export {
187
+ createLogger,
188
+ formatValue,
189
+ log,
190
+ pairs
191
+ };
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "logosaur",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A tiny, colorful, zero-dependency structured logger for Bun and TypeScript. Rawr.",
5
+ "keywords": [
6
+ "log",
7
+ "logger",
8
+ "logging",
9
+ "structured",
10
+ "logfmt",
11
+ "bun",
12
+ "typescript",
13
+ "color"
14
+ ],
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/0c71/logosaur.git"
19
+ },
20
+ "homepage": "https://github.com/0c71/logosaur#readme",
21
+ "bugs": "https://github.com/0c71/logosaur/issues",
22
+ "type": "module",
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ }
30
+ },
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "sideEffects": false,
35
+ "scripts": {
36
+ "build": "bun build src/index.ts --outdir dist --target node && tsc -p tsconfig.build.json",
37
+ "test": "bun test",
38
+ "typecheck": "tsc --noEmit",
39
+ "prepublishOnly": "bun run typecheck && bun test && bun run build"
40
+ },
41
+ "devDependencies": {
42
+ "@types/bun": "^1.4.4",
43
+ "typescript": "^7.0.2"
44
+ }
45
+ }