@logtape/logtape 0.1.0-dev.10 → 0.1.0-dev.13
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 +88 -2
- package/esm/formatter.js +7 -4
- package/esm/sink.js +8 -9
- package/package.json +1 -1
- package/script/formatter.js +7 -4
- package/script/sink.js +8 -9
- package/types/formatter.d.ts.map +1 -1
- package/types/sink.d.ts +32 -11
- package/types/sink.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,9 @@ designed to be used for both applications and libraries.
|
|
|
16
16
|
Currently, LogTape provides only few sinks, but you can easily add your own
|
|
17
17
|
sinks.
|
|
18
18
|
|
|
19
|
+

|
|
20
|
+

|
|
21
|
+
|
|
19
22
|
[GitHub Actions]: https://github.com/dahlia/logtape/actions/workflows/main.yaml
|
|
20
23
|
[GitHub Actions badge]: https://github.com/dahlia/logtape/actions/workflows/main.yaml/badge.svg
|
|
21
24
|
[Codecov]: https://codecov.io/gh/dahlia/logtape
|
|
@@ -126,6 +129,23 @@ to loggers whose categories are `["my-app", "my-module"]` and `["my-app"]`.
|
|
|
126
129
|
This behavior allows you to control the verbosity of log messages by setting
|
|
127
130
|
the log level of loggers at different levels of the category hierarchy.
|
|
128
131
|
|
|
132
|
+
Here's an example of setting log levels for different categories:
|
|
133
|
+
|
|
134
|
+
~~~~ typescript
|
|
135
|
+
import { configure, getConsoleSink } from "@logtape/logtape";
|
|
136
|
+
|
|
137
|
+
configure({
|
|
138
|
+
sinks: {
|
|
139
|
+
console: getConsoleSink(),
|
|
140
|
+
},
|
|
141
|
+
filters: {},
|
|
142
|
+
loggers: [
|
|
143
|
+
{ category: ["my-app"], level: "info", sinks: ["console"] },
|
|
144
|
+
{ category: ["my-app", "my-module"], level: "debug", sinks: ["console"] },
|
|
145
|
+
],
|
|
146
|
+
})
|
|
147
|
+
~~~~
|
|
148
|
+
|
|
129
149
|
|
|
130
150
|
Sinks
|
|
131
151
|
-----
|
|
@@ -153,6 +173,8 @@ configure({
|
|
|
153
173
|
});
|
|
154
174
|
~~~~
|
|
155
175
|
|
|
176
|
+
### Console sink
|
|
177
|
+
|
|
156
178
|
Of course, you don't have to implement your own console sink because LogTape
|
|
157
179
|
provides a console sink:
|
|
158
180
|
|
|
@@ -167,19 +189,38 @@ configure({
|
|
|
167
189
|
});
|
|
168
190
|
~~~~
|
|
169
191
|
|
|
192
|
+
See also [`getConsoleSink()`] function and [`ConsoleSinkOptions`] interface
|
|
193
|
+
in the API reference for more details.
|
|
194
|
+
|
|
195
|
+
[`getConsoleSink()`]: https://jsr.io/@logtape/logtape/doc/~/getConsoleSink
|
|
196
|
+
[`ConsoleSinkOptions`]: https://jsr.io/@logtape/logtape/doc/~/ConsoleSinkOptions
|
|
197
|
+
|
|
198
|
+
### Stream sink
|
|
199
|
+
|
|
170
200
|
Another built-in sink is a stream sink. It writes log messages to
|
|
171
201
|
a [`WritableStream`]. Here's an example of a stream sink that writes log
|
|
172
202
|
messages to the standard error:
|
|
173
203
|
|
|
174
204
|
~~~~ typescript
|
|
175
205
|
// Deno:
|
|
176
|
-
|
|
206
|
+
configure({
|
|
207
|
+
sinks: {
|
|
208
|
+
stream: getStreamSink(Deno.stderr.writable),
|
|
209
|
+
},
|
|
210
|
+
// Omitted for brevity
|
|
211
|
+
});
|
|
177
212
|
~~~~
|
|
178
213
|
|
|
179
214
|
~~~~ typescript
|
|
180
215
|
// Node.js:
|
|
181
216
|
import stream from "node:stream";
|
|
182
|
-
|
|
217
|
+
|
|
218
|
+
configure({
|
|
219
|
+
sinks: {
|
|
220
|
+
stream: getStreamSink(stream.Writable.toWeb(process.stderr)),
|
|
221
|
+
},
|
|
222
|
+
// Omitted for brevity
|
|
223
|
+
});
|
|
183
224
|
~~~~
|
|
184
225
|
|
|
185
226
|
> [!NOTE]
|
|
@@ -189,6 +230,51 @@ const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));
|
|
|
189
230
|
> Node.js stream. You can use [`Writable.toWeb()`] method to convert a Node.js
|
|
190
231
|
> stream to a `WritableStream`.
|
|
191
232
|
|
|
233
|
+
See also [`getStreamSink()`] function and [`StreamSinkOptions`] interface
|
|
234
|
+
in the API reference for more details.
|
|
235
|
+
|
|
192
236
|
[`WritableStream`]: https://developer.mozilla.org/en-US/docs/Web/API/WritableStream
|
|
193
237
|
[`Writable`]: https://nodejs.org/api/stream.html#class-streamwritable
|
|
194
238
|
[`Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable
|
|
239
|
+
[`getStreamSink()`]: https://jsr.io/@logtape/logtape/doc/~/getStreamSink
|
|
240
|
+
[`StreamSinkOptions`]: https://jsr.io/@logtape/logtape/doc/~/StreamSinkOptions
|
|
241
|
+
|
|
242
|
+
### Buffer sink
|
|
243
|
+
|
|
244
|
+
For testing purposes, you may want to collect log messages in memory. Although
|
|
245
|
+
LogTape does not provide a built-in buffer sink, you can easily implement it:
|
|
246
|
+
|
|
247
|
+
~~~~ typescript
|
|
248
|
+
import { type LogRecord, configure } from "@logtape/logtape";
|
|
249
|
+
|
|
250
|
+
const buffer: LogRecord[] = [];
|
|
251
|
+
|
|
252
|
+
configure({
|
|
253
|
+
sinks: {
|
|
254
|
+
buffer: buffer.push.bind(buffer),
|
|
255
|
+
},
|
|
256
|
+
// Omitted for brevity
|
|
257
|
+
});
|
|
258
|
+
~~~~
|
|
259
|
+
|
|
260
|
+
### Text formatter
|
|
261
|
+
|
|
262
|
+
A stream sink writes log messages in a plain text format. You can customize
|
|
263
|
+
the format by providing a text formatter. The type of a text formatter is:
|
|
264
|
+
|
|
265
|
+
~~~~ typescript
|
|
266
|
+
export type TextFormatter = (record: LogRecord) => string;
|
|
267
|
+
~~~~
|
|
268
|
+
|
|
269
|
+
Here's an example of a text formatter that writes log messages in a JSON format:
|
|
270
|
+
|
|
271
|
+
~~~~ typescript
|
|
272
|
+
configure({
|
|
273
|
+
sinks: {
|
|
274
|
+
stream: getStreamSink(Deno.stderr.writable, {
|
|
275
|
+
formatter: JSON.stringify,
|
|
276
|
+
}),
|
|
277
|
+
},
|
|
278
|
+
// Omitted for brevity
|
|
279
|
+
})
|
|
280
|
+
~~~~
|
package/esm/formatter.js
CHANGED
|
@@ -53,9 +53,9 @@ export function defaultTextFormatter(record) {
|
|
|
53
53
|
const logLevelStyles = {
|
|
54
54
|
"debug": "background-color: gray; color: white;",
|
|
55
55
|
"info": "background-color: white; color: black;",
|
|
56
|
-
"warning": "background-color: orange;",
|
|
57
|
-
"error": "background-color: red;",
|
|
58
|
-
"fatal": "background-color: maroon;",
|
|
56
|
+
"warning": "background-color: orange; color: black;",
|
|
57
|
+
"error": "background-color: red; color: white;",
|
|
58
|
+
"fatal": "background-color: maroon; color: white;",
|
|
59
59
|
};
|
|
60
60
|
/**
|
|
61
61
|
* The default console formatter.
|
|
@@ -75,8 +75,11 @@ export function defaultConsoleFormatter(record) {
|
|
|
75
75
|
values.push(record.message[i]);
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
+
const date = new Date(record.timestamp);
|
|
79
|
+
const time = `${date.getUTCHours().toString().padStart(2, "0")}:${date.getUTCMinutes().toString().padStart(2, "0")}:${date.getUTCSeconds().toString().padStart(2, "0")}.${date.getUTCMilliseconds().toString().padStart(3, "0")}`;
|
|
78
80
|
return [
|
|
79
|
-
`%c${record.level
|
|
81
|
+
`%c${time} %c${levelAbbreviations[record.level]}%c %c${record.category.join("\xb7")} %c${msg}`,
|
|
82
|
+
"color: gray;",
|
|
80
83
|
logLevelStyles[record.level],
|
|
81
84
|
"background-color: default;",
|
|
82
85
|
"color: gray;",
|
package/esm/sink.js
CHANGED
|
@@ -20,13 +20,12 @@ import { defaultConsoleFormatter, defaultTextFormatter, } from "./formatter.js";
|
|
|
20
20
|
* ```
|
|
21
21
|
*
|
|
22
22
|
* @param stream The stream to write to.
|
|
23
|
-
* @param
|
|
24
|
-
* {@link defaultTextFormatter}.
|
|
25
|
-
* @param encoder The text encoder to use. Defaults to an instance of
|
|
26
|
-
* {@link TextEncoder}.
|
|
23
|
+
* @param options The options for the sink.
|
|
27
24
|
* @returns A sink that writes to the stream.
|
|
28
25
|
*/
|
|
29
|
-
export function getStreamSink(stream,
|
|
26
|
+
export function getStreamSink(stream, options = {}) {
|
|
27
|
+
const formatter = options.formatter ?? defaultTextFormatter;
|
|
28
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
30
29
|
const writer = stream.getWriter();
|
|
31
30
|
return (record) => {
|
|
32
31
|
const bytes = encoder.encode(formatter(record));
|
|
@@ -36,12 +35,12 @@ export function getStreamSink(stream, formatter = defaultTextFormatter, encoder
|
|
|
36
35
|
/**
|
|
37
36
|
* A console sink factory that returns a sink that logs to the console.
|
|
38
37
|
*
|
|
39
|
-
* @param
|
|
40
|
-
* {@link defaultConsoleFormatter}.
|
|
41
|
-
* @param console The console to log to. Defaults to {@link console}.
|
|
38
|
+
* @param options The options for the sink.
|
|
42
39
|
* @returns A sink that logs to the console.
|
|
43
40
|
*/
|
|
44
|
-
export function getConsoleSink(
|
|
41
|
+
export function getConsoleSink(options = {}) {
|
|
42
|
+
const formatter = options.formatter ?? defaultConsoleFormatter;
|
|
43
|
+
const console = options.console ?? globalThis.console;
|
|
45
44
|
return (record) => {
|
|
46
45
|
const args = formatter(record);
|
|
47
46
|
if (record.level === "debug")
|
package/package.json
CHANGED
package/script/formatter.js
CHANGED
|
@@ -57,9 +57,9 @@ exports.defaultTextFormatter = defaultTextFormatter;
|
|
|
57
57
|
const logLevelStyles = {
|
|
58
58
|
"debug": "background-color: gray; color: white;",
|
|
59
59
|
"info": "background-color: white; color: black;",
|
|
60
|
-
"warning": "background-color: orange;",
|
|
61
|
-
"error": "background-color: red;",
|
|
62
|
-
"fatal": "background-color: maroon;",
|
|
60
|
+
"warning": "background-color: orange; color: black;",
|
|
61
|
+
"error": "background-color: red; color: white;",
|
|
62
|
+
"fatal": "background-color: maroon; color: white;",
|
|
63
63
|
};
|
|
64
64
|
/**
|
|
65
65
|
* The default console formatter.
|
|
@@ -79,8 +79,11 @@ function defaultConsoleFormatter(record) {
|
|
|
79
79
|
values.push(record.message[i]);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
+
const date = new Date(record.timestamp);
|
|
83
|
+
const time = `${date.getUTCHours().toString().padStart(2, "0")}:${date.getUTCMinutes().toString().padStart(2, "0")}:${date.getUTCSeconds().toString().padStart(2, "0")}.${date.getUTCMilliseconds().toString().padStart(3, "0")}`;
|
|
82
84
|
return [
|
|
83
|
-
`%c${record.level
|
|
85
|
+
`%c${time} %c${levelAbbreviations[record.level]}%c %c${record.category.join("\xb7")} %c${msg}`,
|
|
86
|
+
"color: gray;",
|
|
84
87
|
logLevelStyles[record.level],
|
|
85
88
|
"background-color: default;",
|
|
86
89
|
"color: gray;",
|
package/script/sink.js
CHANGED
|
@@ -23,13 +23,12 @@ const formatter_js_1 = require("./formatter.js");
|
|
|
23
23
|
* ```
|
|
24
24
|
*
|
|
25
25
|
* @param stream The stream to write to.
|
|
26
|
-
* @param
|
|
27
|
-
* {@link defaultTextFormatter}.
|
|
28
|
-
* @param encoder The text encoder to use. Defaults to an instance of
|
|
29
|
-
* {@link TextEncoder}.
|
|
26
|
+
* @param options The options for the sink.
|
|
30
27
|
* @returns A sink that writes to the stream.
|
|
31
28
|
*/
|
|
32
|
-
function getStreamSink(stream,
|
|
29
|
+
function getStreamSink(stream, options = {}) {
|
|
30
|
+
const formatter = options.formatter ?? formatter_js_1.defaultTextFormatter;
|
|
31
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
33
32
|
const writer = stream.getWriter();
|
|
34
33
|
return (record) => {
|
|
35
34
|
const bytes = encoder.encode(formatter(record));
|
|
@@ -40,12 +39,12 @@ exports.getStreamSink = getStreamSink;
|
|
|
40
39
|
/**
|
|
41
40
|
* A console sink factory that returns a sink that logs to the console.
|
|
42
41
|
*
|
|
43
|
-
* @param
|
|
44
|
-
* {@link defaultConsoleFormatter}.
|
|
45
|
-
* @param console The console to log to. Defaults to {@link console}.
|
|
42
|
+
* @param options The options for the sink.
|
|
46
43
|
* @returns A sink that logs to the console.
|
|
47
44
|
*/
|
|
48
|
-
function getConsoleSink(
|
|
45
|
+
function getConsoleSink(options = {}) {
|
|
46
|
+
const formatter = options.formatter ?? formatter_js_1.defaultConsoleFormatter;
|
|
47
|
+
const console = options.console ?? globalThis.console;
|
|
49
48
|
return (record) => {
|
|
50
49
|
const args = formatter(record);
|
|
51
50
|
if (record.level === "debug")
|
package/types/formatter.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../src/formatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvD;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,MAAM,CAAC;AA+B1D;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAW9D;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,SAAS,OAAO,EAAE,CAAC;AAazE;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../src/formatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvD;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,MAAM,CAAC;AA+B1D;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAW9D;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,SAAS,OAAO,EAAE,CAAC;AAazE;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,OAAO,EAAE,CA2B7E"}
|
package/types/sink.d.ts
CHANGED
|
@@ -13,6 +13,21 @@ import type { LogRecord } from "./record.js";
|
|
|
13
13
|
* @param record The log record to sink.
|
|
14
14
|
*/
|
|
15
15
|
export type Sink = (record: LogRecord) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Options for the {@link getStreamSink} function.
|
|
18
|
+
*/
|
|
19
|
+
export interface StreamSinkOptions {
|
|
20
|
+
/**
|
|
21
|
+
* The text formatter to use. Defaults to {@link defaultTextFormatter}.
|
|
22
|
+
*/
|
|
23
|
+
formatter?: TextFormatter;
|
|
24
|
+
/**
|
|
25
|
+
* The text encoder to use. Defaults to an instance of {@link TextEncoder}.
|
|
26
|
+
*/
|
|
27
|
+
encoder?: {
|
|
28
|
+
encode(text: string): Uint8Array;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
16
31
|
/**
|
|
17
32
|
* A factory that returns a sink that writes to a {@link WritableStream}.
|
|
18
33
|
*
|
|
@@ -34,22 +49,28 @@ export type Sink = (record: LogRecord) => void;
|
|
|
34
49
|
* ```
|
|
35
50
|
*
|
|
36
51
|
* @param stream The stream to write to.
|
|
37
|
-
* @param
|
|
38
|
-
* {@link defaultTextFormatter}.
|
|
39
|
-
* @param encoder The text encoder to use. Defaults to an instance of
|
|
40
|
-
* {@link TextEncoder}.
|
|
52
|
+
* @param options The options for the sink.
|
|
41
53
|
* @returns A sink that writes to the stream.
|
|
42
54
|
*/
|
|
43
|
-
export declare function getStreamSink(stream: dntShim.WritableStream,
|
|
44
|
-
|
|
45
|
-
}
|
|
55
|
+
export declare function getStreamSink(stream: dntShim.WritableStream, options?: StreamSinkOptions): Sink;
|
|
56
|
+
/**
|
|
57
|
+
* Options for the {@link getConsoleSink} function.
|
|
58
|
+
*/
|
|
59
|
+
export interface ConsoleSinkOptions {
|
|
60
|
+
/**
|
|
61
|
+
* The console formatter to use. Defaults to {@link defaultConsoleFormatter}.
|
|
62
|
+
*/
|
|
63
|
+
formatter?: ConsoleFormatter;
|
|
64
|
+
/**
|
|
65
|
+
* The console to log to. Defaults to {@link console}.
|
|
66
|
+
*/
|
|
67
|
+
console?: Console;
|
|
68
|
+
}
|
|
46
69
|
/**
|
|
47
70
|
* A console sink factory that returns a sink that logs to the console.
|
|
48
71
|
*
|
|
49
|
-
* @param
|
|
50
|
-
* {@link defaultConsoleFormatter}.
|
|
51
|
-
* @param console The console to log to. Defaults to {@link console}.
|
|
72
|
+
* @param options The options for the sink.
|
|
52
73
|
* @returns A sink that logs to the console.
|
|
53
74
|
*/
|
|
54
|
-
export declare function getConsoleSink(
|
|
75
|
+
export declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
|
|
55
76
|
//# sourceMappingURL=sink.d.ts.map
|
package/types/sink.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sink.d.ts","sourceRoot":"","sources":["../src/sink.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,OAAO,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EACL,KAAK,gBAAgB,EAGrB,KAAK,aAAa,EACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;;;;;GAQG;AACH,MAAM,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,IAAI,CAAC;AAE/C
|
|
1
|
+
{"version":3,"file":"sink.d.ts","sourceRoot":"","sources":["../src/sink.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,OAAO,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EACL,KAAK,gBAAgB,EAGrB,KAAK,aAAa,EACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;;;;;GAQG;AACH,MAAM,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,IAAI,CAAC;AAE/C;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,SAAS,CAAC,EAAE,aAAa,CAAC;IAE1B;;OAEG;IACH,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAAA;KAAE,CAAC;CAChD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,OAAO,CAAC,cAAc,EAC9B,OAAO,GAAE,iBAAsB,GAC9B,IAAI,CAQN;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAE7B;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,GAAE,kBAAuB,GAAG,IAAI,CAYrE"}
|