logosaur 0.1.0 → 0.1.1
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 +24 -0
- package/dist/index.js +45 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,30 @@ That's the whole install. logosaur has **zero dependencies**, so it's one small
|
|
|
24
24
|
package (about 5 kB) and nothing else: no tree of transitive installs, nothing
|
|
25
25
|
to audit but the one file. 🦴
|
|
26
26
|
|
|
27
|
+
## How it compares
|
|
28
|
+
|
|
29
|
+
| | logosaur | pino | winston | tslog | consola |
|
|
30
|
+
| --------------------- | --------- | -------- | -------- | -------- | -------- |
|
|
31
|
+
| Packages installed | **1** | 13 | 28 | 1 | 1 |
|
|
32
|
+
| Install size | **14 kB** | 1.2 MB | 1.9 MB | 980 kB | 310 kB |
|
|
33
|
+
| Import, Node / Bun | **4 / 6 ms** | 18 / 18 ms | 31 / 29 ms | 20 / 12 ms | 14 / 10 ms |
|
|
34
|
+
| JSON lines/s, Node / Bun | 1.1M / **2.6M** | **2.7M / 2.6M** | 290k / 530k | 550k / 700k | |
|
|
35
|
+
| Text lines/s, Node / Bun | **830k / 1.2M** | | | 280k / 320k | 100k / 120k |
|
|
36
|
+
|
|
37
|
+
logosaur is the smallest install by far and the quickest to import. It writes
|
|
38
|
+
text lines three times as fast as tslog and eight to ten times as fast as
|
|
39
|
+
consola. On Bun it writes JSON as fast as pino; on Node pino is about two and a
|
|
40
|
+
half times faster, so if raw JSON throughput on Node is what you need, pick pino.
|
|
41
|
+
|
|
42
|
+
Each line has a message and four fields, written to a sink that throws it away.
|
|
43
|
+
Every logger runs in its own process, on Node 24 and Bun 1.4, on one Windows
|
|
44
|
+
desktop. tslog's code-position lookup is off, so it's formatting against
|
|
45
|
+
formatting. Run it yourself:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
cd bench && bun install && bun run size && bun run import && bun run speed
|
|
49
|
+
```
|
|
50
|
+
|
|
27
51
|
## Usage
|
|
28
52
|
|
|
29
53
|
```ts
|
package/dist/index.js
CHANGED
|
@@ -48,6 +48,19 @@ var formatValue = (value) => {
|
|
|
48
48
|
}
|
|
49
49
|
return String(value);
|
|
50
50
|
};
|
|
51
|
+
var plain = (value, depth = 0) => {
|
|
52
|
+
if (typeof value === "bigint")
|
|
53
|
+
return false;
|
|
54
|
+
if (typeof value !== "object" || value === null)
|
|
55
|
+
return true;
|
|
56
|
+
if (value instanceof Error || depth > 8)
|
|
57
|
+
return false;
|
|
58
|
+
for (const item of Object.values(value)) {
|
|
59
|
+
if (!plain(item, depth + 1))
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
};
|
|
51
64
|
var pairs = (fields, replacer) => {
|
|
52
65
|
const shape = shaper(replacer);
|
|
53
66
|
const out = [];
|
|
@@ -75,22 +88,30 @@ var pairs = (fields, replacer) => {
|
|
|
75
88
|
return out;
|
|
76
89
|
};
|
|
77
90
|
var makeClock = (timeZone) => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
year: "numeric",
|
|
82
|
-
month: "2-digit",
|
|
83
|
-
day: "2-digit",
|
|
84
|
-
hour: "2-digit",
|
|
85
|
-
minute: "2-digit",
|
|
86
|
-
second: "2-digit"
|
|
87
|
-
});
|
|
91
|
+
let format;
|
|
92
|
+
let second = Number.NaN;
|
|
93
|
+
let last = { day: "", time: "" };
|
|
88
94
|
return (date) => {
|
|
95
|
+
const now = Math.floor(date.getTime() / 1000);
|
|
96
|
+
if (now === second)
|
|
97
|
+
return last;
|
|
98
|
+
format ??= new Intl.DateTimeFormat("en-US", {
|
|
99
|
+
timeZone,
|
|
100
|
+
hourCycle: "h23",
|
|
101
|
+
year: "numeric",
|
|
102
|
+
month: "2-digit",
|
|
103
|
+
day: "2-digit",
|
|
104
|
+
hour: "2-digit",
|
|
105
|
+
minute: "2-digit",
|
|
106
|
+
second: "2-digit"
|
|
107
|
+
});
|
|
89
108
|
const p = Object.fromEntries(format.formatToParts(date).map(({ type, value }) => [type, value]));
|
|
90
|
-
|
|
109
|
+
second = now;
|
|
110
|
+
last = {
|
|
91
111
|
day: `${p.year}-${p.month}-${p.day}`,
|
|
92
112
|
time: `${p.hour}:${p.minute}:${p.second}`
|
|
93
113
|
};
|
|
114
|
+
return last;
|
|
94
115
|
};
|
|
95
116
|
};
|
|
96
117
|
var createLogger = (options = {}) => {
|
|
@@ -110,11 +131,20 @@ var createLogger = (options = {}) => {
|
|
|
110
131
|
const clock = makeClock(timeZone);
|
|
111
132
|
const shape = shaper(replacer);
|
|
112
133
|
const madeDirs = new Set;
|
|
113
|
-
const
|
|
134
|
+
const toJson = (meta, merged) => {
|
|
135
|
+
const line = { ...meta, ...merged };
|
|
136
|
+
if (!replacer && plain(merged))
|
|
137
|
+
return JSON.stringify(line);
|
|
114
138
|
try {
|
|
115
|
-
return JSON.
|
|
139
|
+
return JSON.stringify(line, function(key, value) {
|
|
140
|
+
const own = key === "" || this === line && Object.hasOwn(meta, key) && !Object.hasOwn(merged, key);
|
|
141
|
+
return own ? value : shape(key, value);
|
|
142
|
+
});
|
|
116
143
|
} catch {
|
|
117
|
-
return
|
|
144
|
+
return JSON.stringify({
|
|
145
|
+
...meta,
|
|
146
|
+
...Object.fromEntries(pairs(merged, replacer))
|
|
147
|
+
});
|
|
118
148
|
}
|
|
119
149
|
};
|
|
120
150
|
const render = (level, msg, extra, date, colored) => {
|
|
@@ -126,7 +156,7 @@ var createLogger = (options = {}) => {
|
|
|
126
156
|
msg
|
|
127
157
|
};
|
|
128
158
|
if (format === "json") {
|
|
129
|
-
return
|
|
159
|
+
return toJson(meta, merged);
|
|
130
160
|
}
|
|
131
161
|
const context = pairs(merged, replacer);
|
|
132
162
|
if (format === "logfmt") {
|