getStreamFileSink(path: string,options?: StreamFileSinkOptions,): Sink & Disposable
Create a high-performance stream-based file sink that writes log records to a file.
This sink uses Node.js PassThrough streams piped to WriteStreams for optimal I/O performance. It leverages the Node.js stream infrastructure to provide automatic backpressure management, efficient buffering, and asynchronous writes without blocking the main thread.
Performance Characteristics
- High Performance: Optimized for high-volume logging scenarios
- Non-blocking: Uses asynchronous I/O that doesn't block the main thread
- Memory Efficient: Automatic backpressure prevents memory buildup
- Stream-based: Leverages Node.js native stream optimizations
When to Use
Use this sink when you need:
- High-performance file logging for production applications
- Non-blocking I/O behavior for real-time applications
- Automatic backpressure handling for high-volume scenarios
- Simple file output without complex buffering configuration
For more control over buffering behavior, consider using getFileSink instead, which provides options for buffer size, flush intervals, and non-blocking modes.
Example
import { configure } from "@logtape/logtape"; import { getStreamFileSink } from "@logtape/file"; await configure({ sinks: { file: getStreamFileSink("app.log", { highWaterMark: 32768 // 32KB buffer for high-volume logging }) }, loggers: [ { category: ["myapp"], sinks: ["file"] } ] });
path: string
The path to the file to write logs to. The file will be created if it doesn't exist, or appended to if it does exist.
options: StreamFileSinkOptions
Configuration options for the stream-based sink.