@logtape/logtape 0.2.0-dev.20 → 0.2.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/README.md CHANGED
@@ -12,17 +12,18 @@ LogTape
12
12
  > LogTape is still in the early stage of development. The API is not stable
13
13
  > yet. Please be careful when using it in production.
14
14
 
15
- LogTape is a simple logging library for Deno/Node.js/Bun/browsers. It is
16
- designed to be used for both applications and libraries.
15
+ LogTape is a simple logging library with zero dependencies for
16
+ Deno/Node.js/Bun/browsers. It is designed to be used for both applications
17
+ and libraries.
17
18
 
18
- Currently, LogTape provides only few sinks, but you can easily add your own
19
- sinks.
19
+ Currently, LogTape provides only few sinks, but [you can easily add your own
20
+ sinks.](#sinks)
20
21
 
21
22
  ![](./screenshots/web-console.png)
22
23
  ![](./screenshots/terminal-console.png)
23
24
 
24
25
  [JSR]: https://jsr.io/@logtape/logtape
25
- [JSR badge]: https://jsr.io/badges/@logtape/logtape
26
+ [JSR badge]: https://jsr.io/badges/@logtape/logtape?v=0.2.0
26
27
  [npm]: https://www.npmjs.com/package/@logtape/logtape
27
28
  [npm badge]: https://img.shields.io/npm/v/@logtape/logtape?logo=npm
28
29
  [GitHub Actions]: https://github.com/dahlia/logtape/actions/workflows/main.yaml
@@ -270,6 +271,37 @@ in the API reference for more details.
270
271
  [`getFileSink()`]: https://jsr.io/@logtape/logtape/doc/~/getFileSink
271
272
  [`FileSinkOptions`]: https://jsr.io/@logtape/logtape/doc/~/FileSinkOptions
272
273
 
274
+ ### Rotating file sink
275
+
276
+ > [!NOTE]
277
+ > File sink is unavailable in the browser environment.
278
+
279
+ A rotating file sink is a file sink that rotates log files. It creates a new
280
+ log file when the current log file reaches a certain size. Here's an example
281
+ of a rotating file sink that writes log messages to a file:
282
+
283
+ ~~~~ typescript
284
+ import { getRotatingFileSink } from "@logtape/logtape";
285
+
286
+ await configure({
287
+ sinks: {
288
+ file: getRotatingFileSink("my-app.log", {
289
+ maxFileSize: 1024 * 1024, // 1 MiB
290
+ maxFiles: 5,
291
+ }),
292
+ },
293
+ // Omitted for brevity
294
+ });
295
+ ~~~~
296
+
297
+ Rotated log files are named with a suffix like *.1*, *.2*, *.3*, and so on.
298
+
299
+ For more details, see [`getRotatingFileSink()`] function and
300
+ [`RotatingFileSinkOptions`] interface in the API reference.
301
+
302
+ [`getRotatingFileSink()`]: https://jsr.io/@logtape/logtape/doc/~/getRotatingFileSink
303
+ [`RotatingFileSinkOptions`]: https://jsr.io/@logtape/logtape/doc/~/RotatingFileSinkOptions
304
+
273
305
  ### Text formatter
274
306
 
275
307
  A stream sink and a file sink write log messages in a plain text format.
@@ -295,6 +327,10 @@ await configure({
295
327
 
296
328
  ### Disposable sink
297
329
 
330
+ > [!TIP]
331
+ > If you are unfamiliar with the concept of disposables, see also the proposal
332
+ > of *[ECMAScript Explicit Resource Management]*.
333
+
298
334
  A disposable sink is a sink that can be disposed of. They are automatically
299
335
  disposed of when the configuration is reset or the program exits. The type
300
336
  of a disposable sink is: `Sink & Disposable`. You can create a disposable
@@ -322,9 +358,11 @@ asyncDisposableSink[Symbol.asyncDispose] = async () => {
322
358
  };
323
359
  ~~~~
324
360
 
361
+ [ECMAScript Explicit Resource Management]: https://github.com/tc39/proposal-explicit-resource-management
362
+
325
363
  ### Explicit disposal
326
364
 
327
- You can explicitly dispose of a sink by calling the `dispose()` method. It is
365
+ You can explicitly dispose of a sink by calling the [`dispose()`] method. It is
328
366
  useful when you want to flush the buffer of a sink without blocking returning
329
367
  a response in edge functions. Here's an example of using the `dispose()`
330
368
  with [`ctx.waitUntil()`] in Cloudflare Workers:
@@ -341,6 +379,7 @@ export default {
341
379
  }
342
380
  ~~~~
343
381
 
382
+ [`dispose()`]: https://jsr.io/@logtape/logtape/doc/~/dispose
344
383
  [`ctx.waitUntil()`]: https://developers.cloudflare.com/workers/runtime-apis/context/#waituntil
345
384
 
346
385
 
@@ -1,7 +1,7 @@
1
1
  import * as dntShim from "./_dnt.shims.js";
2
2
  import fs from "node:fs";
3
3
  import { webDriver } from "./filesink.web.js";
4
- import { getFileSink as getBaseFileSink, } from "./sink.js";
4
+ import { getFileSink as getBaseFileSink, getRotatingFileSink as getBaseRotatingFileSink, } from "./sink.js";
5
5
  /**
6
6
  * A Node.js-specific file sink driver.
7
7
  */
@@ -12,6 +12,8 @@ export const nodeDriver = {
12
12
  writeSync: fs.writeSync,
13
13
  flushSync: fs.fsyncSync,
14
14
  closeSync: fs.closeSync,
15
+ statSync: fs.statSync,
16
+ renameSync: fs.renameSync,
15
17
  };
16
18
  /**
17
19
  * Get a file sink.
@@ -29,4 +31,25 @@ export function getFileSink(path, options = {}) {
29
31
  }
30
32
  return getBaseFileSink(path, { ...options, ...nodeDriver });
31
33
  }
34
+ /**
35
+ * Get a rotating file sink.
36
+ *
37
+ * This sink writes log records to a file, and rotates the file when it reaches
38
+ * the `maxSize`. The rotated files are named with the original file name
39
+ * followed by a dot and a number, starting from 1. The number is incremented
40
+ * for each rotation, and the maximum number of files to keep is `maxFiles`.
41
+ *
42
+ * Note that this function is unavailable in the browser.
43
+ *
44
+ * @param path A path to the file to write to.
45
+ * @param options The options for the sink and the file driver.
46
+ * @returns A sink that writes to the file. The sink is also a disposable
47
+ * object that closes the file when disposed.
48
+ */
49
+ export function getRotatingFileSink(path, options = {}) {
50
+ if ("document" in dntShim.dntGlobalThis) {
51
+ return getBaseRotatingFileSink(path, { ...options, ...webDriver });
52
+ }
53
+ return getBaseRotatingFileSink(path, { ...options, ...nodeDriver });
54
+ }
32
55
  // cSpell: ignore filesink
@@ -9,4 +9,6 @@ export const webDriver = {
9
9
  writeSync: notImplemented,
10
10
  flushSync: notImplemented,
11
11
  closeSync: notImplemented,
12
+ statSync: notImplemented,
13
+ renameSync: notImplemented,
12
14
  };
package/esm/mod.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { ConfigError, configure, dispose, reset, } from "./config.js";
2
- export { getFileSink } from "./filesink.node.js";
2
+ export { getFileSink, getRotatingFileSink } from "./filesink.node.js";
3
3
  export { getLevelFilter, toFilter, } from "./filter.js";
4
4
  export { defaultConsoleFormatter, defaultTextFormatter, } from "./formatter.js";
5
5
  export { getLogger } from "./logger.js";
package/esm/sink.js CHANGED
@@ -84,3 +84,53 @@ export function getFileSink(path, options) {
84
84
  sink[Symbol.dispose] = () => options.closeSync(fd);
85
85
  return sink;
86
86
  }
87
+ /**
88
+ * Get a platform-independent rotating file sink.
89
+ *
90
+ * This sink writes log records to a file, and rotates the file when it reaches
91
+ * the `maxSize`. The rotated files are named with the original file name
92
+ * followed by a dot and a number, starting from 1. The number is incremented
93
+ * for each rotation, and the maximum number of files to keep is `maxFiles`.
94
+ *
95
+ * @param path A path to the file to write to.
96
+ * @param options The options for the sink and the file driver.
97
+ * @returns A sink that writes to the file. The sink is also a disposable
98
+ * object that closes the file when disposed.
99
+ */
100
+ export function getRotatingFileSink(path, options) {
101
+ const formatter = options.formatter ?? defaultTextFormatter;
102
+ const encoder = options.encoder ?? new TextEncoder();
103
+ const maxSize = options.maxSize ?? 1024 * 1024;
104
+ const maxFiles = options.maxFiles ?? 5;
105
+ let { size: offset } = options.statSync(path);
106
+ let fd = options.openSync(path);
107
+ function shouldRollover(bytes) {
108
+ return offset + bytes.length > maxSize;
109
+ }
110
+ function performRollover() {
111
+ options.closeSync(fd);
112
+ for (let i = maxFiles - 1; i > 0; i--) {
113
+ const oldPath = `${path}.${i}`;
114
+ const newPath = `${path}.${i + 1}`;
115
+ try {
116
+ options.renameSync(oldPath, newPath);
117
+ }
118
+ catch (_) {
119
+ // Continue if the file does not exist.
120
+ }
121
+ }
122
+ options.renameSync(path, `${path}.1`);
123
+ offset = 0;
124
+ fd = options.openSync(path);
125
+ }
126
+ const sink = (record) => {
127
+ const bytes = encoder.encode(formatter(record));
128
+ if (shouldRollover(bytes))
129
+ performRollover();
130
+ options.writeSync(fd, bytes);
131
+ options.flushSync(fd);
132
+ offset += bytes.length;
133
+ };
134
+ sink[Symbol.dispose] = () => options.closeSync(fd);
135
+ return sink;
136
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "0.2.0-dev.20+cb7e4d9d",
4
- "description": "Simple logging library for Deno/Node.js/Bun/browsers",
3
+ "version": "0.2.0",
4
+ "description": "Simple logging library with zero dependencies for Deno/Node.js/Bun/browsers",
5
5
  "keywords": [
6
6
  "logging",
7
7
  "log",
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.getFileSink = exports.nodeDriver = void 0;
29
+ exports.getRotatingFileSink = exports.getFileSink = exports.nodeDriver = void 0;
30
30
  const dntShim = __importStar(require("./_dnt.shims.js"));
31
31
  const node_fs_1 = __importDefault(require("node:fs"));
32
32
  const filesink_web_js_1 = require("./filesink.web.js");
@@ -41,6 +41,8 @@ exports.nodeDriver = {
41
41
  writeSync: node_fs_1.default.writeSync,
42
42
  flushSync: node_fs_1.default.fsyncSync,
43
43
  closeSync: node_fs_1.default.closeSync,
44
+ statSync: node_fs_1.default.statSync,
45
+ renameSync: node_fs_1.default.renameSync,
44
46
  };
45
47
  /**
46
48
  * Get a file sink.
@@ -59,4 +61,26 @@ function getFileSink(path, options = {}) {
59
61
  return (0, sink_js_1.getFileSink)(path, { ...options, ...exports.nodeDriver });
60
62
  }
61
63
  exports.getFileSink = getFileSink;
64
+ /**
65
+ * Get a rotating file sink.
66
+ *
67
+ * This sink writes log records to a file, and rotates the file when it reaches
68
+ * the `maxSize`. The rotated files are named with the original file name
69
+ * followed by a dot and a number, starting from 1. The number is incremented
70
+ * for each rotation, and the maximum number of files to keep is `maxFiles`.
71
+ *
72
+ * Note that this function is unavailable in the browser.
73
+ *
74
+ * @param path A path to the file to write to.
75
+ * @param options The options for the sink and the file driver.
76
+ * @returns A sink that writes to the file. The sink is also a disposable
77
+ * object that closes the file when disposed.
78
+ */
79
+ function getRotatingFileSink(path, options = {}) {
80
+ if ("document" in dntShim.dntGlobalThis) {
81
+ return (0, sink_js_1.getRotatingFileSink)(path, { ...options, ...filesink_web_js_1.webDriver });
82
+ }
83
+ return (0, sink_js_1.getRotatingFileSink)(path, { ...options, ...exports.nodeDriver });
84
+ }
85
+ exports.getRotatingFileSink = getRotatingFileSink;
62
86
  // cSpell: ignore filesink
@@ -12,4 +12,6 @@ exports.webDriver = {
12
12
  writeSync: notImplemented,
13
13
  flushSync: notImplemented,
14
14
  closeSync: notImplemented,
15
+ statSync: notImplemented,
16
+ renameSync: notImplemented,
15
17
  };
package/script/mod.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStreamSink = exports.getConsoleSink = exports.getLogger = exports.defaultTextFormatter = exports.defaultConsoleFormatter = exports.toFilter = exports.getLevelFilter = exports.getFileSink = exports.reset = exports.dispose = exports.configure = exports.ConfigError = void 0;
3
+ exports.getStreamSink = exports.getConsoleSink = exports.getLogger = exports.defaultTextFormatter = exports.defaultConsoleFormatter = exports.toFilter = exports.getLevelFilter = exports.getRotatingFileSink = exports.getFileSink = exports.reset = exports.dispose = exports.configure = exports.ConfigError = void 0;
4
4
  var config_js_1 = require("./config.js");
5
5
  Object.defineProperty(exports, "ConfigError", { enumerable: true, get: function () { return config_js_1.ConfigError; } });
6
6
  Object.defineProperty(exports, "configure", { enumerable: true, get: function () { return config_js_1.configure; } });
@@ -8,6 +8,7 @@ Object.defineProperty(exports, "dispose", { enumerable: true, get: function () {
8
8
  Object.defineProperty(exports, "reset", { enumerable: true, get: function () { return config_js_1.reset; } });
9
9
  var filesink_node_js_1 = require("./filesink.node.js");
10
10
  Object.defineProperty(exports, "getFileSink", { enumerable: true, get: function () { return filesink_node_js_1.getFileSink; } });
11
+ Object.defineProperty(exports, "getRotatingFileSink", { enumerable: true, get: function () { return filesink_node_js_1.getRotatingFileSink; } });
11
12
  var filter_js_1 = require("./filter.js");
12
13
  Object.defineProperty(exports, "getLevelFilter", { enumerable: true, get: function () { return filter_js_1.getLevelFilter; } });
13
14
  Object.defineProperty(exports, "toFilter", { enumerable: true, get: function () { return filter_js_1.toFilter; } });
package/script/sink.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFileSink = exports.getConsoleSink = exports.getStreamSink = void 0;
3
+ exports.getRotatingFileSink = 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}.
@@ -90,3 +90,54 @@ function getFileSink(path, options) {
90
90
  return sink;
91
91
  }
92
92
  exports.getFileSink = getFileSink;
93
+ /**
94
+ * Get a platform-independent rotating file sink.
95
+ *
96
+ * This sink writes log records to a file, and rotates the file when it reaches
97
+ * the `maxSize`. The rotated files are named with the original file name
98
+ * followed by a dot and a number, starting from 1. The number is incremented
99
+ * for each rotation, and the maximum number of files to keep is `maxFiles`.
100
+ *
101
+ * @param path A path to the file to write to.
102
+ * @param options The options for the sink and the file driver.
103
+ * @returns A sink that writes to the file. The sink is also a disposable
104
+ * object that closes the file when disposed.
105
+ */
106
+ function getRotatingFileSink(path, options) {
107
+ const formatter = options.formatter ?? formatter_js_1.defaultTextFormatter;
108
+ const encoder = options.encoder ?? new TextEncoder();
109
+ const maxSize = options.maxSize ?? 1024 * 1024;
110
+ const maxFiles = options.maxFiles ?? 5;
111
+ let { size: offset } = options.statSync(path);
112
+ let fd = options.openSync(path);
113
+ function shouldRollover(bytes) {
114
+ return offset + bytes.length > maxSize;
115
+ }
116
+ function performRollover() {
117
+ options.closeSync(fd);
118
+ for (let i = maxFiles - 1; i > 0; i--) {
119
+ const oldPath = `${path}.${i}`;
120
+ const newPath = `${path}.${i + 1}`;
121
+ try {
122
+ options.renameSync(oldPath, newPath);
123
+ }
124
+ catch (_) {
125
+ // Continue if the file does not exist.
126
+ }
127
+ }
128
+ options.renameSync(path, `${path}.1`);
129
+ offset = 0;
130
+ fd = options.openSync(path);
131
+ }
132
+ const sink = (record) => {
133
+ const bytes = encoder.encode(formatter(record));
134
+ if (shouldRollover(bytes))
135
+ performRollover();
136
+ options.writeSync(fd, bytes);
137
+ options.flushSync(fd);
138
+ offset += bytes.length;
139
+ };
140
+ sink[Symbol.dispose] = () => options.closeSync(fd);
141
+ return sink;
142
+ }
143
+ exports.getRotatingFileSink = getRotatingFileSink;
@@ -1,9 +1,9 @@
1
1
  /// <reference types="node" />
2
- import { type FileSinkDriver, type FileSinkOptions, type Sink } from "./sink.js";
2
+ import { type FileSinkOptions, type RotatingFileSinkDriver, type RotatingFileSinkOptions, type Sink } from "./sink.js";
3
3
  /**
4
4
  * A Node.js-specific file sink driver.
5
5
  */
6
- export declare const nodeDriver: FileSinkDriver<number>;
6
+ export declare const nodeDriver: RotatingFileSinkDriver<number>;
7
7
  /**
8
8
  * Get a file sink.
9
9
  *
@@ -15,4 +15,20 @@ export declare const nodeDriver: FileSinkDriver<number>;
15
15
  * object that closes the file when disposed.
16
16
  */
17
17
  export declare function getFileSink(path: string, options?: FileSinkOptions): Sink & Disposable;
18
+ /**
19
+ * Get a rotating file sink.
20
+ *
21
+ * This sink writes log records to a file, and rotates the file when it reaches
22
+ * the `maxSize`. The rotated files are named with the original file name
23
+ * followed by a dot and a number, starting from 1. The number is incremented
24
+ * for each rotation, and the maximum number of files to keep is `maxFiles`.
25
+ *
26
+ * Note that this function is unavailable in the browser.
27
+ *
28
+ * @param path A path to the file to write to.
29
+ * @param options The options for the sink and the file driver.
30
+ * @returns A sink that writes to the file. The sink is also a disposable
31
+ * object that closes the file when disposed.
32
+ */
33
+ export declare function getRotatingFileSink(path: string, options?: RotatingFileSinkOptions): Sink & Disposable;
18
34
  //# sourceMappingURL=filesink.node.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"filesink.node.d.ts","sourceRoot":"","sources":["../src/filesink.node.ts"],"names":[],"mappings":";AAGA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,eAAe,EAEpB,KAAK,IAAI,EACV,MAAM,WAAW,CAAC;AAEnB;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,cAAc,CAAC,MAAM,CAO7C,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,eAAoB,GAC5B,IAAI,GAAG,UAAU,CAKnB"}
1
+ {"version":3,"file":"filesink.node.d.ts","sourceRoot":"","sources":["../src/filesink.node.ts"],"names":[],"mappings":";AAGA,OAAO,EACL,KAAK,eAAe,EAGpB,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,EAC5B,KAAK,IAAI,EACV,MAAM,WAAW,CAAC;AAEnB;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,sBAAsB,CAAC,MAAM,CASrD,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,eAAoB,GAC5B,IAAI,GAAG,UAAU,CAKnB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,uBAA4B,GACpC,IAAI,GAAG,UAAU,CAKnB"}
@@ -1,6 +1,6 @@
1
- import type { FileSinkDriver } from "./sink.js";
1
+ import type { RotatingFileSinkDriver } from "./sink.js";
2
2
  /**
3
3
  * A browser-specific file sink driver. All methods throw an error.
4
4
  */
5
- export declare const webDriver: FileSinkDriver<void>;
5
+ export declare const webDriver: RotatingFileSinkDriver<void>;
6
6
  //# sourceMappingURL=filesink.web.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"filesink.web.d.ts","sourceRoot":"","sources":["../src/filesink.web.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAMhD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,cAAc,CAAC,IAAI,CAK1C,CAAC"}
1
+ {"version":3,"file":"filesink.web.d.ts","sourceRoot":"","sources":["../src/filesink.web.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAMxD;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,sBAAsB,CAAC,IAAI,CAOlD,CAAC"}
package/types/mod.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { type Config, ConfigError, configure, dispose, type LoggerConfig, reset, } from "./config.js";
2
- export { getFileSink } from "./filesink.node.js";
2
+ export { getFileSink, getRotatingFileSink } from "./filesink.node.js";
3
3
  export { type Filter, type FilterLike, getLevelFilter, toFilter, } from "./filter.js";
4
4
  export { type ConsoleFormatter, defaultConsoleFormatter, defaultTextFormatter, type TextFormatter, } from "./formatter.js";
5
5
  export { getLogger, type Logger } from "./logger.js";
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,WAAW,EACX,SAAS,EACT,OAAO,EACP,KAAK,YAAY,EACjB,KAAK,GACN,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EACL,KAAK,MAAM,EACX,KAAK,UAAU,EACf,cAAc,EACd,QAAQ,GACT,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,KAAK,gBAAgB,EACrB,uBAAuB,EACvB,oBAAoB,EACpB,KAAK,aAAa,GACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AACrD,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,cAAc,EACd,aAAa,EACb,KAAK,IAAI,EACT,KAAK,iBAAiB,GACvB,MAAM,WAAW,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,WAAW,EACX,SAAS,EACT,OAAO,EACP,KAAK,YAAY,EACjB,KAAK,GACN,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EACL,KAAK,MAAM,EACX,KAAK,UAAU,EACf,cAAc,EACd,QAAQ,GACT,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,KAAK,gBAAgB,EACrB,uBAAuB,EACvB,oBAAoB,EACpB,KAAK,aAAa,GACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AACrD,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,cAAc,EACd,aAAa,EACb,KAAK,IAAI,EACT,KAAK,iBAAiB,GACvB,MAAM,WAAW,CAAC"}
package/types/sink.d.ts CHANGED
@@ -115,4 +115,50 @@ export interface FileSinkDriver<TFile> {
115
115
  * object that closes the file when disposed.
116
116
  */
117
117
  export declare function getFileSink<TFile>(path: string, options: FileSinkOptions & FileSinkDriver<TFile>): Sink & Disposable;
118
+ /**
119
+ * Options for the {@link getRotatingFileSink} function.
120
+ */
121
+ export interface RotatingFileSinkOptions extends FileSinkOptions {
122
+ /**
123
+ * The maximum bytes of the file before it is rotated. 1 MiB by default.
124
+ */
125
+ maxSize?: number;
126
+ /**
127
+ * The maximum number of files to keep. 5 by default.
128
+ */
129
+ maxFiles?: number;
130
+ }
131
+ /**
132
+ * A platform-specific rotating file sink driver.
133
+ */
134
+ export interface RotatingFileSinkDriver<TFile> extends FileSinkDriver<TFile> {
135
+ /**
136
+ * Get the size of the file.
137
+ * @param path A path to the file.
138
+ * @returns The `size` of the file in bytes, in an object.
139
+ */
140
+ statSync(path: string): {
141
+ size: number;
142
+ };
143
+ /**
144
+ * Rename a file.
145
+ * @param oldPath A path to the file to rename.
146
+ * @param newPath A path to be renamed to.
147
+ */
148
+ renameSync(oldPath: string, newPath: string): void;
149
+ }
150
+ /**
151
+ * Get a platform-independent rotating file sink.
152
+ *
153
+ * This sink writes log records to a file, and rotates the file when it reaches
154
+ * the `maxSize`. The rotated files are named with the original file name
155
+ * followed by a dot and a number, starting from 1. The number is incremented
156
+ * for each rotation, and the maximum number of files to keep is `maxFiles`.
157
+ *
158
+ * @param path A path to the file to write to.
159
+ * @param options The options for the sink and the file driver.
160
+ * @returns A sink that writes to the file. The sink is also a disposable
161
+ * object that closes the file when disposed.
162
+ */
163
+ export declare function getRotatingFileSink<TFile>(path: string, options: RotatingFileSinkOptions & RotatingFileSinkDriver<TFile>): Sink & Disposable;
118
164
  //# 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;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