Skip to content

Commit

Permalink
fix(log): don't discard bytes >4096 in FileHandler (#4415)
Browse files Browse the repository at this point in the history
* fix(log): fix error when logging >4096 bytes when using FileHandler or RotatingfileHandler

* trigger ci

* Update log/rotating_file_handler_test.ts

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>

* Update log/rotating_file_handler_test.ts

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>

* Update log/file_handler_test.ts

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>

* Update log/file_handler_test.ts

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
  • Loading branch information
3 people authored Mar 1, 2024
1 parent cb9e70e commit e5e737b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
9 changes: 7 additions & 2 deletions log/file_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { type LevelName, LogLevels } from "./levels.ts";
import type { LogRecord } from "./logger.ts";
import { BaseHandler, type BaseHandlerOptions } from "./base_handler.ts";
import { writeAllSync } from "../io/write_all.ts";

const PAGE_SIZE = 4096;
export type LogMode = "a" | "w" | "x";
Expand Down Expand Up @@ -76,8 +77,12 @@ export class FileHandler extends BaseHandler {
if (bytes.byteLength > this._buf.byteLength - this._pointer) {
this.flush();
}
this._buf.set(bytes, this._pointer);
this._pointer += bytes.byteLength;
if (bytes.byteLength > this._buf.byteLength) {
writeAllSync(this._file!, bytes);
} else {
this._buf.set(bytes, this._pointer);
this._pointer += bytes.byteLength;
}
}

flush() {
Expand Down
47 changes: 47 additions & 0 deletions log/file_handler_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,50 @@ Deno.test({
Deno.removeSync(LOG_FILE);
},
});

Deno.test({
name: "FileHandler handles strings larger than the buffer",
fn() {
const fileHandler = new FileHandler("WARN", {
filename: LOG_FILE,
mode: "w",
});
const logOverBufferLimit = "A".repeat(4096);
fileHandler.setup();

fileHandler.log(logOverBufferLimit);
fileHandler.destroy();

assertEquals(
Deno.readTextFileSync(LOG_FILE),
`${logOverBufferLimit}\n`,
);

Deno.removeSync(LOG_FILE);
},
});

Deno.test({
name: "FileHandler handles a mixture of string sizes",
fn() {
const fileHandler = new FileHandler("WARN", {
filename: LOG_FILE,
mode: "w",
});
const veryLargeLog = "A".repeat(10000);
const regularLog = "B".repeat(100);
fileHandler.setup();

fileHandler.log(regularLog);
fileHandler.log(veryLargeLog);
fileHandler.log(regularLog);
fileHandler.destroy();

assertEquals(
Deno.readTextFileSync(LOG_FILE),
`${regularLog}\n${veryLargeLog}\n${regularLog}\n`,
);

Deno.removeSync(LOG_FILE);
},
});
51 changes: 51 additions & 0 deletions log/rotating_file_handler_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,54 @@ Deno.test({
Deno.removeSync(LOG_FILE + ".1");
},
});

Deno.test({
name: "RotatingFileHandler handles strings larger than the buffer",
fn() {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
mode: "w",
maxBytes: 4000000,
maxBackupCount: 10,
});
const logOverBufferLimit = "A".repeat(4096);
fileHandler.setup();

fileHandler.log(logOverBufferLimit);
fileHandler.destroy();

assertEquals(
Deno.readTextFileSync(LOG_FILE),
`${logOverBufferLimit}\n`,
);

Deno.removeSync(LOG_FILE);
},
});

Deno.test({
name: "RotatingFileHandler handles a mixture of string sizes",
fn() {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
mode: "w",
maxBytes: 4000000,
maxBackupCount: 10,
});
const veryLargeLog = "A".repeat(10000);
const regularLog = "B".repeat(100);
fileHandler.setup();

fileHandler.log(regularLog);
fileHandler.log(veryLargeLog);
fileHandler.log(regularLog);
fileHandler.destroy();

assertEquals(
Deno.readTextFileSync(LOG_FILE),
`${regularLog}\n${veryLargeLog}\n${regularLog}\n`,
);

Deno.removeSync(LOG_FILE);
},
});

0 comments on commit e5e737b

Please sign in to comment.