@logtape/logtape 0.1.0-dev.13 → 0.1.0-dev.14

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
@@ -239,6 +239,68 @@ in the API reference for more details.
239
239
  [`getStreamSink()`]: https://jsr.io/@logtape/logtape/doc/~/getStreamSink
240
240
  [`StreamSinkOptions`]: https://jsr.io/@logtape/logtape/doc/~/StreamSinkOptions
241
241
 
242
+ ### File sink
243
+
244
+ LogTape provides a platform-independent file sink. You can use it by providing
245
+ a platform-specific file driver for Deno or Node.js. Here's an example of
246
+ a file sink that writes log messages to a file:
247
+
248
+ ~~~~ typescript
249
+ // Deno
250
+ import { type FileSinkDriver, getFileSink } from "@logtape/logtape";
251
+
252
+ const driver: FileSinkDriver<Deno.FsFile> = {
253
+ openSync(path: string) {
254
+ return Deno.openSync(path, { create: true, append: true });
255
+ },
256
+ writeSync(fd, chunk) {
257
+ fd.writeSync(chunk);
258
+ },
259
+ flushSync(fd) {
260
+ fd.syncSync();
261
+ },
262
+ closeSync(fd) {
263
+ fd.close();
264
+ },
265
+ };
266
+
267
+ configure({
268
+ sinks: {
269
+ file: getFileSink("my-app.log", driver),
270
+ },
271
+ // Omitted for brevity
272
+ });
273
+ ~~~~
274
+
275
+ ~~~~ typescript
276
+ // Node.js or Bun
277
+ import fs from "node:fs";
278
+ import { type FileSinkDriver, getFileSink } from "@logtape/logtape";
279
+
280
+ const driver: FileSinkDriver<number> = {
281
+ openSync(path: string) {
282
+ return fs.openSync(path, "a");
283
+ },
284
+ writeSync: fs.writeSync,
285
+ flushSync: fs.fsyncSync,
286
+ closeSync: fs.closeSync,
287
+ };
288
+
289
+ configure({
290
+ sinks: {
291
+ file: getFileSink("my-app.log", driver),
292
+ },
293
+ // Omitted for brevity
294
+ });
295
+ ~~~~
296
+
297
+ See also [`getFileSink()`] function, [`FileSinkOptions`] interface, and
298
+ [`FileSinkDriver`] interface in the API reference for more details.
299
+
300
+ [`getFileSink()`]: https://jsr.io/@logtape/logtape/doc/~/getFileSink
301
+ [`FileSinkOptions`]: https://jsr.io/@logtape/logtape/doc/~/FileSinkOptions
302
+ [`FileSinkDriver`]: https://jsr.io/@logtape/logtape/doc/~/FileSinkDriver
303
+
242
304
  ### Buffer sink
243
305
 
244
306
  For testing purposes, you may want to collect log messages in memory. Although
@@ -259,8 +321,9 @@ configure({
259
321
 
260
322
  ### Text formatter
261
323
 
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:
324
+ A stream sink and a file sink write log messages in a plain text format.
325
+ You can customize the format by providing a text formatter. The type of a
326
+ text formatter is:
264
327
 
265
328
  ~~~~ typescript
266
329
  export type TextFormatter = (record: LogRecord) => string;
package/esm/sink.js CHANGED
@@ -56,3 +56,21 @@ export function getConsoleSink(options = {}) {
56
56
  throw new TypeError(`Invalid log level: ${record.level}.`);
57
57
  };
58
58
  }
59
+ /**
60
+ * Get a platform-independent file sink.
61
+ * @typeParam TFile The type of the file descriptor.
62
+ * @param path A path to the file to write to.
63
+ * @param options The options for the sink and the file driver.
64
+ * @returns A sink that writes to the file.
65
+ */
66
+ export function getFileSink(path, options) {
67
+ const formatter = options.formatter ?? defaultTextFormatter;
68
+ const encoder = options.encoder ?? new TextEncoder();
69
+ const fd = options.openSync(path);
70
+ const sink = (record) => {
71
+ options.writeSync(fd, encoder.encode(formatter(record)));
72
+ options.flushSync(fd);
73
+ };
74
+ sink.close = () => options.closeSync(fd);
75
+ return sink;
76
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "0.1.0-dev.13+11d80a56",
3
+ "version": "0.1.0-dev.14+777b0238",
4
4
  "description": "Simple logging library for Deno/Node.js/Bun/browsers",
5
5
  "keywords": [
6
6
  "logging",
package/script/sink.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getConsoleSink = exports.getStreamSink = void 0;
3
+ exports.getFileSink = exports.getConsoleSink = exports.getStreamSink = void 0;
4
4
  const formatter_js_1 = require("./formatter.js");
5
5
  /**
6
6
  * A factory that returns a sink that writes to a {@link WritableStream}.
@@ -61,3 +61,22 @@ function getConsoleSink(options = {}) {
61
61
  };
62
62
  }
63
63
  exports.getConsoleSink = getConsoleSink;
64
+ /**
65
+ * Get a platform-independent file sink.
66
+ * @typeParam TFile The type of the file descriptor.
67
+ * @param path A path to the file to write to.
68
+ * @param options The options for the sink and the file driver.
69
+ * @returns A sink that writes to the file.
70
+ */
71
+ function getFileSink(path, options) {
72
+ const formatter = options.formatter ?? formatter_js_1.defaultTextFormatter;
73
+ const encoder = options.encoder ?? new TextEncoder();
74
+ const fd = options.openSync(path);
75
+ const sink = (record) => {
76
+ options.writeSync(fd, encoder.encode(formatter(record)));
77
+ options.flushSync(fd);
78
+ };
79
+ sink.close = () => options.closeSync(fd);
80
+ return sink;
81
+ }
82
+ exports.getFileSink = getFileSink;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../../../src/deps/deno.land/x/which_runtime@0.2.0/mod.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,MAAM,SAAgC,CAAC;AACpD,eAAO,MAAM,MAAM,SAA0B,CAAC"}
package/types/sink.d.ts CHANGED
@@ -73,4 +73,45 @@ export interface ConsoleSinkOptions {
73
73
  * @returns A sink that logs to the console.
74
74
  */
75
75
  export declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
76
+ /**
77
+ * Options for the {@link getFileSink} function.
78
+ */
79
+ export type FileSinkOptions = StreamSinkOptions;
80
+ /**
81
+ * A platform-specific file sink driver.
82
+ * @typeParam TFile The type of the file descriptor.
83
+ */
84
+ export interface FileSinkDriver<TFile> {
85
+ /**
86
+ * Open a file for appending and return a file descriptor.
87
+ * @param path A path to the file to open.
88
+ */
89
+ openSync(path: string): TFile;
90
+ /**
91
+ * Write a chunk of data to the file.
92
+ * @param fd The file descriptor.
93
+ * @param chunk The data to write.
94
+ */
95
+ writeSync(fd: TFile, chunk: Uint8Array): void;
96
+ /**
97
+ * Flush the file to ensure that all data is written to the disk.
98
+ * @param fd The file descriptor.
99
+ */
100
+ flushSync(fd: TFile): void;
101
+ /**
102
+ * Close the file.
103
+ * @param fd The file descriptor.
104
+ */
105
+ closeSync(fd: TFile): void;
106
+ }
107
+ /**
108
+ * Get a platform-independent file sink.
109
+ * @typeParam TFile The type of the file descriptor.
110
+ * @param path A path to the file to write to.
111
+ * @param options The options for the sink and the file driver.
112
+ * @returns A sink that writes to the file.
113
+ */
114
+ export declare function getFileSink<TFile>(path: string, options: FileSinkOptions & FileSinkDriver<TFile>): Sink & {
115
+ close: () => void;
116
+ };
76
117
  //# 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;;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,kBA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