@logtape/file 2.2.1-dev.777 → 2.2.2
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/dist/dist/filesink.base.d.cts +74 -0
- package/dist/dist/filesink.base.d.cts.map +1 -0
- package/dist/dist/filesink.node.d.cts +65 -0
- package/dist/dist/filesink.node.d.cts.map +1 -0
- package/dist/dist/timefilesink.d.cts +44 -0
- package/dist/dist/timefilesink.d.cts.map +1 -0
- package/dist/filesink.deno.cjs +15 -5
- package/dist/filesink.deno.js +15 -5
- package/dist/filesink.deno.js.map +1 -1
- package/dist/filesink.node.cjs +15 -5
- package/dist/filesink.node.js +15 -5
- package/dist/filesink.node.js.map +1 -1
- package/dist/mod.d.cts +1 -1
- package/dist/timefilesink.cjs +33 -12
- package/dist/timefilesink.d.cts +3 -0
- package/dist/timefilesink.d.cts.map +1 -1
- package/dist/timefilesink.d.ts +3 -0
- package/dist/timefilesink.d.ts.map +1 -1
- package/dist/timefilesink.js +33 -12
- package/dist/timefilesink.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { StreamSinkOptions } from "@logtape/logtape";
|
|
2
|
+
|
|
3
|
+
//#region dist/filesink.base.d.ts
|
|
4
|
+
|
|
5
|
+
//#region src/filesink.base.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Options for the {@link getBaseFileSink} function.
|
|
8
|
+
*/
|
|
9
|
+
interface FileSinkOptions extends StreamSinkOptions {
|
|
10
|
+
/**
|
|
11
|
+
* If `true`, the file is not opened until the first write. Defaults to `false`.
|
|
12
|
+
*/
|
|
13
|
+
lazy?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* The size of the buffer to use when writing to the file. If not specified,
|
|
16
|
+
* a default buffer size will be used. If it is less or equal to 0,
|
|
17
|
+
* the file will be written directly without buffering.
|
|
18
|
+
* @default 8192
|
|
19
|
+
* @since 0.12.0
|
|
20
|
+
*/
|
|
21
|
+
bufferSize?: number;
|
|
22
|
+
/**
|
|
23
|
+
* The maximum time interval in milliseconds between flushes. If this time
|
|
24
|
+
* passes since the last flush, the buffer will be flushed regardless of size.
|
|
25
|
+
* This helps prevent log loss during unexpected process termination.
|
|
26
|
+
* @default 5000
|
|
27
|
+
* @since 0.12.0
|
|
28
|
+
*/
|
|
29
|
+
flushInterval?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Enable non-blocking mode with background flushing.
|
|
32
|
+
* When enabled, flush operations are performed asynchronously to prevent
|
|
33
|
+
* blocking the main thread during file I/O operations.
|
|
34
|
+
*
|
|
35
|
+
* @default `false`
|
|
36
|
+
* @since 1.0.0
|
|
37
|
+
*/
|
|
38
|
+
nonBlocking?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A platform-specific file sink driver.
|
|
42
|
+
* @template TFile The type of the file descriptor.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get a platform-independent file sink.
|
|
47
|
+
*
|
|
48
|
+
* @template TFile The type of the file descriptor.
|
|
49
|
+
* @param path A path to the file to write to.
|
|
50
|
+
* @param options The options for the sink and the file driver.
|
|
51
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
52
|
+
* object that closes the file when disposed. If `nonBlocking` is enabled,
|
|
53
|
+
* returns a sink that also implements {@link AsyncDisposable}.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Options for the {@link getBaseRotatingFileSink} function.
|
|
58
|
+
*/
|
|
59
|
+
interface RotatingFileSinkOptions extends Omit<FileSinkOptions, "lazy"> {
|
|
60
|
+
/**
|
|
61
|
+
* The maximum bytes of the file before it is rotated. 1 MiB by default.
|
|
62
|
+
*/
|
|
63
|
+
maxSize?: number;
|
|
64
|
+
/**
|
|
65
|
+
* The maximum number of files to keep. 5 by default.
|
|
66
|
+
*/
|
|
67
|
+
maxFiles?: number;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A platform-specific rotating file sink driver.
|
|
71
|
+
*/
|
|
72
|
+
//#endregion
|
|
73
|
+
export { FileSinkOptions, RotatingFileSinkOptions };
|
|
74
|
+
//# sourceMappingURL=filesink.base.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesink.base.d.cts","names":["Sink","StreamSinkOptions","FileSinkOptions","FileSinkDriver","TFile","Uint8Array","AsyncFileSinkDriver","Promise","RotatingFileSinkOptions","Omit","RotatingFileSinkDriver","AsyncRotatingFileSinkDriver"],"sources":["../filesink.base.d.ts"],"sourcesContent":["import { Sink, StreamSinkOptions } from \"@logtape/logtape\";\n\n//#region src/filesink.base.d.ts\n\n/**\n * Options for the {@link getBaseFileSink} function.\n */\ninterface FileSinkOptions extends StreamSinkOptions {\n /**\n * If `true`, the file is not opened until the first write. Defaults to `false`.\n */\n lazy?: boolean;\n /**\n * The size of the buffer to use when writing to the file. If not specified,\n * a default buffer size will be used. If it is less or equal to 0,\n * the file will be written directly without buffering.\n * @default 8192\n * @since 0.12.0\n */\n bufferSize?: number;\n /**\n * The maximum time interval in milliseconds between flushes. If this time\n * passes since the last flush, the buffer will be flushed regardless of size.\n * This helps prevent log loss during unexpected process termination.\n * @default 5000\n * @since 0.12.0\n */\n flushInterval?: number;\n /**\n * Enable non-blocking mode with background flushing.\n * When enabled, flush operations are performed asynchronously to prevent\n * blocking the main thread during file I/O operations.\n *\n * @default `false`\n * @since 1.0.0\n */\n nonBlocking?: boolean;\n}\n/**\n * A platform-specific file sink driver.\n * @template TFile The type of the file descriptor.\n */\ninterface FileSinkDriver<TFile> {\n /**\n * Open a file for appending and return a file descriptor.\n * @param path A path to the file to open.\n */\n openSync(path: string): TFile;\n /**\n * Write a chunk of data to the file.\n * @param fd The file descriptor.\n * @param chunk The data to write.\n */\n writeSync(fd: TFile, chunk: Uint8Array): void;\n /**\n * Write multiple chunks of data to the file in a single operation.\n * This is optional - if not implemented, falls back to multiple writeSync calls.\n * @param fd The file descriptor.\n * @param chunks Array of data chunks to write.\n */\n writeManySync?(fd: TFile, chunks: Uint8Array[]): void;\n /**\n * Flush the file to ensure that all data is written to the disk.\n * @param fd The file descriptor.\n */\n flushSync(fd: TFile): void;\n /**\n * Close the file.\n * @param fd The file descriptor.\n */\n closeSync(fd: TFile): void;\n}\n/**\n * A platform-specific async file sink driver.\n * @template TFile The type of the file descriptor.\n * @since 1.0.0\n */\ninterface AsyncFileSinkDriver<TFile> extends FileSinkDriver<TFile> {\n /**\n * Asynchronously write multiple chunks of data to the file in a single operation.\n * This is optional - if not implemented, falls back to multiple writeSync calls.\n * @param fd The file descriptor.\n * @param chunks Array of data chunks to write.\n */\n writeMany?(fd: TFile, chunks: Uint8Array[]): Promise<void>;\n /**\n * Asynchronously flush the file to ensure that all data is written to the disk.\n * @param fd The file descriptor.\n */\n flush(fd: TFile): Promise<void>;\n /**\n * Asynchronously close the file.\n * @param fd The file descriptor.\n */\n close(fd: TFile): Promise<void>;\n}\n/**\n * Get a platform-independent file sink.\n *\n * @template TFile The type of the file descriptor.\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\n\n/**\n * Options for the {@link getBaseRotatingFileSink} function.\n */\ninterface RotatingFileSinkOptions extends Omit<FileSinkOptions, \"lazy\"> {\n /**\n * The maximum bytes of the file before it is rotated. 1 MiB by default.\n */\n maxSize?: number;\n /**\n * The maximum number of files to keep. 5 by default.\n */\n maxFiles?: number;\n}\n/**\n * A platform-specific rotating file sink driver.\n */\ninterface RotatingFileSinkDriver<TFile> extends FileSinkDriver<TFile> {\n /**\n * Get the size of the file.\n * @param path A path to the file.\n * @returns The `size` of the file in bytes, in an object.\n */\n statSync(path: string): {\n size: number;\n };\n /**\n * Rename a file.\n * @param oldPath A path to the file to rename.\n * @param newPath A path to be renamed to.\n */\n renameSync(oldPath: string, newPath: string): void;\n /**\n * Delete a file.\n * @param path A path to the file to delete.\n */\n unlinkSync?(path: string): void;\n}\n/**\n * A platform-specific async rotating file sink driver.\n * @since 1.0.0\n */\ninterface AsyncRotatingFileSinkDriver<TFile> extends AsyncFileSinkDriver<TFile> {\n /**\n * Get the size of the file.\n * @param path A path to the file.\n * @returns The `size` of the file in bytes, in an object.\n */\n statSync(path: string): {\n size: number;\n };\n /**\n * Rename a file.\n * @param oldPath A path to the file to rename.\n * @param newPath A path to be renamed to.\n */\n renameSync(oldPath: string, newPath: string): void;\n /**\n * Delete a file.\n * @param path A path to the file to delete.\n */\n unlinkSync?(path: string): void;\n}\n/**\n * Get a platform-independent rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\n//#endregion\nexport { AsyncFileSinkDriver, AsyncRotatingFileSinkDriver, FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions };\n//# sourceMappingURL=filesink.base.d.ts.map"],"mappings":";;;;;AA6E2D;;;UAtEjDE,eAAAA,SAAwBD,iBAuGQQ,CAAAA;EAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAApCD,uBAAAA,SAAgCC,KAAKP"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { FileSinkOptions, RotatingFileSinkOptions } from "./filesink.base.cjs";
|
|
2
|
+
import { TimeRotatingFileSinkOptions } from "./timefilesink.cjs";
|
|
3
|
+
import { Sink } from "@logtape/logtape";
|
|
4
|
+
|
|
5
|
+
//#region dist/filesink.node.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get a file sink.
|
|
9
|
+
*
|
|
10
|
+
* Note that this function is unavailable in the browser.
|
|
11
|
+
*
|
|
12
|
+
* @param path A path to the file to write to.
|
|
13
|
+
* @param options The options for the sink.
|
|
14
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
15
|
+
* object that closes the file when disposed. If `nonBlocking` is enabled,
|
|
16
|
+
* returns a sink that also implements {@link AsyncDisposable}.
|
|
17
|
+
*/
|
|
18
|
+
declare function getFileSink(path: string, options?: FileSinkOptions): Sink & Disposable;
|
|
19
|
+
declare function getFileSink(path: string, options: FileSinkOptions & {
|
|
20
|
+
nonBlocking: true;
|
|
21
|
+
}): Sink & AsyncDisposable;
|
|
22
|
+
/**
|
|
23
|
+
* Get a rotating file sink.
|
|
24
|
+
*
|
|
25
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
26
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
27
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
28
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
29
|
+
*
|
|
30
|
+
* Note that this function is unavailable in the browser.
|
|
31
|
+
*
|
|
32
|
+
* @param path A path to the file to write to.
|
|
33
|
+
* @param options The options for the sink and the file driver.
|
|
34
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
35
|
+
* object that closes the file when disposed. If `nonBlocking` is enabled,
|
|
36
|
+
* returns a sink that also implements {@link AsyncDisposable}.
|
|
37
|
+
*/
|
|
38
|
+
declare function getRotatingFileSink(path: string, options?: RotatingFileSinkOptions): Sink & Disposable;
|
|
39
|
+
declare function getRotatingFileSink(path: string, options: RotatingFileSinkOptions & {
|
|
40
|
+
nonBlocking: true;
|
|
41
|
+
}): Sink & AsyncDisposable;
|
|
42
|
+
/**
|
|
43
|
+
* Get a time-rotating file sink.
|
|
44
|
+
*
|
|
45
|
+
* This sink writes log records to a file in a directory, rotating to a new
|
|
46
|
+
* file based on time intervals. The filename is generated based on the
|
|
47
|
+
* current date/time and the configured interval.
|
|
48
|
+
*
|
|
49
|
+
* Note that this function is unavailable in the browser.
|
|
50
|
+
*
|
|
51
|
+
* @param options The options for the sink.
|
|
52
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
53
|
+
* object that closes the file when disposed. If `nonBlocking` is
|
|
54
|
+
* enabled, returns a sink that also implements {@link AsyncDisposable}.
|
|
55
|
+
* @since 2.0.0
|
|
56
|
+
*/
|
|
57
|
+
declare function getTimeRotatingFileSink(options: TimeRotatingFileSinkOptions): Sink & Disposable;
|
|
58
|
+
declare function getTimeRotatingFileSink(options: TimeRotatingFileSinkOptions & {
|
|
59
|
+
nonBlocking: true;
|
|
60
|
+
}): Sink & AsyncDisposable;
|
|
61
|
+
//# sourceMappingURL=filesink.node.d.ts.map
|
|
62
|
+
//#endregion
|
|
63
|
+
//#endregion
|
|
64
|
+
export { getFileSink, getRotatingFileSink, getTimeRotatingFileSink };
|
|
65
|
+
//# sourceMappingURL=filesink.node.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesink.node.d.cts","names":["AsyncRotatingFileSinkDriver","FileSinkOptions","RotatingFileSinkDriver","RotatingFileSinkOptions","AsyncTimeRotatingFileSinkDriver","TimeRotatingFileSinkDriver","TimeRotatingFileSinkOptions","Sink","nodeDriver","nodeAsyncDriver","nodeTimeDriver","nodeAsyncTimeDriver","getFileSink","Disposable","AsyncDisposable","getRotatingFileSink","getTimeRotatingFileSink"],"sources":["../filesink.node.d.ts"],"sourcesContent":["import { AsyncRotatingFileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions } from \"./filesink.base.js\";\nimport { AsyncTimeRotatingFileSinkDriver, TimeRotatingFileSinkDriver, TimeRotatingFileSinkOptions } from \"./timefilesink.js\";\nimport { Sink } from \"@logtape/logtape\";\n\n//#region src/filesink.node.d.ts\n\n/**\n * A Node.js-specific file sink driver.\n */\ndeclare const nodeDriver: RotatingFileSinkDriver<number | void>;\n/**\n * A Node.js-specific async file sink driver.\n * @since 1.0.0\n */\ndeclare const nodeAsyncDriver: AsyncRotatingFileSinkDriver<number | void>;\n/**\n * A Node.js-specific time-rotating file sink driver.\n * @since 2.0.0\n */\ndeclare const nodeTimeDriver: TimeRotatingFileSinkDriver<number | void>;\n/**\n * A Node.js-specific async time-rotating file sink driver.\n * @since 2.0.0\n */\ndeclare const nodeAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<number | void>;\n/**\n * Get a file sink.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\ndeclare function getFileSink(path: string, options?: FileSinkOptions): Sink & Disposable;\ndeclare function getFileSink(path: string, options: FileSinkOptions & {\n nonBlocking: true;\n}): Sink & AsyncDisposable;\n/**\n * Get a rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\ndeclare function getRotatingFileSink(path: string, options?: RotatingFileSinkOptions): Sink & Disposable;\ndeclare function getRotatingFileSink(path: string, options: RotatingFileSinkOptions & {\n nonBlocking: true;\n}): Sink & AsyncDisposable;\n/**\n * Get a time-rotating file sink.\n *\n * This sink writes log records to a file in a directory, rotating to a new\n * file based on time intervals. The filename is generated based on the\n * current date/time and the configured interval.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is\n * enabled, returns a sink that also implements {@link AsyncDisposable}.\n * @since 2.0.0\n */\ndeclare function getTimeRotatingFileSink(options: TimeRotatingFileSinkOptions): Sink & Disposable;\ndeclare function getTimeRotatingFileSink(options: TimeRotatingFileSinkOptions & {\n nonBlocking: true;\n}): Sink & AsyncDisposable;\n//# sourceMappingURL=filesink.node.d.ts.map\n//#endregion\nexport { getFileSink, getRotatingFileSink, getTimeRotatingFileSink, nodeAsyncDriver, nodeAsyncTimeDriver, nodeDriver, nodeTimeDriver };\n//# sourceMappingURL=filesink.node.d.ts.map"],"mappings":";;;;;;;;;AA2D0B;AAAA;;;;;AAgBuE;AAAA;iBAvChFY,WAAAA,CAwCuB,IAAA,EAAA,MAAA,EAAA,OAAA,CAAA,EAxCaX,eAwCb,CAAA,EAxC+BM,IAwC/B,GAxCsCM,UAwCtC;iBAvCvBD,WAAAA,CAuCiCN,IAAAA,EAAAA,MAAAA,EAAAA,OAAAA,EAvCEL,eAuCFK,GAAAA;EAA2B,WAEzEC,EAAAA,IAAAA;CAAI,CAAA,EAvCJA,IAuCOO,GAvCAA,eAuCAA;AAAe;;;;;;;;;;;;;;;;iBAtBTC,mBAAAA,yBAA4CZ,0BAA0BI,OAAOM;iBAC7EE,mBAAAA,wBAA2CZ;;IAExDI,OAAOO;;;;;;;;;;;;;;;;iBAgBME,uBAAAA,UAAiCV,8BAA8BC,OAAOM;iBACtEG,uBAAAA,UAAiCV;;IAE9CC,OAAOO"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { FileSinkOptions } from "./filesink.base.cjs";
|
|
2
|
+
|
|
3
|
+
//#region dist/timefilesink.d.ts
|
|
4
|
+
|
|
5
|
+
//#region src/timefilesink.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* The rotation interval for time-based file sinks.
|
|
8
|
+
*/
|
|
9
|
+
type TimeRotationInterval = "hourly" | "daily" | "weekly";
|
|
10
|
+
/**
|
|
11
|
+
* Options for the {@link getBaseTimeRotatingFileSink} function.
|
|
12
|
+
*/
|
|
13
|
+
interface TimeRotatingFileSinkOptions extends Omit<FileSinkOptions, "lazy"> {
|
|
14
|
+
/**
|
|
15
|
+
* The directory to write log files to.
|
|
16
|
+
*/
|
|
17
|
+
directory: string;
|
|
18
|
+
/**
|
|
19
|
+
* A function that generates the filename for the log file based on the date.
|
|
20
|
+
* Default depends on `interval`:
|
|
21
|
+
* - `"daily"`: `YYYY-MM-DD.log` (e.g., `2025-01-15.log`)
|
|
22
|
+
* - `"hourly"`: `YYYY-MM-DD-HH.log` (e.g., `2025-01-15-09.log`)
|
|
23
|
+
* - `"weekly"`: `YYYY-WW.log` (e.g., `2025-W03.log`)
|
|
24
|
+
*/
|
|
25
|
+
filename?: (date: Date) => string;
|
|
26
|
+
/**
|
|
27
|
+
* The rotation interval. Defaults to `"daily"`.
|
|
28
|
+
*/
|
|
29
|
+
interval?: TimeRotationInterval;
|
|
30
|
+
/**
|
|
31
|
+
* The maximum age of log files in milliseconds. Files older than this
|
|
32
|
+
* will be deleted. If not specified, old files are not deleted.
|
|
33
|
+
*
|
|
34
|
+
* When using `getTimeRotatingFileSink()` with `filename` set, cleanup uses
|
|
35
|
+
* each file's modification time instead of parsing dates from filenames.
|
|
36
|
+
*/
|
|
37
|
+
maxAgeMs?: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A platform-specific time-rotating file sink driver.
|
|
41
|
+
*/
|
|
42
|
+
//#endregion
|
|
43
|
+
export { TimeRotatingFileSinkOptions };
|
|
44
|
+
//# sourceMappingURL=timefilesink.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timefilesink.d.cts","names":["AsyncFileSinkDriver","FileSinkDriver","FileSinkOptions","Sink","TimeRotationInterval","TimeRotatingFileSinkOptions","Date","Omit","TimeRotatingFileSinkDriver","TFile","AsyncTimeRotatingFileSinkDriver"],"sources":["../timefilesink.d.ts"],"sourcesContent":["import { AsyncFileSinkDriver, FileSinkDriver, FileSinkOptions } from \"./filesink.base.js\";\nimport { Sink } from \"@logtape/logtape\";\n\n//#region src/timefilesink.d.ts\n\n/**\n * The rotation interval for time-based file sinks.\n */\ntype TimeRotationInterval = \"hourly\" | \"daily\" | \"weekly\";\n/**\n * Options for the {@link getBaseTimeRotatingFileSink} function.\n */\ninterface TimeRotatingFileSinkOptions extends Omit<FileSinkOptions, \"lazy\"> {\n /**\n * The directory to write log files to.\n */\n directory: string;\n /**\n * A function that generates the filename for the log file based on the date.\n * Default depends on `interval`:\n * - `\"daily\"`: `YYYY-MM-DD.log` (e.g., `2025-01-15.log`)\n * - `\"hourly\"`: `YYYY-MM-DD-HH.log` (e.g., `2025-01-15-09.log`)\n * - `\"weekly\"`: `YYYY-WW.log` (e.g., `2025-W03.log`)\n */\n filename?: (date: Date) => string;\n /**\n * The rotation interval. Defaults to `\"daily\"`.\n */\n interval?: TimeRotationInterval;\n /**\n * The maximum age of log files in milliseconds. Files older than this\n * will be deleted. If not specified, old files are not deleted.\n *\n * When using `getTimeRotatingFileSink()` with `filename` set, cleanup uses\n * each file's modification time instead of parsing dates from filenames.\n */\n maxAgeMs?: number;\n}\n/**\n * A platform-specific time-rotating file sink driver.\n */\ninterface TimeRotatingFileSinkDriver<TFile> extends FileSinkDriver<TFile> {\n /**\n * Read the contents of a directory.\n * @param path A path to the directory.\n * @returns An array of filenames in the directory.\n */\n readdirSync(path: string): string[];\n /**\n * Delete a file.\n * @param path A path to the file to delete.\n */\n unlinkSync(path: string): void;\n /**\n * Create a directory if it doesn't exist.\n * @param path A path to the directory to create.\n * @param options Options for directory creation.\n */\n mkdirSync(path: string, options?: {\n recursive?: boolean;\n }): void;\n /**\n * Join path segments.\n * @param paths Path segments to join.\n * @returns The joined path.\n */\n joinPath(...paths: string[]): string;\n}\n/**\n * A platform-specific async time-rotating file sink driver.\n * @since 2.0.0\n */\ninterface AsyncTimeRotatingFileSinkDriver<TFile> extends AsyncFileSinkDriver<TFile> {\n /**\n * Read the contents of a directory.\n * @param path A path to the directory.\n * @returns An array of filenames in the directory.\n */\n readdirSync(path: string): string[];\n /**\n * Delete a file.\n * @param path A path to the file to delete.\n */\n unlinkSync(path: string): void;\n /**\n * Create a directory if it doesn't exist.\n * @param path A path to the directory to create.\n * @param options Options for directory creation.\n */\n mkdirSync(path: string, options?: {\n recursive?: boolean;\n }): void;\n /**\n * Join path segments.\n * @param paths Path segments to join.\n * @returns The joined path.\n */\n joinPath(...paths: string[]): string;\n}\n/**\n * Get the ISO week number of a date.\n * @param date The date to get the week number of.\n * @returns The ISO week number (1-53).\n */\n//#endregion\nexport { AsyncTimeRotatingFileSinkDriver, TimeRotatingFileSinkDriver, TimeRotatingFileSinkOptions, TimeRotationInterval };\n//# sourceMappingURL=timefilesink.d.ts.map"],"mappings":";;;;AACwC;;;;KAOnCI,oBAAAA,GAoBQA,QAAAA,GAAAA,OAAAA,GAAAA,QAAAA;;AAhBqC;;UAAxCC,2BAAAA,SAAoCE,KAAKL;;;;;;;;;;;;oBAY/BI;;;;aAIPF"}
|
package/dist/filesink.deno.cjs
CHANGED
|
@@ -96,13 +96,23 @@ function getRotatingFileSink(path, options = {}) {
|
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
98
|
function getTimeRotatingFileSink(options) {
|
|
99
|
-
if (options.nonBlocking)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
if (options.nonBlocking) {
|
|
100
|
+
const driver$1 = {
|
|
101
|
+
...denoAsyncTimeDriver,
|
|
102
|
+
statSync: globalThis?.Deno.statSync
|
|
103
|
+
};
|
|
104
|
+
return require_timefilesink.getBaseTimeRotatingFileSink({
|
|
105
|
+
...options,
|
|
106
|
+
...driver$1
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
const driver = {
|
|
110
|
+
...denoTimeDriver,
|
|
111
|
+
statSync: globalThis?.Deno.statSync
|
|
112
|
+
};
|
|
103
113
|
return require_timefilesink.getBaseTimeRotatingFileSink({
|
|
104
114
|
...options,
|
|
105
|
-
...
|
|
115
|
+
...driver
|
|
106
116
|
});
|
|
107
117
|
}
|
|
108
118
|
|
package/dist/filesink.deno.js
CHANGED
|
@@ -96,13 +96,23 @@ function getRotatingFileSink(path, options = {}) {
|
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
98
|
function getTimeRotatingFileSink(options) {
|
|
99
|
-
if (options.nonBlocking)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
if (options.nonBlocking) {
|
|
100
|
+
const driver$1 = {
|
|
101
|
+
...denoAsyncTimeDriver,
|
|
102
|
+
statSync: globalThis?.Deno.statSync
|
|
103
|
+
};
|
|
104
|
+
return getBaseTimeRotatingFileSink({
|
|
105
|
+
...options,
|
|
106
|
+
...driver$1
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
const driver = {
|
|
110
|
+
...denoTimeDriver,
|
|
111
|
+
statSync: globalThis?.Deno.statSync
|
|
112
|
+
};
|
|
103
113
|
return getBaseTimeRotatingFileSink({
|
|
104
114
|
...options,
|
|
105
|
-
...
|
|
115
|
+
...driver
|
|
106
116
|
});
|
|
107
117
|
}
|
|
108
118
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filesink.deno.js","names":["denoDriver: RotatingFileSinkDriver<Deno.FsFile>","path: string","fd: Deno.FsFile","chunks: Uint8Array[]","denoAsyncDriver: AsyncRotatingFileSinkDriver<Deno.FsFile>","denoTimeDriver: TimeRotatingFileSinkDriver<Deno.FsFile>","options?: { recursive?: boolean }","denoAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<Deno.FsFile>","options: FileSinkOptions","options: RotatingFileSinkOptions","options: TimeRotatingFileSinkOptions"],"sources":["../src/filesink.deno.ts"],"sourcesContent":["import type { Sink } from \"@logtape/logtape\";\nimport { join } from \"@std/path/join\";\nimport {\n type AsyncRotatingFileSinkDriver,\n type FileSinkOptions,\n getBaseFileSink,\n getBaseRotatingFileSink,\n type RotatingFileSinkDriver,\n type RotatingFileSinkOptions,\n} from \"./filesink.base.ts\";\nimport {\n type AsyncTimeRotatingFileSinkDriver,\n getBaseTimeRotatingFileSink,\n type TimeRotatingFileSinkDriver,\n type TimeRotatingFileSinkOptions,\n} from \"./timefilesink.ts\";\n\n/**\n * A Deno-specific file sink driver.\n */\nexport const denoDriver: RotatingFileSinkDriver<Deno.FsFile> = {\n openSync(path: string) {\n return Deno.openSync(path, { create: true, append: true });\n },\n writeSync(fd, chunk) {\n fd.writeSync(chunk);\n },\n writeManySync(fd: Deno.FsFile, chunks: Uint8Array[]): void {\n // Deno doesn't have writev, but we can optimize by writing all chunks\n // then doing a single sync operation\n for (const chunk of chunks) {\n fd.writeSync(chunk);\n }\n },\n flushSync(fd) {\n fd.syncSync();\n },\n closeSync(fd) {\n fd.close();\n },\n statSync: globalThis?.Deno.statSync,\n renameSync: globalThis?.Deno.renameSync,\n unlinkSync: globalThis?.Deno.removeSync,\n};\n\n/**\n * A Deno-specific async file sink driver.\n * @since 1.0.0\n */\nexport const denoAsyncDriver: AsyncRotatingFileSinkDriver<Deno.FsFile> = {\n ...denoDriver,\n async writeMany(fd: Deno.FsFile, chunks: Uint8Array[]): Promise<void> {\n // Deno doesn't have async writev, but we can write all chunks\n // then do a single async sync\n for (const chunk of chunks) {\n await fd.write(chunk);\n }\n },\n async flush(fd) {\n await fd.sync();\n },\n close(fd) {\n return Promise.resolve(fd.close());\n },\n};\n\n/**\n * A Deno-specific time-rotating file sink driver.\n * @since 2.0.0\n */\nexport const denoTimeDriver: TimeRotatingFileSinkDriver<Deno.FsFile> = {\n ...denoDriver,\n readdirSync(path: string) {\n return [...Deno.readDirSync(path)].map((entry) => entry.name);\n },\n unlinkSync: globalThis?.Deno.removeSync,\n mkdirSync(path: string, options?: { recursive?: boolean }) {\n Deno.mkdirSync(path, options);\n },\n joinPath: join,\n};\n\n/**\n * A Deno-specific async time-rotating file sink driver.\n * @since 2.0.0\n */\nexport const denoAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<Deno.FsFile> =\n {\n ...denoAsyncDriver,\n readdirSync(path: string) {\n return [...Deno.readDirSync(path)].map((entry) => entry.name);\n },\n unlinkSync: globalThis?.Deno.removeSync,\n mkdirSync(path: string, options?: { recursive?: boolean }) {\n Deno.mkdirSync(path, options);\n },\n joinPath: join,\n };\n\n/**\n * Get a file sink.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getFileSink(\n path: string,\n options?: FileSinkOptions,\n): Sink & Disposable;\nexport function getFileSink(\n path: string,\n options: FileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getFileSink(\n path: string,\n options: FileSinkOptions = {},\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseFileSink(path, { ...options, ...denoAsyncDriver });\n }\n return getBaseFileSink(path, { ...options, ...denoDriver });\n}\n\n/**\n * Get a rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getRotatingFileSink(\n path: string,\n options?: RotatingFileSinkOptions,\n): Sink & Disposable;\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions = {},\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseRotatingFileSink(path, { ...options, ...denoAsyncDriver });\n }\n return getBaseRotatingFileSink(path, { ...options, ...denoDriver });\n}\n\n/**\n * Get a time-rotating file sink.\n *\n * This sink writes log records to a file in a directory, rotating to a new\n * file based on time intervals. The filename is generated based on the\n * current date/time and the configured interval.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is\n * enabled, returns a sink that also implements {@link AsyncDisposable}.\n * @since 2.0.0\n */\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions,\n): Sink & Disposable;\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions,\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseTimeRotatingFileSink({ ...options, ...
|
|
1
|
+
{"version":3,"file":"filesink.deno.js","names":["denoDriver: RotatingFileSinkDriver<Deno.FsFile>","path: string","fd: Deno.FsFile","chunks: Uint8Array[]","denoAsyncDriver: AsyncRotatingFileSinkDriver<Deno.FsFile>","denoTimeDriver: TimeRotatingFileSinkDriver<Deno.FsFile>","options?: { recursive?: boolean }","denoAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<Deno.FsFile>","options: FileSinkOptions","options: RotatingFileSinkOptions","options: TimeRotatingFileSinkOptions","driver"],"sources":["../src/filesink.deno.ts"],"sourcesContent":["import type { Sink } from \"@logtape/logtape\";\nimport { join } from \"@std/path/join\";\nimport {\n type AsyncRotatingFileSinkDriver,\n type FileSinkOptions,\n getBaseFileSink,\n getBaseRotatingFileSink,\n type RotatingFileSinkDriver,\n type RotatingFileSinkOptions,\n} from \"./filesink.base.ts\";\nimport {\n type AsyncTimeRotatingFileSinkDriver,\n getBaseTimeRotatingFileSink,\n type TimeRotatingFileSinkDriver,\n type TimeRotatingFileSinkOptions,\n} from \"./timefilesink.ts\";\n\n/**\n * A Deno-specific file sink driver.\n */\nexport const denoDriver: RotatingFileSinkDriver<Deno.FsFile> = {\n openSync(path: string) {\n return Deno.openSync(path, { create: true, append: true });\n },\n writeSync(fd, chunk) {\n fd.writeSync(chunk);\n },\n writeManySync(fd: Deno.FsFile, chunks: Uint8Array[]): void {\n // Deno doesn't have writev, but we can optimize by writing all chunks\n // then doing a single sync operation\n for (const chunk of chunks) {\n fd.writeSync(chunk);\n }\n },\n flushSync(fd) {\n fd.syncSync();\n },\n closeSync(fd) {\n fd.close();\n },\n statSync: globalThis?.Deno.statSync,\n renameSync: globalThis?.Deno.renameSync,\n unlinkSync: globalThis?.Deno.removeSync,\n};\n\n/**\n * A Deno-specific async file sink driver.\n * @since 1.0.0\n */\nexport const denoAsyncDriver: AsyncRotatingFileSinkDriver<Deno.FsFile> = {\n ...denoDriver,\n async writeMany(fd: Deno.FsFile, chunks: Uint8Array[]): Promise<void> {\n // Deno doesn't have async writev, but we can write all chunks\n // then do a single async sync\n for (const chunk of chunks) {\n await fd.write(chunk);\n }\n },\n async flush(fd) {\n await fd.sync();\n },\n close(fd) {\n return Promise.resolve(fd.close());\n },\n};\n\n/**\n * A Deno-specific time-rotating file sink driver.\n * @since 2.0.0\n */\nexport const denoTimeDriver: TimeRotatingFileSinkDriver<Deno.FsFile> = {\n ...denoDriver,\n readdirSync(path: string) {\n return [...Deno.readDirSync(path)].map((entry) => entry.name);\n },\n unlinkSync: globalThis?.Deno.removeSync,\n mkdirSync(path: string, options?: { recursive?: boolean }) {\n Deno.mkdirSync(path, options);\n },\n joinPath: join,\n};\n\n/**\n * A Deno-specific async time-rotating file sink driver.\n * @since 2.0.0\n */\nexport const denoAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<Deno.FsFile> =\n {\n ...denoAsyncDriver,\n readdirSync(path: string) {\n return [...Deno.readDirSync(path)].map((entry) => entry.name);\n },\n unlinkSync: globalThis?.Deno.removeSync,\n mkdirSync(path: string, options?: { recursive?: boolean }) {\n Deno.mkdirSync(path, options);\n },\n joinPath: join,\n };\n\n/**\n * Get a file sink.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getFileSink(\n path: string,\n options?: FileSinkOptions,\n): Sink & Disposable;\nexport function getFileSink(\n path: string,\n options: FileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getFileSink(\n path: string,\n options: FileSinkOptions = {},\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseFileSink(path, { ...options, ...denoAsyncDriver });\n }\n return getBaseFileSink(path, { ...options, ...denoDriver });\n}\n\n/**\n * Get a rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getRotatingFileSink(\n path: string,\n options?: RotatingFileSinkOptions,\n): Sink & Disposable;\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions = {},\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseRotatingFileSink(path, { ...options, ...denoAsyncDriver });\n }\n return getBaseRotatingFileSink(path, { ...options, ...denoDriver });\n}\n\n/**\n * Get a time-rotating file sink.\n *\n * This sink writes log records to a file in a directory, rotating to a new\n * file based on time intervals. The filename is generated based on the\n * current date/time and the configured interval.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is\n * enabled, returns a sink that also implements {@link AsyncDisposable}.\n * @since 2.0.0\n */\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions,\n): Sink & Disposable;\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions,\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n const driver = {\n ...denoAsyncTimeDriver,\n statSync: globalThis?.Deno.statSync,\n };\n return getBaseTimeRotatingFileSink({ ...options, ...driver });\n }\n const driver = { ...denoTimeDriver, statSync: globalThis?.Deno.statSync };\n return getBaseTimeRotatingFileSink({ ...options, ...driver });\n}\n\n// cSpell: ignore filesink\n"],"mappings":";;;;;;;;AAoBA,MAAaA,aAAkD;CAC7D,SAASC,MAAc;AACrB,SAAO,KAAK,SAAS,MAAM;GAAE,QAAQ;GAAM,QAAQ;EAAM,EAAC;CAC3D;CACD,UAAU,IAAI,OAAO;AACnB,KAAG,UAAU,MAAM;CACpB;CACD,cAAcC,IAAiBC,QAA4B;AAGzD,OAAK,MAAM,SAAS,OAClB,IAAG,UAAU,MAAM;CAEtB;CACD,UAAU,IAAI;AACZ,KAAG,UAAU;CACd;CACD,UAAU,IAAI;AACZ,KAAG,OAAO;CACX;CACD,UAAU,YAAY,KAAK;CAC3B,YAAY,YAAY,KAAK;CAC7B,YAAY,YAAY,KAAK;AAC9B;;;;;AAMD,MAAaC,kBAA4D;CACvE,GAAG;CACH,MAAM,UAAUF,IAAiBC,QAAqC;AAGpE,OAAK,MAAM,SAAS,OAClB,OAAM,GAAG,MAAM,MAAM;CAExB;CACD,MAAM,MAAM,IAAI;AACd,QAAM,GAAG,MAAM;CAChB;CACD,MAAM,IAAI;AACR,SAAO,QAAQ,QAAQ,GAAG,OAAO,C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ink.node.cjs
CHANGED
|
@@ -88,13 +88,23 @@ function getRotatingFileSink(path, options = {}) {
|
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
90
|
function getTimeRotatingFileSink(options) {
|
|
91
|
-
if (options.nonBlocking)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
if (options.nonBlocking) {
|
|
92
|
+
const driver$1 = {
|
|
93
|
+
...nodeAsyncTimeDriver,
|
|
94
|
+
statSync: node_fs.default.statSync
|
|
95
|
+
};
|
|
96
|
+
return require_timefilesink.getBaseTimeRotatingFileSink({
|
|
97
|
+
...options,
|
|
98
|
+
...driver$1
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
const driver = {
|
|
102
|
+
...nodeTimeDriver,
|
|
103
|
+
statSync: node_fs.default.statSync
|
|
104
|
+
};
|
|
95
105
|
return require_timefilesink.getBaseTimeRotatingFileSink({
|
|
96
106
|
...options,
|
|
97
|
-
...
|
|
107
|
+
...driver
|
|
98
108
|
});
|
|
99
109
|
}
|
|
100
110
|
|
package/dist/filesink.node.js
CHANGED
|
@@ -87,13 +87,23 @@ function getRotatingFileSink(path, options = {}) {
|
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
function getTimeRotatingFileSink(options) {
|
|
90
|
-
if (options.nonBlocking)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
if (options.nonBlocking) {
|
|
91
|
+
const driver$1 = {
|
|
92
|
+
...nodeAsyncTimeDriver,
|
|
93
|
+
statSync: fs.statSync
|
|
94
|
+
};
|
|
95
|
+
return getBaseTimeRotatingFileSink({
|
|
96
|
+
...options,
|
|
97
|
+
...driver$1
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const driver = {
|
|
101
|
+
...nodeTimeDriver,
|
|
102
|
+
statSync: fs.statSync
|
|
103
|
+
};
|
|
94
104
|
return getBaseTimeRotatingFileSink({
|
|
95
105
|
...options,
|
|
96
|
-
...
|
|
106
|
+
...driver
|
|
97
107
|
});
|
|
98
108
|
}
|
|
99
109
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filesink.node.js","names":["nodeDriver: RotatingFileSinkDriver<number | void>","path: string","fd: number","chunks: Uint8Array[]","nodeAsyncDriver: AsyncRotatingFileSinkDriver<number | void>","nodeTimeDriver: TimeRotatingFileSinkDriver<number | void>","nodeAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<\n number | void\n>","options: FileSinkOptions","options: RotatingFileSinkOptions","options: TimeRotatingFileSinkOptions"],"sources":["../src/filesink.node.ts"],"sourcesContent":["import type { Sink } from \"@logtape/logtape\";\nimport fs from \"node:fs\";\nimport { join } from \"node:path\";\nimport { promisify } from \"node:util\";\nimport {\n type AsyncRotatingFileSinkDriver,\n type FileSinkOptions,\n getBaseFileSink,\n getBaseRotatingFileSink,\n type RotatingFileSinkDriver,\n type RotatingFileSinkOptions,\n} from \"./filesink.base.ts\";\nimport {\n type AsyncTimeRotatingFileSinkDriver,\n getBaseTimeRotatingFileSink,\n type TimeRotatingFileSinkDriver,\n type TimeRotatingFileSinkOptions,\n} from \"./timefilesink.ts\";\n\n/**\n * A Node.js-specific file sink driver.\n */\nexport const nodeDriver: RotatingFileSinkDriver<number | void> = {\n openSync(path: string) {\n return fs.openSync(path, \"a\");\n },\n writeSync: fs.writeSync,\n writeManySync(fd: number, chunks: Uint8Array[]): void {\n if (chunks.length === 0) return;\n if (chunks.length === 1) {\n fs.writeSync(fd, chunks[0]);\n return;\n }\n // Use writev for multiple chunks\n fs.writevSync(fd, chunks);\n },\n flushSync: fs.fsyncSync,\n closeSync: fs.closeSync,\n statSync: fs.statSync,\n renameSync: fs.renameSync,\n unlinkSync: fs.unlinkSync,\n};\n\n/**\n * A Node.js-specific async file sink driver.\n * @since 1.0.0\n */\nexport const nodeAsyncDriver: AsyncRotatingFileSinkDriver<number | void> = {\n ...nodeDriver,\n async writeMany(fd: number, chunks: Uint8Array[]): Promise<void> {\n if (chunks.length === 0) return;\n if (chunks.length === 1) {\n await promisify(fs.write)(fd, chunks[0]);\n return;\n }\n // Use async writev for multiple chunks\n await promisify(fs.writev)(fd, chunks);\n },\n flush: promisify(fs.fsync),\n close: promisify(fs.close),\n};\n\n/**\n * A Node.js-specific time-rotating file sink driver.\n * @since 2.0.0\n */\nexport const nodeTimeDriver: TimeRotatingFileSinkDriver<number | void> = {\n ...nodeDriver,\n readdirSync: fs.readdirSync as (path: string) => string[],\n unlinkSync: fs.unlinkSync,\n mkdirSync: fs.mkdirSync,\n joinPath: join,\n};\n\n/**\n * A Node.js-specific async time-rotating file sink driver.\n * @since 2.0.0\n */\nexport const nodeAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<\n number | void\n> = {\n ...nodeAsyncDriver,\n readdirSync: fs.readdirSync as (path: string) => string[],\n unlinkSync: fs.unlinkSync,\n mkdirSync: fs.mkdirSync,\n joinPath: join,\n};\n\n/**\n * Get a file sink.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getFileSink(\n path: string,\n options?: FileSinkOptions,\n): Sink & Disposable;\nexport function getFileSink(\n path: string,\n options: FileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getFileSink(\n path: string,\n options: FileSinkOptions = {},\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseFileSink(path, { ...options, ...nodeAsyncDriver });\n }\n return getBaseFileSink(path, { ...options, ...nodeDriver });\n}\n\n/**\n * Get a rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getRotatingFileSink(\n path: string,\n options?: RotatingFileSinkOptions,\n): Sink & Disposable;\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions = {},\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseRotatingFileSink(path, { ...options, ...nodeAsyncDriver });\n }\n return getBaseRotatingFileSink(path, { ...options, ...nodeDriver });\n}\n\n/**\n * Get a time-rotating file sink.\n *\n * This sink writes log records to a file in a directory, rotating to a new\n * file based on time intervals. The filename is generated based on the\n * current date/time and the configured interval.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is\n * enabled, returns a sink that also implements {@link AsyncDisposable}.\n * @since 2.0.0\n */\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions,\n): Sink & Disposable;\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions,\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseTimeRotatingFileSink({ ...options, ...
|
|
1
|
+
{"version":3,"file":"filesink.node.js","names":["nodeDriver: RotatingFileSinkDriver<number | void>","path: string","fd: number","chunks: Uint8Array[]","nodeAsyncDriver: AsyncRotatingFileSinkDriver<number | void>","nodeTimeDriver: TimeRotatingFileSinkDriver<number | void>","nodeAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<\n number | void\n>","options: FileSinkOptions","options: RotatingFileSinkOptions","options: TimeRotatingFileSinkOptions","driver"],"sources":["../src/filesink.node.ts"],"sourcesContent":["import type { Sink } from \"@logtape/logtape\";\nimport fs from \"node:fs\";\nimport { join } from \"node:path\";\nimport { promisify } from \"node:util\";\nimport {\n type AsyncRotatingFileSinkDriver,\n type FileSinkOptions,\n getBaseFileSink,\n getBaseRotatingFileSink,\n type RotatingFileSinkDriver,\n type RotatingFileSinkOptions,\n} from \"./filesink.base.ts\";\nimport {\n type AsyncTimeRotatingFileSinkDriver,\n getBaseTimeRotatingFileSink,\n type TimeRotatingFileSinkDriver,\n type TimeRotatingFileSinkOptions,\n} from \"./timefilesink.ts\";\n\n/**\n * A Node.js-specific file sink driver.\n */\nexport const nodeDriver: RotatingFileSinkDriver<number | void> = {\n openSync(path: string) {\n return fs.openSync(path, \"a\");\n },\n writeSync: fs.writeSync,\n writeManySync(fd: number, chunks: Uint8Array[]): void {\n if (chunks.length === 0) return;\n if (chunks.length === 1) {\n fs.writeSync(fd, chunks[0]);\n return;\n }\n // Use writev for multiple chunks\n fs.writevSync(fd, chunks);\n },\n flushSync: fs.fsyncSync,\n closeSync: fs.closeSync,\n statSync: fs.statSync,\n renameSync: fs.renameSync,\n unlinkSync: fs.unlinkSync,\n};\n\n/**\n * A Node.js-specific async file sink driver.\n * @since 1.0.0\n */\nexport const nodeAsyncDriver: AsyncRotatingFileSinkDriver<number | void> = {\n ...nodeDriver,\n async writeMany(fd: number, chunks: Uint8Array[]): Promise<void> {\n if (chunks.length === 0) return;\n if (chunks.length === 1) {\n await promisify(fs.write)(fd, chunks[0]);\n return;\n }\n // Use async writev for multiple chunks\n await promisify(fs.writev)(fd, chunks);\n },\n flush: promisify(fs.fsync),\n close: promisify(fs.close),\n};\n\n/**\n * A Node.js-specific time-rotating file sink driver.\n * @since 2.0.0\n */\nexport const nodeTimeDriver: TimeRotatingFileSinkDriver<number | void> = {\n ...nodeDriver,\n readdirSync: fs.readdirSync as (path: string) => string[],\n unlinkSync: fs.unlinkSync,\n mkdirSync: fs.mkdirSync,\n joinPath: join,\n};\n\n/**\n * A Node.js-specific async time-rotating file sink driver.\n * @since 2.0.0\n */\nexport const nodeAsyncTimeDriver: AsyncTimeRotatingFileSinkDriver<\n number | void\n> = {\n ...nodeAsyncDriver,\n readdirSync: fs.readdirSync as (path: string) => string[],\n unlinkSync: fs.unlinkSync,\n mkdirSync: fs.mkdirSync,\n joinPath: join,\n};\n\n/**\n * Get a file sink.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getFileSink(\n path: string,\n options?: FileSinkOptions,\n): Sink & Disposable;\nexport function getFileSink(\n path: string,\n options: FileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getFileSink(\n path: string,\n options: FileSinkOptions = {},\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseFileSink(path, { ...options, ...nodeAsyncDriver });\n }\n return getBaseFileSink(path, { ...options, ...nodeDriver });\n}\n\n/**\n * Get a rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getRotatingFileSink(\n path: string,\n options?: RotatingFileSinkOptions,\n): Sink & Disposable;\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions = {},\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n return getBaseRotatingFileSink(path, { ...options, ...nodeAsyncDriver });\n }\n return getBaseRotatingFileSink(path, { ...options, ...nodeDriver });\n}\n\n/**\n * Get a time-rotating file sink.\n *\n * This sink writes log records to a file in a directory, rotating to a new\n * file based on time intervals. The filename is generated based on the\n * current date/time and the configured interval.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is\n * enabled, returns a sink that also implements {@link AsyncDisposable}.\n * @since 2.0.0\n */\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions,\n): Sink & Disposable;\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions & { nonBlocking: true },\n): Sink & AsyncDisposable;\nexport function getTimeRotatingFileSink(\n options: TimeRotatingFileSinkOptions,\n): Sink & (Disposable | AsyncDisposable) {\n if (options.nonBlocking) {\n const driver = { ...nodeAsyncTimeDriver, statSync: fs.statSync };\n return getBaseTimeRotatingFileSink({ ...options, ...driver });\n }\n const driver = { ...nodeTimeDriver, statSync: fs.statSync };\n return getBaseTimeRotatingFileSink({ ...options, ...driver });\n}\n\n// cSpell: ignore filesink\n"],"mappings":";;;;;;;;;;AAsBA,MAAaA,aAAoD;CAC/D,SAASC,MAAc;AACrB,SAAO,GAAG,SAAS,MAAM,IAAI;CAC9B;CACD,WAAW,GAAG;CACd,cAAcC,IAAYC,QAA4B;AACpD,MAAI,OAAO,WAAW,EAAG;AACzB,MAAI,OAAO,WAAW,GAAG;AACvB,MAAG,UAAU,IAAI,OAAO,GAAG;AAC3B;EACD;AAED,KAAG,WAAW,IAAI,OAAO;CAC1B;CACD,WAAW,GAAG;CACd,WAAW,GAAG;CACd,UAAU,GAAG;CACb,YAAY,GAAG;CACf,YAAY,GAAG;AAChB;;;;;AAMD,MAAaC,kBAA8D;CACzE,GAAG;CACH,MAAM,UAAUF,IAAYC,QAAqC;AAC/D,MAAI,OAAO,WAAW,EAAG;AACzB,MAAI,OAAO,WAAW,GAAG;AACvB,SAAM,UAAU,GAAG,MAAM,CAAC,IAAI,OAAO,GAAG;AACxC;EACD;AAED,QAAM,UAAU,GAAG,OAAO,CAAC,IAAI,OAAO;CACvC;CACD,OAAO,UAAU,GAAG,MAAM;CAC1B,OAAO,UAAU,GAAG,MAAM;AAC3B;;;;;AAMD,MAAaE,iBAA4D;CACvE,GAAG;CACH,aAAa,GAAG;CAChB,YAAY,GAAG;CACf,WAAW,GAAG;CACd,UAAU;AACX;;;;;AAMD,MAAaC,sBAET;CACF,GAAG;CACH,aAAa,GAAG;CAChB,YAAY,GAAG;CACf,WAAW,GAAG;CACd,UAAU;AACX;AAqBD,SAAgB,YACdL,MACAM,UAA2B,CAAE,GACU;AACvC,KAAI,QAAQ,YACV,QAAO,gBAAgB,MAAM;EAAE,GAAG;EAAS,GAAG;CAAiB,EAAC;AAElE,QAAO,gBAAgB,MAAM;EAAE,GAAG;EAAS,GAAG;CAAY,EAAC;AAC5D;AA0BD,SAAgB,oBACdN,MACAO,UAAmC,CAAE,GACE;AACvC,KAAI,QAAQ,YACV,QAAO,wBAAwB,MAAM;EAAE,GAAG;EAAS,GAAG;CAAiB,EAAC;AAE1E,QAAO,wBAAwB,MAAM;EAAE,GAAG;EAAS,GAAG;CAAY,EAAC;AACpE;AAuBD,SAAgB,wBACdC,SACuC;AACvC,KAAI,QAAQ,aAAa;EACvB,MAAMC,WAAS;GAAE,GAAG;GAAqB,UAAU,GAAG;EAAU;AAChE,SAAO,4BAA4B;GAAE,GAAG;GAAS,GAAGA;EAAQ,EAAC;CAC9D;CACD,MAAM,SAAS;EAAE,GAAG;EAAgB,UAAU,GAAG;CAAU;AAC3D,QAAO,4BAA4B;EAAE,GAAG;EAAS,GAAG;CAAQ,EAAC;AAC9D"}
|
package/dist/mod.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions } from "./filesink.base.cjs";
|
|
2
2
|
import { TimeRotatingFileSinkOptions, TimeRotationInterval } from "./timefilesink.cjs";
|
|
3
3
|
import { StreamFileSinkOptions, getStreamFileSink } from "./streamfilesink.cjs";
|
|
4
|
-
import { getFileSink, getRotatingFileSink, getTimeRotatingFileSink } from "
|
|
4
|
+
import { getFileSink, getRotatingFileSink, getTimeRotatingFileSink } from "./dist/filesink.node.cjs";
|
|
5
5
|
export { FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions, StreamFileSinkOptions, TimeRotatingFileSinkOptions, TimeRotationInterval, getFileSink, getRotatingFileSink, getStreamFileSink, getTimeRotatingFileSink };
|
package/dist/timefilesink.cjs
CHANGED
|
@@ -75,16 +75,38 @@ function getRotationIntervalMs(interval) {
|
|
|
75
75
|
case "weekly": return 7 * 24 * 60 * 60 * 1e3;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
+
function hasStatSyncDriver(options) {
|
|
79
|
+
return "statSync" in options && typeof options.statSync === "function";
|
|
80
|
+
}
|
|
81
|
+
function parseDefaultFilename(file) {
|
|
82
|
+
const dateMatch = file.match(/^(\d{4})-(\d{2})-(\d{2})(?:-(\d{2}))?\.log$/);
|
|
83
|
+
const weekMatch = file.match(/^(\d{4})-W(\d{2})\.log$/);
|
|
84
|
+
if (dateMatch) {
|
|
85
|
+
const [, year, month, day, hour] = dateMatch;
|
|
86
|
+
return new Date(parseInt(year, 10), parseInt(month, 10) - 1, parseInt(day, 10), hour ? parseInt(hour, 10) : 0);
|
|
87
|
+
}
|
|
88
|
+
if (weekMatch) {
|
|
89
|
+
const [, year, week] = weekMatch;
|
|
90
|
+
const jan4 = new Date(parseInt(year, 10), 0, 4);
|
|
91
|
+
const dayOfWeek = jan4.getDay() || 7;
|
|
92
|
+
const fileDate = new Date(jan4);
|
|
93
|
+
fileDate.setDate(jan4.getDate() - dayOfWeek + 1 + (parseInt(week, 10) - 1) * 7);
|
|
94
|
+
return fileDate;
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
78
98
|
function getBaseTimeRotatingFileSink(options) {
|
|
79
99
|
const formatter = options.formatter ?? __logtape_logtape.defaultTextFormatter;
|
|
80
100
|
const encoder = options.encoder ?? new TextEncoder();
|
|
81
101
|
const interval = options.interval ?? "daily";
|
|
102
|
+
const hasCustomFilename = options.filename !== void 0;
|
|
82
103
|
const filenameGenerator = options.filename ?? getDefaultFilename(interval);
|
|
83
104
|
const maxAgeMs = options.maxAgeMs;
|
|
84
105
|
const cleanupInterval = maxAgeMs === void 0 ? getRotationIntervalMs(interval) : Math.min(getRotationIntervalMs(interval), maxAgeMs);
|
|
85
106
|
const bufferSize = options.bufferSize ?? 1024 * 8;
|
|
86
107
|
const flushInterval = options.flushInterval ?? 5e3;
|
|
87
108
|
const directory = options.directory;
|
|
109
|
+
const statSync = hasStatSyncDriver(options) ? options.statSync : void 0;
|
|
88
110
|
try {
|
|
89
111
|
options.mkdirSync(directory, { recursive: true });
|
|
90
112
|
} catch {}
|
|
@@ -120,20 +142,19 @@ function getBaseTimeRotatingFileSink(options) {
|
|
|
120
142
|
return;
|
|
121
143
|
}
|
|
122
144
|
for (const file of files) {
|
|
123
|
-
if (!file.endsWith(".log")) continue;
|
|
124
145
|
if (file === currentFilename) continue;
|
|
125
|
-
const dateMatch = file.match(/^(\d{4})-(\d{2})-(\d{2})(?:-(\d{2}))?\.log$/);
|
|
126
|
-
const weekMatch = file.match(/^(\d{4})-W(\d{2})\.log$/);
|
|
127
146
|
let fileDate = null;
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
fileDate
|
|
136
|
-
|
|
147
|
+
if (hasCustomFilename) {
|
|
148
|
+
if (statSync === void 0) continue;
|
|
149
|
+
try {
|
|
150
|
+
fileDate = statSync(options.joinPath(directory, file)).mtime;
|
|
151
|
+
} catch {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (fileDate === null || filenameGenerator(fileDate) !== file) continue;
|
|
155
|
+
} else {
|
|
156
|
+
if (!file.endsWith(".log")) continue;
|
|
157
|
+
fileDate = parseDefaultFilename(file);
|
|
137
158
|
}
|
|
138
159
|
if (fileDate && now - fileDate.getTime() > maxAgeMs) {
|
|
139
160
|
const filePath = options.joinPath(directory, file);
|
package/dist/timefilesink.d.cts
CHANGED
|
@@ -30,6 +30,9 @@ interface TimeRotatingFileSinkOptions extends Omit<FileSinkOptions, "lazy"> {
|
|
|
30
30
|
/**
|
|
31
31
|
* The maximum age of log files in milliseconds. Files older than this
|
|
32
32
|
* will be deleted. If not specified, old files are not deleted.
|
|
33
|
+
*
|
|
34
|
+
* When using `getTimeRotatingFileSink()` with `filename` set, cleanup uses
|
|
35
|
+
* each file's modification time instead of parsing dates from filenames.
|
|
33
36
|
*/
|
|
34
37
|
maxAgeMs?: number;
|
|
35
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timefilesink.d.cts","names":[],"sources":["../src/timefilesink.ts"],"sourcesContent":[],"mappings":";;;;;;;AAeA;AAKiB,KALL,oBAAA,GAMV,QAAA,GAAA,OAAA,GAAA,QAAA;;;;AAkBW,UAnBI,2BAAA,SACP,IAkBG,CAlBE,eAkBF,EAAA,MAAA,CAAA,CAAA;EAAoB;AAlBnB;
|
|
1
|
+
{"version":3,"file":"timefilesink.d.cts","names":[],"sources":["../src/timefilesink.ts"],"sourcesContent":[],"mappings":";;;;;;;AAeA;AAKiB,KALL,oBAAA,GAMV,QAAA,GAAA,OAAA,GAAA,QAAA;;;;AAkBW,UAnBI,2BAAA,SACP,IAkBG,CAlBE,eAkBF,EAAA,MAAA,CAAA,CAAA;EAAoB;AAlBnB;AAiCd;EAA2C,SAAA,EAAA,MAAA;EAAA;;AACnB;AAiCxB;;;;EAC6B,QAAA,CAAA,EAAA,CAAA,IAAA,EAvDT,IAuDS,EAAA,GAAA,MAAA;;;;aAlDhB;;;;;;;;;;;;;UAeI,0CACP,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAiCR,+CACP,oBAAoB"}
|
package/dist/timefilesink.d.ts
CHANGED
|
@@ -30,6 +30,9 @@ interface TimeRotatingFileSinkOptions extends Omit<FileSinkOptions, "lazy"> {
|
|
|
30
30
|
/**
|
|
31
31
|
* The maximum age of log files in milliseconds. Files older than this
|
|
32
32
|
* will be deleted. If not specified, old files are not deleted.
|
|
33
|
+
*
|
|
34
|
+
* When using `getTimeRotatingFileSink()` with `filename` set, cleanup uses
|
|
35
|
+
* each file's modification time instead of parsing dates from filenames.
|
|
33
36
|
*/
|
|
34
37
|
maxAgeMs?: number;
|
|
35
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timefilesink.d.ts","names":[],"sources":["../src/timefilesink.ts"],"sourcesContent":[],"mappings":";;;;;;;AAeA;AAKiB,KALL,oBAAA,GAMV,QAAA,GAAA,OAAA,GAAA,QAAA;;;;AAkBW,UAnBI,2BAAA,SACP,IAkBG,CAlBE,eAkBF,EAAA,MAAA,CAAA,CAAA;EAAoB;AAlBnB;
|
|
1
|
+
{"version":3,"file":"timefilesink.d.ts","names":[],"sources":["../src/timefilesink.ts"],"sourcesContent":[],"mappings":";;;;;;;AAeA;AAKiB,KALL,oBAAA,GAMV,QAAA,GAAA,OAAA,GAAA,QAAA;;;;AAkBW,UAnBI,2BAAA,SACP,IAkBG,CAlBE,eAkBF,EAAA,MAAA,CAAA,CAAA;EAAoB;AAlBnB;AAiCd;EAA2C,SAAA,EAAA,MAAA;EAAA;;AACnB;AAiCxB;;;;EAC6B,QAAA,CAAA,EAAA,CAAA,IAAA,EAvDT,IAuDS,EAAA,GAAA,MAAA;;;;aAlDhB;;;;;;;;;;;;;UAeI,0CACP,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAiCR,+CACP,oBAAoB"}
|
package/dist/timefilesink.js
CHANGED
|
@@ -74,16 +74,38 @@ function getRotationIntervalMs(interval) {
|
|
|
74
74
|
case "weekly": return 7 * 24 * 60 * 60 * 1e3;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
+
function hasStatSyncDriver(options) {
|
|
78
|
+
return "statSync" in options && typeof options.statSync === "function";
|
|
79
|
+
}
|
|
80
|
+
function parseDefaultFilename(file) {
|
|
81
|
+
const dateMatch = file.match(/^(\d{4})-(\d{2})-(\d{2})(?:-(\d{2}))?\.log$/);
|
|
82
|
+
const weekMatch = file.match(/^(\d{4})-W(\d{2})\.log$/);
|
|
83
|
+
if (dateMatch) {
|
|
84
|
+
const [, year, month, day, hour] = dateMatch;
|
|
85
|
+
return new Date(parseInt(year, 10), parseInt(month, 10) - 1, parseInt(day, 10), hour ? parseInt(hour, 10) : 0);
|
|
86
|
+
}
|
|
87
|
+
if (weekMatch) {
|
|
88
|
+
const [, year, week] = weekMatch;
|
|
89
|
+
const jan4 = new Date(parseInt(year, 10), 0, 4);
|
|
90
|
+
const dayOfWeek = jan4.getDay() || 7;
|
|
91
|
+
const fileDate = new Date(jan4);
|
|
92
|
+
fileDate.setDate(jan4.getDate() - dayOfWeek + 1 + (parseInt(week, 10) - 1) * 7);
|
|
93
|
+
return fileDate;
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
77
97
|
function getBaseTimeRotatingFileSink(options) {
|
|
78
98
|
const formatter = options.formatter ?? defaultTextFormatter;
|
|
79
99
|
const encoder = options.encoder ?? new TextEncoder();
|
|
80
100
|
const interval = options.interval ?? "daily";
|
|
101
|
+
const hasCustomFilename = options.filename !== void 0;
|
|
81
102
|
const filenameGenerator = options.filename ?? getDefaultFilename(interval);
|
|
82
103
|
const maxAgeMs = options.maxAgeMs;
|
|
83
104
|
const cleanupInterval = maxAgeMs === void 0 ? getRotationIntervalMs(interval) : Math.min(getRotationIntervalMs(interval), maxAgeMs);
|
|
84
105
|
const bufferSize = options.bufferSize ?? 1024 * 8;
|
|
85
106
|
const flushInterval = options.flushInterval ?? 5e3;
|
|
86
107
|
const directory = options.directory;
|
|
108
|
+
const statSync = hasStatSyncDriver(options) ? options.statSync : void 0;
|
|
87
109
|
try {
|
|
88
110
|
options.mkdirSync(directory, { recursive: true });
|
|
89
111
|
} catch {}
|
|
@@ -119,20 +141,19 @@ function getBaseTimeRotatingFileSink(options) {
|
|
|
119
141
|
return;
|
|
120
142
|
}
|
|
121
143
|
for (const file of files) {
|
|
122
|
-
if (!file.endsWith(".log")) continue;
|
|
123
144
|
if (file === currentFilename) continue;
|
|
124
|
-
const dateMatch = file.match(/^(\d{4})-(\d{2})-(\d{2})(?:-(\d{2}))?\.log$/);
|
|
125
|
-
const weekMatch = file.match(/^(\d{4})-W(\d{2})\.log$/);
|
|
126
145
|
let fileDate = null;
|
|
127
|
-
if (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
fileDate
|
|
135
|
-
|
|
146
|
+
if (hasCustomFilename) {
|
|
147
|
+
if (statSync === void 0) continue;
|
|
148
|
+
try {
|
|
149
|
+
fileDate = statSync(options.joinPath(directory, file)).mtime;
|
|
150
|
+
} catch {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (fileDate === null || filenameGenerator(fileDate) !== file) continue;
|
|
154
|
+
} else {
|
|
155
|
+
if (!file.endsWith(".log")) continue;
|
|
156
|
+
fileDate = parseDefaultFilename(file);
|
|
136
157
|
}
|
|
137
158
|
if (fileDate && now - fileDate.getTime() > maxAgeMs) {
|
|
138
159
|
const filePath = options.joinPath(directory, file);
|
package/dist/timefilesink.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timefilesink.js","names":["date: Date","interval: TimeRotationInterval","options:\n & TimeRotatingFileSinkOptions\n & (\n | TimeRotatingFileSinkDriver<TFile>\n | AsyncTimeRotatingFileSinkDriver<TFile>\n )","currentFilename: string","currentPath: string","currentRotationKey: string","fd: TFile","lastFlushTimestamp: number","lastCleanupTimestamp: number | undefined","buffer: string","files: string[]","fileDate: Date | null","flushBuffer","sink: Sink & Disposable","record: LogRecord","activeFlush: Promise<void> | null","flushTimer: ReturnType<typeof setInterval> | null","nonBlockingSink: Sink & AsyncDisposable"],"sources":["../src/timefilesink.ts"],"sourcesContent":["import {\n defaultTextFormatter,\n type LogRecord,\n type Sink,\n} from \"@logtape/logtape\";\nimport type {\n AsyncFileSinkDriver,\n FileSinkDriver,\n FileSinkOptions,\n} from \"./filesink.base.ts\";\nimport { markSinkAsImmediate } from \"./snapshot.ts\";\n\n/**\n * The rotation interval for time-based file sinks.\n */\nexport type TimeRotationInterval = \"hourly\" | \"daily\" | \"weekly\";\n\n/**\n * Options for the {@link getBaseTimeRotatingFileSink} function.\n */\nexport interface TimeRotatingFileSinkOptions\n extends Omit<FileSinkOptions, \"lazy\"> {\n /**\n * The directory to write log files to.\n */\n directory: string;\n\n /**\n * A function that generates the filename for the log file based on the date.\n * Default depends on `interval`:\n * - `\"daily\"`: `YYYY-MM-DD.log` (e.g., `2025-01-15.log`)\n * - `\"hourly\"`: `YYYY-MM-DD-HH.log` (e.g., `2025-01-15-09.log`)\n * - `\"weekly\"`: `YYYY-WW.log` (e.g., `2025-W03.log`)\n */\n filename?: (date: Date) => string;\n\n /**\n * The rotation interval. Defaults to `\"daily\"`.\n */\n interval?: TimeRotationInterval;\n\n /**\n * The maximum age of log files in milliseconds. Files older than this\n * will be deleted. If not specified, old files are not deleted.\n */\n maxAgeMs?: number;\n}\n\n/**\n * A platform-specific time-rotating file sink driver.\n */\nexport interface TimeRotatingFileSinkDriver<TFile>\n extends FileSinkDriver<TFile> {\n /**\n * Read the contents of a directory.\n * @param path A path to the directory.\n * @returns An array of filenames in the directory.\n */\n readdirSync(path: string): string[];\n\n /**\n * Delete a file.\n * @param path A path to the file to delete.\n */\n unlinkSync(path: string): void;\n\n /**\n * Create a directory if it doesn't exist.\n * @param path A path to the directory to create.\n * @param options Options for directory creation.\n */\n mkdirSync(path: string, options?: { recursive?: boolean }): void;\n\n /**\n * Join path segments.\n * @param paths Path segments to join.\n * @returns The joined path.\n */\n joinPath(...paths: string[]): string;\n}\n\n/**\n * A platform-specific async time-rotating file sink driver.\n * @since 2.0.0\n */\nexport interface AsyncTimeRotatingFileSinkDriver<TFile>\n extends AsyncFileSinkDriver<TFile> {\n /**\n * Read the contents of a directory.\n * @param path A path to the directory.\n * @returns An array of filenames in the directory.\n */\n readdirSync(path: string): string[];\n\n /**\n * Delete a file.\n * @param path A path to the file to delete.\n */\n unlinkSync(path: string): void;\n\n /**\n * Create a directory if it doesn't exist.\n * @param path A path to the directory to create.\n * @param options Options for directory creation.\n */\n mkdirSync(path: string, options?: { recursive?: boolean }): void;\n\n /**\n * Join path segments.\n * @param paths Path segments to join.\n * @returns The joined path.\n */\n joinPath(...paths: string[]): string;\n}\n\n/**\n * Get the ISO week number of a date.\n * @param date The date to get the week number of.\n * @returns The ISO week number (1-53).\n */\nexport function getISOWeek(date: Date): number {\n const d = new Date(\n Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),\n );\n const dayNum = d.getUTCDay() || 7;\n d.setUTCDate(d.getUTCDate() + 4 - dayNum);\n const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));\n return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);\n}\n\n/**\n * Get the ISO week year of a date. This may differ from the calendar year\n * for dates near the start or end of a year.\n * @param date The date to get the ISO week year of.\n * @returns The ISO week year.\n */\nexport function getISOWeekYear(date: Date): number {\n const d = new Date(\n Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),\n );\n const dayNum = d.getUTCDay() || 7;\n d.setUTCDate(d.getUTCDate() + 4 - dayNum);\n return d.getUTCFullYear();\n}\n\n/**\n * Get the default filename generator for the given interval.\n * @param interval The rotation interval.\n * @returns A function that generates a filename for a given date.\n */\nexport function getDefaultFilename(\n interval: TimeRotationInterval,\n): (date: Date) => string {\n switch (interval) {\n case \"hourly\":\n return (date: Date): string => {\n const yyyy = date.getFullYear();\n const mm = String(date.getMonth() + 1).padStart(2, \"0\");\n const dd = String(date.getDate()).padStart(2, \"0\");\n const hh = String(date.getHours()).padStart(2, \"0\");\n return `${yyyy}-${mm}-${dd}-${hh}.log`;\n };\n case \"daily\":\n return (date: Date): string => {\n const yyyy = date.getFullYear();\n const mm = String(date.getMonth() + 1).padStart(2, \"0\");\n const dd = String(date.getDate()).padStart(2, \"0\");\n return `${yyyy}-${mm}-${dd}.log`;\n };\n case \"weekly\":\n return (date: Date): string => {\n const yyyy = getISOWeekYear(date);\n const week = getISOWeek(date);\n return `${yyyy}-W${String(week).padStart(2, \"0\")}.log`;\n };\n }\n}\n\n/**\n * Get the rotation key for the given date and interval.\n * The key is used to determine when to rotate to a new file.\n * @param date The date to get the rotation key of.\n * @param interval The rotation interval.\n * @returns A string key that changes when rotation should occur.\n */\nfunction getRotationKey(date: Date, interval: TimeRotationInterval): string {\n switch (interval) {\n case \"hourly\":\n return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}-${date.getHours()}`;\n case \"daily\":\n return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;\n case \"weekly\":\n return `${getISOWeekYear(date)}-${getISOWeek(date)}`;\n }\n}\n\nfunction getRotationIntervalMs(interval: TimeRotationInterval): number {\n switch (interval) {\n case \"hourly\":\n return 60 * 60 * 1000;\n case \"daily\":\n return 24 * 60 * 60 * 1000;\n case \"weekly\":\n return 7 * 24 * 60 * 60 * 1000;\n }\n}\n\n/**\n * Get a platform-independent time-rotating file sink.\n *\n * This sink writes log records to a file in a directory, rotating to a new\n * file based on time intervals. The filename is generated based on the\n * current date/time and the configured interval.\n *\n * @template TFile The type of the file descriptor.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is\n * enabled, returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getBaseTimeRotatingFileSink<TFile>(\n options: TimeRotatingFileSinkOptions & TimeRotatingFileSinkDriver<TFile>,\n): Sink & Disposable;\nexport function getBaseTimeRotatingFileSink<TFile>(\n options: TimeRotatingFileSinkOptions & AsyncTimeRotatingFileSinkDriver<TFile>,\n): Sink & AsyncDisposable;\nexport function getBaseTimeRotatingFileSink<TFile>(\n options:\n & TimeRotatingFileSinkOptions\n & (\n | TimeRotatingFileSinkDriver<TFile>\n | AsyncTimeRotatingFileSinkDriver<TFile>\n ),\n): Sink & (Disposable | AsyncDisposable) {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const interval = options.interval ?? \"daily\";\n const filenameGenerator = options.filename ?? getDefaultFilename(interval);\n const maxAgeMs = options.maxAgeMs;\n const cleanupInterval = maxAgeMs === undefined\n ? getRotationIntervalMs(interval)\n : Math.min(getRotationIntervalMs(interval), maxAgeMs);\n const bufferSize = options.bufferSize ?? 1024 * 8;\n const flushInterval = options.flushInterval ?? 5000;\n const directory = options.directory;\n\n // Ensure directory exists\n try {\n options.mkdirSync(directory, { recursive: true });\n } catch {\n // Directory might already exist\n }\n\n let currentFilename: string = filenameGenerator(new Date());\n let currentPath: string = options.joinPath(directory, currentFilename);\n let currentRotationKey: string = getRotationKey(new Date(), interval);\n let fd: TFile = options.openSync(currentPath);\n let lastFlushTimestamp: number = Date.now();\n let lastCleanupTimestamp: number | undefined;\n let buffer: string = \"\";\n\n function shouldRotate(): boolean {\n const now = new Date();\n const newKey = getRotationKey(now, interval);\n return newKey !== currentRotationKey;\n }\n\n function performRotation(): void {\n options.closeSync(fd);\n const now = new Date();\n currentFilename = filenameGenerator(now);\n currentPath = options.joinPath(directory, currentFilename);\n currentRotationKey = getRotationKey(now, interval);\n fd = options.openSync(currentPath);\n }\n\n function cleanupOldFiles(): void {\n if (maxAgeMs === undefined) return;\n\n const now = Date.now();\n if (\n lastCleanupTimestamp !== undefined && cleanupInterval > 0 &&\n now - lastCleanupTimestamp < cleanupInterval\n ) {\n return;\n }\n lastCleanupTimestamp = now;\n\n let files: string[];\n try {\n files = options.readdirSync(directory);\n } catch {\n return;\n }\n\n for (const file of files) {\n if (!file.endsWith(\".log\")) continue;\n if (file === currentFilename) continue;\n\n // Try to parse the date from the filename\n const dateMatch = file.match(\n /^(\\d{4})-(\\d{2})-(\\d{2})(?:-(\\d{2}))?\\.log$/,\n );\n const weekMatch = file.match(/^(\\d{4})-W(\\d{2})\\.log$/);\n\n let fileDate: Date | null = null;\n\n if (dateMatch) {\n const [, year, month, day, hour] = dateMatch;\n fileDate = new Date(\n parseInt(year!, 10),\n parseInt(month!, 10) - 1,\n parseInt(day!, 10),\n hour ? parseInt(hour, 10) : 0,\n );\n } else if (weekMatch) {\n const [, year, week] = weekMatch;\n // Get the date of the first day of the week\n const jan4 = new Date(parseInt(year!, 10), 0, 4);\n const dayOfWeek = jan4.getDay() || 7;\n fileDate = new Date(jan4);\n fileDate.setDate(\n jan4.getDate() - dayOfWeek + 1 + (parseInt(week!, 10) - 1) * 7,\n );\n }\n\n if (fileDate && now - fileDate.getTime() > maxAgeMs) {\n const filePath = options.joinPath(directory, file);\n try {\n options.unlinkSync(filePath);\n } catch {\n // Ignore errors when deleting files\n }\n }\n }\n }\n\n if (!options.nonBlocking) {\n // Blocking mode implementation\n // deno-lint-ignore no-inner-declarations\n function flushBuffer(): void {\n if (buffer.length > 0) {\n if (shouldRotate()) {\n performRotation();\n }\n const bytes = encoder.encode(buffer);\n buffer = \"\";\n options.writeSync(fd, bytes);\n options.flushSync(fd);\n lastFlushTimestamp = Date.now();\n cleanupOldFiles();\n }\n }\n\n const sink: Sink & Disposable = (record: LogRecord) => {\n buffer += formatter(record);\n\n const shouldFlushBySize = buffer.length >= bufferSize;\n const shouldFlushByTime = flushInterval > 0 &&\n (record.timestamp - lastFlushTimestamp) >= flushInterval;\n\n if (shouldFlushBySize || shouldFlushByTime) {\n flushBuffer();\n }\n };\n\n sink[Symbol.dispose] = () => {\n flushBuffer();\n options.closeSync(fd);\n };\n\n return markSinkAsImmediate(sink);\n }\n\n // Non-blocking mode implementation\n const asyncOptions = options as AsyncTimeRotatingFileSinkDriver<TFile>;\n let disposed = false;\n let activeFlush: Promise<void> | null = null;\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n\n async function flushBuffer(): Promise<void> {\n if (buffer.length === 0) return;\n\n if (shouldRotate()) {\n performRotation();\n }\n\n const data = buffer;\n buffer = \"\";\n try {\n const bytes = encoder.encode(data);\n asyncOptions.writeSync(fd, bytes);\n await asyncOptions.flush(fd);\n lastFlushTimestamp = Date.now();\n cleanupOldFiles();\n } catch {\n // Silently ignore errors in non-blocking mode\n }\n }\n\n function scheduleFlush(): void {\n if (activeFlush || disposed) return;\n\n activeFlush = flushBuffer().finally(() => {\n activeFlush = null;\n });\n }\n\n function startFlushTimer(): void {\n if (flushTimer !== null || disposed) return;\n\n flushTimer = setInterval(() => {\n scheduleFlush();\n }, flushInterval);\n }\n\n const nonBlockingSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n buffer += formatter(record);\n\n const shouldFlushBySize = buffer.length >= bufferSize;\n const shouldFlushByTime = flushInterval > 0 &&\n (record.timestamp - lastFlushTimestamp) >= flushInterval;\n\n if (shouldFlushBySize || shouldFlushByTime) {\n scheduleFlush();\n } else if (flushTimer === null && flushInterval > 0) {\n startFlushTimer();\n }\n };\n\n nonBlockingSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n await flushBuffer();\n try {\n await asyncOptions.close(fd);\n } catch {\n // Writer might already be closed or errored\n }\n };\n\n return markSinkAsImmediate(nonBlockingSink);\n}\n"],"mappings":";;;;;;;;;AAwHA,SAAgB,WAAWA,MAAoB;CAC7C,MAAM,IAAI,IAAI,KACZ,KAAK,IAAI,KAAK,aAAa,EAAE,KAAK,UAAU,EAAE,KAAK,SAAS,CAAC;CAE/D,MAAM,SAAS,EAAE,WAAW,IAAI;AAChC,GAAE,WAAW,EAAE,YAAY,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H;CAED,SAAS,kBAAwB;AAC/B,MAAI,eAAe,QAAQ,SAAU;AAErC,eAAa,YAAY,MAAM;AAC7B,kBAAe;EAChB,GAAE,cAAc;CAClB;CAED,MAAMC,kBAA0C,CAACH,WAAsB;AACrE,MAAI,SAAU;AACd,YAAU,UAAU,OAAO;EAE3B,MAAM,oBAAoB,OAAO,UAAU;EAC3C,MAAM,oBAAoB,gBAAgB,KACvC,OAAO,YAAY,sBAAuB;AAE7C,MAAI,qBAAqB,kBACvB,gBAAe;WACN,eAAe,QAAQ,gBAAgB,EAChD,kBAAiB;CAEpB;AAED,iBAAgB,OAAO,gBAAgB,YAAY;AACjD,aAAW;AACX,MAAI,eAAe,MAAM;AACvB,iBAAc,WAAW;AACzB,gBAAa;EACd;AACD,QAAM,aAAa;AACnB,MAAI;AACF,SAAM,aAAa,MAAM,GAAG;EAC7B,QAAO,CAEP;CACF;AAED,QAAO,oBAAoB,gBAAgB;AAC5C"}
|
|
1
|
+
{"version":3,"file":"timefilesink.js","names":["date: Date","interval: TimeRotationInterval","options: object","file: string","options:\n & TimeRotatingFileSinkOptions\n & (\n | TimeRotatingFileSinkDriver<TFile>\n | AsyncTimeRotatingFileSinkDriver<TFile>\n )","currentFilename: string","currentPath: string","currentRotationKey: string","fd: TFile","lastFlushTimestamp: number","lastCleanupTimestamp: number | undefined","buffer: string","files: string[]","fileDate: Date | null","flushBuffer","sink: Sink & Disposable","record: LogRecord","activeFlush: Promise<void> | null","flushTimer: ReturnType<typeof setInterval> | null","nonBlockingSink: Sink & AsyncDisposable"],"sources":["../src/timefilesink.ts"],"sourcesContent":["import {\n defaultTextFormatter,\n type LogRecord,\n type Sink,\n} from \"@logtape/logtape\";\nimport type {\n AsyncFileSinkDriver,\n FileSinkDriver,\n FileSinkOptions,\n} from \"./filesink.base.ts\";\nimport { markSinkAsImmediate } from \"./snapshot.ts\";\n\n/**\n * The rotation interval for time-based file sinks.\n */\nexport type TimeRotationInterval = \"hourly\" | \"daily\" | \"weekly\";\n\n/**\n * Options for the {@link getBaseTimeRotatingFileSink} function.\n */\nexport interface TimeRotatingFileSinkOptions\n extends Omit<FileSinkOptions, \"lazy\"> {\n /**\n * The directory to write log files to.\n */\n directory: string;\n\n /**\n * A function that generates the filename for the log file based on the date.\n * Default depends on `interval`:\n * - `\"daily\"`: `YYYY-MM-DD.log` (e.g., `2025-01-15.log`)\n * - `\"hourly\"`: `YYYY-MM-DD-HH.log` (e.g., `2025-01-15-09.log`)\n * - `\"weekly\"`: `YYYY-WW.log` (e.g., `2025-W03.log`)\n */\n filename?: (date: Date) => string;\n\n /**\n * The rotation interval. Defaults to `\"daily\"`.\n */\n interval?: TimeRotationInterval;\n\n /**\n * The maximum age of log files in milliseconds. Files older than this\n * will be deleted. If not specified, old files are not deleted.\n *\n * When using `getTimeRotatingFileSink()` with `filename` set, cleanup uses\n * each file's modification time instead of parsing dates from filenames.\n */\n maxAgeMs?: number;\n}\n\n/**\n * A platform-specific time-rotating file sink driver.\n */\nexport interface TimeRotatingFileSinkDriver<TFile>\n extends FileSinkDriver<TFile> {\n /**\n * Read the contents of a directory.\n * @param path A path to the directory.\n * @returns An array of filenames in the directory.\n */\n readdirSync(path: string): string[];\n\n /**\n * Delete a file.\n * @param path A path to the file to delete.\n */\n unlinkSync(path: string): void;\n\n /**\n * Create a directory if it doesn't exist.\n * @param path A path to the directory to create.\n * @param options Options for directory creation.\n */\n mkdirSync(path: string, options?: { recursive?: boolean }): void;\n\n /**\n * Join path segments.\n * @param paths Path segments to join.\n * @returns The joined path.\n */\n joinPath(...paths: string[]): string;\n}\n\n/**\n * A platform-specific async time-rotating file sink driver.\n * @since 2.0.0\n */\nexport interface AsyncTimeRotatingFileSinkDriver<TFile>\n extends AsyncFileSinkDriver<TFile> {\n /**\n * Read the contents of a directory.\n * @param path A path to the directory.\n * @returns An array of filenames in the directory.\n */\n readdirSync(path: string): string[];\n\n /**\n * Delete a file.\n * @param path A path to the file to delete.\n */\n unlinkSync(path: string): void;\n\n /**\n * Create a directory if it doesn't exist.\n * @param path A path to the directory to create.\n * @param options Options for directory creation.\n */\n mkdirSync(path: string, options?: { recursive?: boolean }): void;\n\n /**\n * Join path segments.\n * @param paths Path segments to join.\n * @returns The joined path.\n */\n joinPath(...paths: string[]): string;\n}\n\n/**\n * Get the ISO week number of a date.\n * @param date The date to get the week number of.\n * @returns The ISO week number (1-53).\n */\nexport function getISOWeek(date: Date): number {\n const d = new Date(\n Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),\n );\n const dayNum = d.getUTCDay() || 7;\n d.setUTCDate(d.getUTCDate() + 4 - dayNum);\n const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));\n return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);\n}\n\n/**\n * Get the ISO week year of a date. This may differ from the calendar year\n * for dates near the start or end of a year.\n * @param date The date to get the ISO week year of.\n * @returns The ISO week year.\n */\nexport function getISOWeekYear(date: Date): number {\n const d = new Date(\n Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),\n );\n const dayNum = d.getUTCDay() || 7;\n d.setUTCDate(d.getUTCDate() + 4 - dayNum);\n return d.getUTCFullYear();\n}\n\n/**\n * Get the default filename generator for the given interval.\n * @param interval The rotation interval.\n * @returns A function that generates a filename for a given date.\n */\nexport function getDefaultFilename(\n interval: TimeRotationInterval,\n): (date: Date) => string {\n switch (interval) {\n case \"hourly\":\n return (date: Date): string => {\n const yyyy = date.getFullYear();\n const mm = String(date.getMonth() + 1).padStart(2, \"0\");\n const dd = String(date.getDate()).padStart(2, \"0\");\n const hh = String(date.getHours()).padStart(2, \"0\");\n return `${yyyy}-${mm}-${dd}-${hh}.log`;\n };\n case \"daily\":\n return (date: Date): string => {\n const yyyy = date.getFullYear();\n const mm = String(date.getMonth() + 1).padStart(2, \"0\");\n const dd = String(date.getDate()).padStart(2, \"0\");\n return `${yyyy}-${mm}-${dd}.log`;\n };\n case \"weekly\":\n return (date: Date): string => {\n const yyyy = getISOWeekYear(date);\n const week = getISOWeek(date);\n return `${yyyy}-W${String(week).padStart(2, \"0\")}.log`;\n };\n }\n}\n\n/**\n * Get the rotation key for the given date and interval.\n * The key is used to determine when to rotate to a new file.\n * @param date The date to get the rotation key of.\n * @param interval The rotation interval.\n * @returns A string key that changes when rotation should occur.\n */\nfunction getRotationKey(date: Date, interval: TimeRotationInterval): string {\n switch (interval) {\n case \"hourly\":\n return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}-${date.getHours()}`;\n case \"daily\":\n return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;\n case \"weekly\":\n return `${getISOWeekYear(date)}-${getISOWeek(date)}`;\n }\n}\n\nfunction getRotationIntervalMs(interval: TimeRotationInterval): number {\n switch (interval) {\n case \"hourly\":\n return 60 * 60 * 1000;\n case \"daily\":\n return 24 * 60 * 60 * 1000;\n case \"weekly\":\n return 7 * 24 * 60 * 60 * 1000;\n }\n}\n\ninterface StatSyncDriver {\n statSync(path: string): { mtime: Date | null };\n}\n\nfunction hasStatSyncDriver(options: object): options is StatSyncDriver {\n return \"statSync\" in options && typeof options.statSync === \"function\";\n}\n\nfunction parseDefaultFilename(file: string): Date | null {\n const dateMatch = file.match(\n /^(\\d{4})-(\\d{2})-(\\d{2})(?:-(\\d{2}))?\\.log$/,\n );\n const weekMatch = file.match(/^(\\d{4})-W(\\d{2})\\.log$/);\n\n if (dateMatch) {\n const [, year, month, day, hour] = dateMatch;\n return new Date(\n parseInt(year!, 10),\n parseInt(month!, 10) - 1,\n parseInt(day!, 10),\n hour ? parseInt(hour, 10) : 0,\n );\n }\n\n if (weekMatch) {\n const [, year, week] = weekMatch;\n const jan4 = new Date(parseInt(year!, 10), 0, 4);\n const dayOfWeek = jan4.getDay() || 7;\n const fileDate = new Date(jan4);\n fileDate.setDate(\n jan4.getDate() - dayOfWeek + 1 + (parseInt(week!, 10) - 1) * 7,\n );\n return fileDate;\n }\n\n return null;\n}\n\n/**\n * Get a platform-independent time-rotating file sink.\n *\n * This sink writes log records to a file in a directory, rotating to a new\n * file based on time intervals. The filename is generated based on the\n * current date/time and the configured interval.\n *\n * @template TFile The type of the file descriptor.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed. If `nonBlocking` is\n * enabled, returns a sink that also implements {@link AsyncDisposable}.\n */\nexport function getBaseTimeRotatingFileSink<TFile>(\n options: TimeRotatingFileSinkOptions & TimeRotatingFileSinkDriver<TFile>,\n): Sink & Disposable;\nexport function getBaseTimeRotatingFileSink<TFile>(\n options: TimeRotatingFileSinkOptions & AsyncTimeRotatingFileSinkDriver<TFile>,\n): Sink & AsyncDisposable;\nexport function getBaseTimeRotatingFileSink<TFile>(\n options:\n & TimeRotatingFileSinkOptions\n & (\n | TimeRotatingFileSinkDriver<TFile>\n | AsyncTimeRotatingFileSinkDriver<TFile>\n ),\n): Sink & (Disposable | AsyncDisposable) {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const interval = options.interval ?? \"daily\";\n const hasCustomFilename = options.filename !== undefined;\n const filenameGenerator = options.filename ?? getDefaultFilename(interval);\n const maxAgeMs = options.maxAgeMs;\n const cleanupInterval = maxAgeMs === undefined\n ? getRotationIntervalMs(interval)\n : Math.min(getRotationIntervalMs(interval), maxAgeMs);\n const bufferSize = options.bufferSize ?? 1024 * 8;\n const flushInterval = options.flushInterval ?? 5000;\n const directory = options.directory;\n const statSync = hasStatSyncDriver(options) ? options.statSync : undefined;\n\n // Ensure directory exists\n try {\n options.mkdirSync(directory, { recursive: true });\n } catch {\n // Directory might already exist\n }\n\n let currentFilename: string = filenameGenerator(new Date());\n let currentPath: string = options.joinPath(directory, currentFilename);\n let currentRotationKey: string = getRotationKey(new Date(), interval);\n let fd: TFile = options.openSync(currentPath);\n let lastFlushTimestamp: number = Date.now();\n let lastCleanupTimestamp: number | undefined;\n let buffer: string = \"\";\n\n function shouldRotate(): boolean {\n const now = new Date();\n const newKey = getRotationKey(now, interval);\n return newKey !== currentRotationKey;\n }\n\n function performRotation(): void {\n options.closeSync(fd);\n const now = new Date();\n currentFilename = filenameGenerator(now);\n currentPath = options.joinPath(directory, currentFilename);\n currentRotationKey = getRotationKey(now, interval);\n fd = options.openSync(currentPath);\n }\n\n function cleanupOldFiles(): void {\n if (maxAgeMs === undefined) return;\n\n const now = Date.now();\n if (\n lastCleanupTimestamp !== undefined && cleanupInterval > 0 &&\n now - lastCleanupTimestamp < cleanupInterval\n ) {\n return;\n }\n lastCleanupTimestamp = now;\n\n let files: string[];\n try {\n files = options.readdirSync(directory);\n } catch {\n return;\n }\n\n for (const file of files) {\n if (file === currentFilename) continue;\n\n let fileDate: Date | null = null;\n if (hasCustomFilename) {\n if (statSync === undefined) continue;\n try {\n fileDate = statSync(options.joinPath(directory, file)).mtime;\n } catch {\n continue;\n }\n if (fileDate === null || filenameGenerator(fileDate) !== file) {\n continue;\n }\n } else {\n if (!file.endsWith(\".log\")) continue;\n fileDate = parseDefaultFilename(file);\n }\n\n if (fileDate && now - fileDate.getTime() > maxAgeMs) {\n const filePath = options.joinPath(directory, file);\n try {\n options.unlinkSync(filePath);\n } catch {\n // Ignore errors when deleting files\n }\n }\n }\n }\n\n if (!options.nonBlocking) {\n // Blocking mode implementation\n // deno-lint-ignore no-inner-declarations\n function flushBuffer(): void {\n if (buffer.length > 0) {\n if (shouldRotate()) {\n performRotation();\n }\n const bytes = encoder.encode(buffer);\n buffer = \"\";\n options.writeSync(fd, bytes);\n options.flushSync(fd);\n lastFlushTimestamp = Date.now();\n cleanupOldFiles();\n }\n }\n\n const sink: Sink & Disposable = (record: LogRecord) => {\n buffer += formatter(record);\n\n const shouldFlushBySize = buffer.length >= bufferSize;\n const shouldFlushByTime = flushInterval > 0 &&\n (record.timestamp - lastFlushTimestamp) >= flushInterval;\n\n if (shouldFlushBySize || shouldFlushByTime) {\n flushBuffer();\n }\n };\n\n sink[Symbol.dispose] = () => {\n flushBuffer();\n options.closeSync(fd);\n };\n\n return markSinkAsImmediate(sink);\n }\n\n // Non-blocking mode implementation\n const asyncOptions = options as AsyncTimeRotatingFileSinkDriver<TFile>;\n let disposed = false;\n let activeFlush: Promise<void> | null = null;\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n\n async function flushBuffer(): Promise<void> {\n if (buffer.length === 0) return;\n\n if (shouldRotate()) {\n performRotation();\n }\n\n const data = buffer;\n buffer = \"\";\n try {\n const bytes = encoder.encode(data);\n asyncOptions.writeSync(fd, bytes);\n await asyncOptions.flush(fd);\n lastFlushTimestamp = Date.now();\n cleanupOldFiles();\n } catch {\n // Silently ignore errors in non-blocking mode\n }\n }\n\n function scheduleFlush(): void {\n if (activeFlush || disposed) return;\n\n activeFlush = flushBuffer().finally(() => {\n activeFlush = null;\n });\n }\n\n function startFlushTimer(): void {\n if (flushTimer !== null || disposed) return;\n\n flushTimer = setInterval(() => {\n scheduleFlush();\n }, flushInterval);\n }\n\n const nonBlockingSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n buffer += formatter(record);\n\n const shouldFlushBySize = buffer.length >= bufferSize;\n const shouldFlushByTime = flushInterval > 0 &&\n (record.timestamp - lastFlushTimestamp) >= flushInterval;\n\n if (shouldFlushBySize || shouldFlushByTime) {\n scheduleFlush();\n } else if (flushTimer === null && flushInterval > 0) {\n startFlushTimer();\n }\n };\n\n nonBlockingSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n await flushBuffer();\n try {\n await asyncOptions.close(fd);\n } catch {\n // Writer might already be closed or errored\n }\n };\n\n return markSinkAsImmediate(nonBlockingSink);\n}\n"],"mappings":";;;;;;;;;AA2HA,SAAgB,WAAWA,MAAoB;CAC7C,MAAM,IAAI,IAAI,KACZ,KAAK,IAAI,KAAK,aAAa,EAAE,KAAK,UAAU,EAAE,KAAK,SAAS,CAAC;CAE/D,MAAM,SAAS,EAAE,WAAW,IAAI;AAChC,GAAE,WA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rB,IAAI,WAAW;CACf,IAAIC,cAAoC;CACxC,IAAIC,aAAoD;CAExD,eAAe,cAA6B;AAC1C,MAAI,OAAO,WAAW,EAAG;AAEzB,MAAI,cAAc,CAChB,kBAAiB;EAGnB,MAAM,OAAO;AACb,WAAS;AACT,MAAI;GACF,MAAM,QAAQ,QAAQ,OAAO,KAAK;AAClC,gBAAa,UAAU,IAAI,MAAM;AACjC,SAAM,aAAa,MAAM,GAAG;AAC5B,wBAAqB,KAAK,KAAK;AAC/B,oBAAiB;EAClB,QAAO,CAEP;CACF;CAED,SAAS,gBAAsB;AAC7B,MAAI,eAAe,SAAU;AAE7B,gBAAc,aAAa,CAAC,QAAQ,MAAM;AACxC,iBAAc;EACf,EAAC;CACH;CAED,SAAS,kBAAwB;AAC/B,MAAI,eAAe,QAAQ,SAAU;AAErC,eAAa,YAAY,MAAM;AAC7B,kBAAe;EAChB,GAAE,cAAc;CAClB;CAED,MAAMC,kBAA0C,CAACH,WAAsB;AACrE,MAAI,SAAU;AACd,YAAU,UAAU,OAAO;EAE3B,MAAM,oBAAoB,OAAO,UAAU;EAC3C,MAAM,oBAAoB,gBAAgB,KACvC,OAAO,YAAY,sBAAuB;AAE7C,MAAI,qBAAqB,kBACvB,gBAAe;WACN,eAAe,QAAQ,gBAAgB,EAChD,kBAAiB;CAEpB;AAED,iBAAgB,OAAO,gBAAgB,YAAY;AACjD,aAAW;AACX,MAAI,eAAe,MAAM;AACvB,iBAAc,WAAW;AACzB,gBAAa;EACd;AACD,QAAM,aAAa;AACnB,MAAI;AACF,SAAM,aAAa,MAAM,GAAG;EAC7B,QAAO,CAEP;CACF;AAED,QAAO,oBAAoB,gBAAgB;AAC5C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logtape/file",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.2",
|
|
4
4
|
"description": "File sink and rotating file sink for LogTape",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"logging",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"dist/"
|
|
61
61
|
],
|
|
62
62
|
"peerDependencies": {
|
|
63
|
-
"@logtape/logtape": "^2.2.
|
|
63
|
+
"@logtape/logtape": "^2.2.2"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@alinea/suite": "^0.6.3",
|