@monochromatic-dev/module-logger 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/CHANGELOG.md +11 -0
- package/LICENSES/GPL-3.0-or-later.txt +674 -0
- package/LICENSES/LGPL-3.0-or-later.txt +165 -0
- package/README.md +404 -0
- package/dist/final/neutral/index.d.mts +673 -0
- package/dist/final/neutral/index.mjs +3 -0
- package/dist/final/neutral/rolldown-runtime-5duEfhBv.mjs +1 -0
- package/dist/final/node/index.d.mts +673 -0
- package/dist/final/node/index.mjs +3 -0
- package/dist/final/node/rolldown-runtime-5duEfhBv.mjs +1 -0
- package/package.json +43 -0
- package/src/create-logger.ts +494 -0
- package/src/create-logger.unit.test.ts +752 -0
- package/src/error-format.ts +43 -0
- package/src/index.ts +35 -0
- package/src/logger.ts +67 -0
- package/src/logger.unit.test.ts +190 -0
- package/src/sink/console-control-chars.ts +140 -0
- package/src/sink/console-control-chars.unit.test.ts +206 -0
- package/src/sink/console.ts +531 -0
- package/src/sink/console.unit.test.ts +542 -0
- package/src/sink/file.ts +297 -0
- package/src/sink/file.unit.test.ts +202 -0
- package/src/sink/index.ts +11 -0
- package/src/sink/indexed-db-util.ts +96 -0
- package/src/sink/indexed-db.browser.test.ts +184 -0
- package/src/sink/indexed-db.ts +324 -0
- package/src/sink/indexed-db.unit.test.ts +80 -0
- package/src/sink/local-storage-key.ts +176 -0
- package/src/sink/local-storage-key.unit.test.ts +106 -0
- package/src/sink/local-storage-quota.ts +60 -0
- package/src/sink/local-storage-quota.unit.test.ts +98 -0
- package/src/sink/local-storage-store.ts +368 -0
- package/src/sink/local-storage-store.unit.test.ts +329 -0
- package/src/sink/local-storage.browser.test.ts +125 -0
- package/src/sink/local-storage.ts +182 -0
- package/src/sink/local-storage.unit.test.ts +218 -0
- package/src/sink/noop.ts +46 -0
- package/src/sink/noop.unit.test.ts +47 -0
- package/src/sink/opfs.browser.test.ts +84 -0
- package/src/sink/opfs.ts +212 -0
- package/src/sink/opfs.unit.test.ts +81 -0
- package/src/sink/record-buffer.ts +230 -0
- package/src/sink/record-buffer.unit.test.ts +288 -0
- package/src/sink/session-storage-quota.ts +57 -0
- package/src/sink/session-storage-quota.unit.test.ts +98 -0
- package/src/sink/session-storage-store.ts +178 -0
- package/src/sink/session-storage.browser.test.ts +137 -0
- package/src/sink/session-storage.ts +128 -0
- package/src/sink/session-storage.unit.test.ts +527 -0
- package/src/sink/web-storage-quota-error.ts +43 -0
- package/src/sink/web-storage-quota-error.unit.test.ts +55 -0
- package/src/sink/web-storage-runtime.ts +49 -0
- package/src/startup.unit.test.ts +232 -0
- package/src/tagged.ts +74 -0
- package/src/tagged.unit.test.ts +211 -0
- package/src/types.ts +78 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
# module-logger
|
|
2
|
+
|
|
3
|
+
Zero-config multi-sink logger with tagged composition.
|
|
4
|
+
Works immediately at import:
|
|
5
|
+
auto-discovers available backends for the current runtime,
|
|
6
|
+
and records emitted while async backend verification is still pending replay to those
|
|
7
|
+
backends as soon as they verify.
|
|
8
|
+
Consumers do not await `initPromise` before logging.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { tagged, } from '@monochromatic-dev/module-logger';
|
|
14
|
+
|
|
15
|
+
const l = tagged({ tag: 'http', },);
|
|
16
|
+
l.info('server started on port 3000',);
|
|
17
|
+
// console output: [info] [2026-03-11T...] [http] server started on port 3000
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Composing tags
|
|
21
|
+
|
|
22
|
+
Pass a parent logger via the `l` parameter to build hierarchical prefixes:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
const l = tagged({ tag: 'http', },);
|
|
26
|
+
const rl = tagged({ tag: 'retry', l, },);
|
|
27
|
+
rl.warn('attempt 3 failed',);
|
|
28
|
+
// [warn] [...] [http] [retry] attempt 3 failed
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Composed tags render root-first because each wrapper prepends before delegating to its parent logger.
|
|
32
|
+
|
|
33
|
+
Convention:
|
|
34
|
+
use `myFn.name` as the tag so prefixes stay in sync with refactors.
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
function handleRequest({ l, }: { l: Logger; },): void {
|
|
38
|
+
const rl = tagged({ tag: handleRequest.name, l, },);
|
|
39
|
+
rl.info('received',);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Default logger (no tags)
|
|
44
|
+
|
|
45
|
+
Import the singleton directly when tags are not needed:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { logger, } from '@monochromatic-dev/module-logger';
|
|
49
|
+
|
|
50
|
+
logger.error('unexpected shutdown',);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Runtime support
|
|
54
|
+
|
|
55
|
+
Node 24 or newer (the build calls `Error.isError`),
|
|
56
|
+
plus current browsers,
|
|
57
|
+
Deno,
|
|
58
|
+
and Bun for the sinks whose `verify` finds a backend there.
|
|
59
|
+
The published package exposes the built artifact only.
|
|
60
|
+
The `/ts` source subpath used inside this workspace is stripped at publish time,
|
|
61
|
+
because Node refuses `.ts` files under `node_modules`.
|
|
62
|
+
|
|
63
|
+
## Log levels
|
|
64
|
+
|
|
65
|
+
Six levels,
|
|
66
|
+
each mapping to a dedicated method:
|
|
67
|
+
`trace`,
|
|
68
|
+
`debug`,
|
|
69
|
+
`info`,
|
|
70
|
+
`warn`,
|
|
71
|
+
`error`,
|
|
72
|
+
`fatal`.
|
|
73
|
+
|
|
74
|
+
All methods accept a single `string` argument.
|
|
75
|
+
The caller owns serialization;
|
|
76
|
+
template literals cover the common case
|
|
77
|
+
and keep the logger free of stringify opinions.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
l.info(`status ${code} for ${url}`,);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The console sink silences `debug` and `trace` by default in non-browser environments.
|
|
84
|
+
Set `MONOCHROMATIC_VERBOSE=true` or pass `--verbose` to enable them.
|
|
85
|
+
In browsers,
|
|
86
|
+
verbose mode is enabled automatically because DevTools
|
|
87
|
+
already provides its own log-level filtering.
|
|
88
|
+
|
|
89
|
+
## Sinks
|
|
90
|
+
|
|
91
|
+
The default logger writes to **all** available sinks simultaneously.
|
|
92
|
+
Availability is verified at module load;
|
|
93
|
+
records emitted while an async sink is
|
|
94
|
+
still being verified are replayed to that sink when it becomes available.
|
|
95
|
+
|
|
96
|
+
- **console**:
|
|
97
|
+
formats as `[level] [ISO timestamp] message`;
|
|
98
|
+
maps levels to corresponding `console.*` methods,
|
|
99
|
+
except `debug` writes to `process.stderr` when `process.stderr.write` is available
|
|
100
|
+
- **file**:
|
|
101
|
+
Node.
|
|
102
|
+
js only;
|
|
103
|
+
walks up from `process.cwd()` to the nearest
|
|
104
|
+
ancestor `node_modules/`,
|
|
105
|
+
then appends JSONL records to
|
|
106
|
+
`<that dir>/node_modules/.monochromatic/{timestamp}.log.jsonl` via
|
|
107
|
+
`node:fs/promises`.
|
|
108
|
+
When no ancestor `node_modules/` exists,
|
|
109
|
+
the sink
|
|
110
|
+
is marked unavailable rather than creating one at cwd;
|
|
111
|
+
this prevents
|
|
112
|
+
stray log directories from landing inside build output or other
|
|
113
|
+
non-project trees when a script is invoked from an unexpected cwd
|
|
114
|
+
- **IndexedDB**:
|
|
115
|
+
browser only;
|
|
116
|
+
buffers records through the same shared policy and flush triggers as the
|
|
117
|
+
sessionStorage sink and stores each newline-joined JSONL batch as one
|
|
118
|
+
string value per transaction in the `monochromatic.log` database
|
|
119
|
+
(`batch` store,
|
|
120
|
+
auto-incremented keys,
|
|
121
|
+
so concurrent tabs serialize
|
|
122
|
+
without any key scheme);
|
|
123
|
+
records are readable the moment their transaction settles,
|
|
124
|
+
DevTools
|
|
125
|
+
Application tab included,
|
|
126
|
+
and survive tab close and browser restart;
|
|
127
|
+
retention trims oldest-first past 2048 stored batches;
|
|
128
|
+
`logger.flush()` settles every issued batch transaction
|
|
129
|
+
- **OPFS** (exported but not a default):
|
|
130
|
+
browser only;
|
|
131
|
+
buffers records and appends them to the Origin Private File System as
|
|
132
|
+
newline-joined JSONL batches,
|
|
133
|
+
one queued stream write per batch,
|
|
134
|
+
under
|
|
135
|
+
the same flush triggers as the sessionStorage sink;
|
|
136
|
+
keeps a `FileSystemWritableFileStream` open for the session,
|
|
137
|
+
and `logger.flush()` settles every issued batch write;
|
|
138
|
+
staged stream content only becomes the file on a close a crash never
|
|
139
|
+
performs,
|
|
140
|
+
which is why the IndexedDB sink holds the default
|
|
141
|
+
persistent-browser slot instead (see `DECISIONS.md`)
|
|
142
|
+
- **sessionStorage**:
|
|
143
|
+
available wherever `globalThis.sessionStorage` exists (browsers,
|
|
144
|
+
Node,
|
|
145
|
+
Deno);
|
|
146
|
+
buffers records and stores them as newline-joined JSONL batches under
|
|
147
|
+
`monochromatic.log.{n}` keys with an auto-incrementing counter;
|
|
148
|
+
one uniform write path on every runtime flushes a batch when it reaches
|
|
149
|
+
32 KiB,
|
|
150
|
+
when a record's severity is `warn` or worse,
|
|
151
|
+
after 250 ms of quiet,
|
|
152
|
+
on `pagehide`/document-hidden where those events exist,
|
|
153
|
+
and on
|
|
154
|
+
`logger.flush()`;
|
|
155
|
+
caps its own footprint at half the runtime's default sessionStorage quota
|
|
156
|
+
(a measured per-runtime heuristic:
|
|
157
|
+
5 MiB on Node and the browser engines,
|
|
158
|
+
10 MiB on Deno),
|
|
159
|
+
evicting its oldest batches first and reclaiming further space reactively
|
|
160
|
+
if the real store still overflows
|
|
161
|
+
- **localStorage**:
|
|
162
|
+
available wherever `globalThis.localStorage` round-trips (browsers,
|
|
163
|
+
Deno,
|
|
164
|
+
Node launched with `--localstorage-file`;
|
|
165
|
+
flagless Node skips the probe silently);
|
|
166
|
+
buffers through the same shared policy and flush triggers as the
|
|
167
|
+
sessionStorage sink,
|
|
168
|
+
but stores each batch under a run-scoped key
|
|
169
|
+
(`monochromatic.log.{stamp}.{nonce}.{index}`) because localStorage is shared
|
|
170
|
+
across tabs and survives restarts;
|
|
171
|
+
adopts entries left by earlier runs and evicts oldest-first,
|
|
172
|
+
capping the combined footprint at half the runtime's default localStorage
|
|
173
|
+
quota (5 MiB on Node and the browser engines,
|
|
174
|
+
just under 10 MiB on Deno);
|
|
175
|
+
the one web storage sink whose records remain inspectable after tab close
|
|
176
|
+
or a full browser restart
|
|
177
|
+
- **noop**:
|
|
178
|
+
discards all records;
|
|
179
|
+
a stand-in that disables logging without removing log calls
|
|
180
|
+
|
|
181
|
+
Each sink is a factory,
|
|
182
|
+
`createConsoleSink()`,
|
|
183
|
+
`createFileSink()`,
|
|
184
|
+
`createIndexedDbSink()`,
|
|
185
|
+
`createOpfsSink()`,
|
|
186
|
+
`createSessionStorageSink()`,
|
|
187
|
+
`createLocalStorageSink()`,
|
|
188
|
+
and `createNoopSink()`,
|
|
189
|
+
exported under the `sinks` namespace.
|
|
190
|
+
A sink instance keeps its own buffers,
|
|
191
|
+
streams,
|
|
192
|
+
and counters,
|
|
193
|
+
so independent loggers never
|
|
194
|
+
share state.
|
|
195
|
+
|
|
196
|
+
Async sinks are fire-and-forget;
|
|
197
|
+
the log call never blocks the caller.
|
|
198
|
+
Call `logger.flush()` before assertions or process shutdown to wait for startup
|
|
199
|
+
verification,
|
|
200
|
+
pending sink writes,
|
|
201
|
+
and sink-owned flush hooks.
|
|
202
|
+
A sink is dropped only when its `verify` reports the backend unavailable (resolves
|
|
203
|
+
`false` or rejects);
|
|
204
|
+
a sink whose `flush` hook rejects is also dropped.
|
|
205
|
+
Individual `write` failures are the sink's own concern and do not disable the backend,
|
|
206
|
+
so one transient I/O hiccup never silently kills a sink for the rest of the run.
|
|
207
|
+
|
|
208
|
+
## Console output safety
|
|
209
|
+
|
|
210
|
+
Log text can carry attacker-influenced content,
|
|
211
|
+
and a terminal treats control characters as commands (clear screen,
|
|
212
|
+
set title,
|
|
213
|
+
move the cursor,
|
|
214
|
+
write the clipboard).
|
|
215
|
+
The console sink renders every C0 control except newline and tab,
|
|
216
|
+
`DEL`,
|
|
217
|
+
and every C1 control as a `\uXXXX` escape before the text reaches `console.*` or `process.stderr`,
|
|
218
|
+
so the attempted sequence stays visible but inert.
|
|
219
|
+
Newlines and tabs pass through because multi-line messages are core.
|
|
220
|
+
The JSONL sinks need no such step:
|
|
221
|
+
`JSON.stringify` already escapes control characters.
|
|
222
|
+
|
|
223
|
+
## Log record format
|
|
224
|
+
|
|
225
|
+
Every sink receives a `LogRecord`:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
type LogRecord = {
|
|
229
|
+
level: Level; // 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'
|
|
230
|
+
message: string;
|
|
231
|
+
timestamp: number; // Date.now()
|
|
232
|
+
};
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
File,
|
|
236
|
+
IndexedDB,
|
|
237
|
+
OPFS,
|
|
238
|
+
sessionStorage,
|
|
239
|
+
and localStorage sinks write records as one JSON object per line (JSONL).
|
|
240
|
+
|
|
241
|
+
## Error handling
|
|
242
|
+
|
|
243
|
+
- `initPromise` resolves after eager verification and startup replay;
|
|
244
|
+
consumers do not await it before logging
|
|
245
|
+
- `logger.flush()` awaits startup verification,
|
|
246
|
+
pending sink writes,
|
|
247
|
+
and sink-owned flush hooks,
|
|
248
|
+
all under one deadline (`flushDeadlineMs`,
|
|
249
|
+
default `DEFAULT_FLUSH_DEADLINE_MS`,
|
|
250
|
+
5000 ms).
|
|
251
|
+
When the deadline elapses the logger reports one `console.warn` breadcrumb,
|
|
252
|
+
drops the in-flight writes from its view (sinks expose no cancellation,
|
|
253
|
+
so the work continues in the background),
|
|
254
|
+
and resolves,
|
|
255
|
+
so a wedged backend cannot hang a shutdown
|
|
256
|
+
- Throws at log time once initialization has completed with no available backend.
|
|
257
|
+
The console sink verifies wherever `console` and `queueMicrotask` exist,
|
|
258
|
+
so this is reachable only through `createLogger` with sinks that all fail verification
|
|
259
|
+
- A sink is dropped when its `verify` reports unavailable (or its flush hook rejects);
|
|
260
|
+
remaining sinks continue
|
|
261
|
+
- Individual `write` failures are handled per sink and do not disable the backend
|
|
262
|
+
|
|
263
|
+
## Custom loggers
|
|
264
|
+
|
|
265
|
+
The default `logger` is `createLogger` applied to the default sink set,
|
|
266
|
+
and it stays
|
|
267
|
+
zero-config.
|
|
268
|
+
`createLogger` is also exported for building a logger over an explicit sink
|
|
269
|
+
list,
|
|
270
|
+
for example to write to a fixed subset of backends or to inject a fake in tests.
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
import { createLogger, sinks, } from '@monochromatic-dev/module-logger';
|
|
274
|
+
|
|
275
|
+
const { logger, initPromise, } = createLogger({ sinks: [sinks.createNoopSink()], },);
|
|
276
|
+
logger.info('goes nowhere');
|
|
277
|
+
await initPromise; // optional; flush() awaits it internally
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Raise the flush deadline for a slow but working backend,
|
|
281
|
+
such as a network filesystem:
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
import { createLogger, sinks, } from '@monochromatic-dev/module-logger';
|
|
285
|
+
|
|
286
|
+
const { logger, } = createLogger({
|
|
287
|
+
sinks: [sinks.createFileSink(),],
|
|
288
|
+
flushDeadlineMs: 30_000,
|
|
289
|
+
},);
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
A custom sink is any object satisfying the `Sink` interface,
|
|
293
|
+
a single self-describing
|
|
294
|
+
adapter carrying `verify`,
|
|
295
|
+
`write`,
|
|
296
|
+
and an optional `flush`:
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
import type { LogRecord, Sink, } from '@monochromatic-dev/module-logger';
|
|
300
|
+
|
|
301
|
+
function createArraySink(): { records: LogRecord[]; sink: Sink; } {
|
|
302
|
+
const records: LogRecord[] = [];
|
|
303
|
+
const sink: Sink = {
|
|
304
|
+
verify: () => Promise.resolve(true),
|
|
305
|
+
write: (record) => {
|
|
306
|
+
records.push(record);
|
|
307
|
+
return Promise.resolve();
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
return { records, sink, };
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
`verify`,
|
|
315
|
+
`write`,
|
|
316
|
+
and `flush` are all async (returning `Promise`) so the logger awaits
|
|
317
|
+
them uniformly.
|
|
318
|
+
|
|
319
|
+
## Design decisions
|
|
320
|
+
|
|
321
|
+
See [DECISIONS.md](DECISIONS.md) for rationale on:
|
|
322
|
+
|
|
323
|
+
- No sub-logger hierarchy;
|
|
324
|
+
per-component filtering is a log viewer problem
|
|
325
|
+
- String-only messages;
|
|
326
|
+
callers own serialization;
|
|
327
|
+
no auto-stringify
|
|
328
|
+
- Sinks are self-describing factories;
|
|
329
|
+
the logger owns availability
|
|
330
|
+
- Write failures do not disable a sink;
|
|
331
|
+
only verify failure does
|
|
332
|
+
- localStorage and IndexedDB sink designs and measurements
|
|
333
|
+
- Console output neutralizes control characters
|
|
334
|
+
- `flush()` has a deadline
|
|
335
|
+
|
|
336
|
+
## Source files
|
|
337
|
+
|
|
338
|
+
- `src/types.ts`:
|
|
339
|
+
`Logger`,
|
|
340
|
+
`LogRecord`,
|
|
341
|
+
`Sink`,
|
|
342
|
+
`SinkFlush`,
|
|
343
|
+
`Verify`,
|
|
344
|
+
`Level` type definitions
|
|
345
|
+
- `src/create-logger.ts`:
|
|
346
|
+
`createLogger({ sinks })` orchestration (verify,
|
|
347
|
+
startup replay,
|
|
348
|
+
flush)
|
|
349
|
+
- `src/logger.ts`:
|
|
350
|
+
default singleton built by applying `createLogger` to the default sinks
|
|
351
|
+
- `src/tagged.ts`:
|
|
352
|
+
`tagged()` wrapper for composable prefixes
|
|
353
|
+
- `src/sink/console.ts`:
|
|
354
|
+
`createConsoleSink()`,
|
|
355
|
+
verbose-mode gating and microtask batching
|
|
356
|
+
- `src/sink/console-control-chars.ts`:
|
|
357
|
+
control-character neutralization for console-bound text
|
|
358
|
+
- `src/sink/file.ts`:
|
|
359
|
+
`createFileSink()`,
|
|
360
|
+
Node.
|
|
361
|
+
js file sink (JSONL via `appendFile`)
|
|
362
|
+
- `src/sink/indexed-db.ts`:
|
|
363
|
+
`createIndexedDbSink()`,
|
|
364
|
+
default persistent-browser sink (one transaction per batch,
|
|
365
|
+
retention trim)
|
|
366
|
+
- `src/sink/indexed-db-util.ts`:
|
|
367
|
+
promise bridges for the event-based IndexedDB API
|
|
368
|
+
- `src/sink/opfs.ts`:
|
|
369
|
+
`createOpfsSink()`,
|
|
370
|
+
opt-in browser OPFS sink with persistent writable stream (batched writes)
|
|
371
|
+
- `src/sink/record-buffer.ts`:
|
|
372
|
+
buffering stage shared by the OPFS and sessionStorage sinks
|
|
373
|
+
(byte cap,
|
|
374
|
+
severity flush,
|
|
375
|
+
quiet-period deadline,
|
|
376
|
+
page lifecycle)
|
|
377
|
+
- `src/sink/session-storage.ts`:
|
|
378
|
+
`createSessionStorageSink()`,
|
|
379
|
+
cross-runtime web storage sink (buffering,
|
|
380
|
+
flush triggers)
|
|
381
|
+
- `src/sink/session-storage-store.ts`:
|
|
382
|
+
persistence engine behind it (key allocation,
|
|
383
|
+
footprint accounting,
|
|
384
|
+
quota eviction)
|
|
385
|
+
- `src/sink/local-storage.ts`:
|
|
386
|
+
`createLocalStorageSink()`,
|
|
387
|
+
cross-runtime persistent web storage sink (same buffering,
|
|
388
|
+
run-scoped keys)
|
|
389
|
+
- `src/sink/local-storage-store.ts`:
|
|
390
|
+
persistence engine behind it (run identity,
|
|
391
|
+
prior-run adoption,
|
|
392
|
+
cross-run oldest-first eviction)
|
|
393
|
+
- `src/sink/local-storage-key.ts`:
|
|
394
|
+
run-scoped key building,
|
|
395
|
+
strict parsing,
|
|
396
|
+
and eviction ordering
|
|
397
|
+
- `src/sink/local-storage-quota.ts` and `src/sink/session-storage-quota.ts`:
|
|
398
|
+
fill-probed per-runtime quota tables
|
|
399
|
+
- `src/sink/web-storage-runtime.ts` and `src/sink/web-storage-quota-error.ts`:
|
|
400
|
+
host-runtime detection and quota-overflow recognition shared by both
|
|
401
|
+
web storage engines
|
|
402
|
+
- `src/sink/noop.ts`:
|
|
403
|
+
`createNoopSink()`,
|
|
404
|
+
discards all records
|