Skip to content

feat(log): make FileHandler and RotatingFileHandler buffer size configurable #4680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions log/file_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import type { LogRecord } from "./logger.ts";
import { BaseHandler, type BaseHandlerOptions } from "./base_handler.ts";
import { writeAllSync } from "@std/io/write-all";

const PAGE_SIZE = 4096;
export type LogMode = "a" | "w" | "x";

export interface FileHandlerOptions extends BaseHandlerOptions {
filename: string;
mode?: LogMode;
/**
* Buffer size for writing log messages to file, in bytes.
*
* @default {4096}
*/
bufferSize?: number;
}

/**
Expand All @@ -32,7 +37,7 @@ export interface FileHandlerOptions extends BaseHandlerOptions {
*/
export class FileHandler extends BaseHandler {
protected _file: Deno.FsFile | undefined;
protected _buf: Uint8Array = new Uint8Array(PAGE_SIZE);
protected _buf: Uint8Array;
protected _pointer = 0;
protected _filename: string;
protected _mode: LogMode;
Expand All @@ -54,6 +59,7 @@ export class FileHandler extends BaseHandler {
truncate: this._mode !== "a",
write: true,
};
this._buf = new Uint8Array(options.bufferSize ?? 4096);
}

override setup() {
Expand Down