@logtape/file
Version:
File sink and rotating file sink for LogTape
114 lines (111 loc) • 3.57 kB
TypeScript
import { Sink, StreamSinkOptions } from "@logtape/logtape";
//#region filesink.base.d.ts
/**
* Options for the {@link getBaseFileSink} function.
*/
type FileSinkOptions = StreamSinkOptions & {
/**
* If `true`, the file is not opened until the first write. Defaults to `false`.
*/
lazy?: boolean;
/**
* The size of the buffer to use when writing to the file. If not specified,
* a default buffer size will be used. If it is less or equal to 0,
* the file will be written directly without buffering.
* @default 8192
* @since 0.12.0
*/
bufferSize?: number;
/**
* The maximum time interval in milliseconds between flushes. If this time
* passes since the last flush, the buffer will be flushed regardless of size.
* This helps prevent log loss during unexpected process termination.
* @default 5000
* @since 0.12.0
*/
flushInterval?: number;
};
/**
* A platform-specific file sink driver.
* @typeParam TFile The type of the file descriptor.
*/
interface FileSinkDriver<TFile> {
/**
* Open a file for appending and return a file descriptor.
* @param path A path to the file to open.
*/
openSync(path: string): TFile;
/**
* Write a chunk of data to the file.
* @param fd The file descriptor.
* @param chunk The data to write.
*/
writeSync(fd: TFile, chunk: Uint8Array): void;
/**
* Flush the file to ensure that all data is written to the disk.
* @param fd The file descriptor.
*/
flushSync(fd: TFile): void;
/**
* Close the file.
* @param fd The file descriptor.
*/
closeSync(fd: TFile): void;
}
/**
* Get a platform-independent file sink.
*
* @typeParam TFile The type of the file descriptor.
* @param path A path to the file to write to.
* @param options The options for the sink and the file driver.
* @returns A sink that writes to the file. The sink is also a disposable
* object that closes the file when disposed.
*/
/**
* Options for the {@link getBaseRotatingFileSink} function.
*/
interface RotatingFileSinkOptions extends Omit<FileSinkOptions, "lazy"> {
/**
* The maximum bytes of the file before it is rotated. 1 MiB by default.
*/
maxSize?: number;
/**
* The maximum number of files to keep. 5 by default.
*/
maxFiles?: number;
}
/**
* A platform-specific rotating file sink driver.
*/
interface RotatingFileSinkDriver<TFile> extends FileSinkDriver<TFile> {
/**
* Get the size of the file.
* @param path A path to the file.
* @returns The `size` of the file in bytes, in an object.
*/
statSync(path: string): {
size: number;
};
/**
* Rename a file.
* @param oldPath A path to the file to rename.
* @param newPath A path to be renamed to.
*/
renameSync(oldPath: string, newPath: string): void;
}
/**
* Get a platform-independent rotating file sink.
*
* This sink writes log records to a file, and rotates the file when it reaches
* the `maxSize`. The rotated files are named with the original file name
* followed by a dot and a number, starting from 1. The number is incremented
* for each rotation, and the maximum number of files to keep is `maxFiles`.
*
* @param path A path to the file to write to.
* @param options The options for the sink and the file driver.
* @returns A sink that writes to the file. The sink is also a disposable
* object that closes the file when disposed.
*/
//#endregion
export { FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions };
//# sourceMappingURL=filesink.base.d.ts.map