@logtape/logtape 0.1.0-dev.12 → 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 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
+ ![](./screenshots/web-console.png)
20
+ ![](./screenshots/terminal-console.png)
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
- const stderrSink = getStreamSink(Deno.stderr.writable);
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
- const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));
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/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 formatter The text formatter to use. Defaults to
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, formatter = defaultTextFormatter, encoder = new TextEncoder()) {
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 formatter A console formatter. Defaults to
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(formatter = defaultConsoleFormatter, console = globalThis.console) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "0.1.0-dev.12+e99600b7",
3
+ "version": "0.1.0-dev.13+11d80a56",
4
4
  "description": "Simple logging library for Deno/Node.js/Bun/browsers",
5
5
  "keywords": [
6
6
  "logging",
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 formatter The text formatter to use. Defaults to
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, formatter = formatter_js_1.defaultTextFormatter, encoder = new TextEncoder()) {
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 formatter A console formatter. Defaults to
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(formatter = formatter_js_1.defaultConsoleFormatter, console = globalThis.console) {
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/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 formatter The text formatter to use. Defaults to
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, formatter?: TextFormatter, encoder?: {
44
- encode(text: string): Uint8Array;
45
- }): Sink;
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 formatter A console formatter. Defaults to
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(formatter?: ConsoleFormatter, console?: Console): Sink;
75
+ export declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
55
76
  //# sourceMappingURL=sink.d.ts.map
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,OAAO,CAAC,cAAc,EAC9B,SAAS,GAAE,aAAoC,EAC/C,OAAO,GAAE;IAAE,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAAA;CAAsB,GAChE,IAAI,CAMN;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,SAAS,GAAE,gBAA0C,EACrD,OAAO,GAAE,OAA4B,GACpC,IAAI,CAUN"}
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"}