summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--std/log/handlers.ts13
-rw-r--r--std/log/handlers_test.ts33
2 files changed, 42 insertions, 4 deletions
diff --git a/std/log/handlers.ts b/std/log/handlers.ts
index ffcd846cc..3c8dd6896 100644
--- a/std/log/handlers.ts
+++ b/std/log/handlers.ts
@@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-const { open, openSync, close, renameSync, statSync } = Deno;
+const { open, openSync, close, renameSync, stat } = Deno;
type File = Deno.File;
type Writer = Deno.Writer;
type OpenOptions = Deno.OpenOptions;
@@ -142,6 +142,8 @@ interface RotatingFileHandlerOptions extends FileHandlerOptions {
export class RotatingFileHandler extends FileHandler {
#maxBytes: number;
#maxBackupCount: number;
+ #currentFileSize = 0;
+ #encoder = new TextEncoder();
constructor(levelName: LevelName, options: RotatingFileHandlerOptions) {
super(levelName, options);
@@ -176,6 +178,8 @@ export class RotatingFileHandler extends FileHandler {
);
}
}
+ } else {
+ this.#currentFileSize = (await stat(this._filename)).size;
}
}
@@ -183,9 +187,12 @@ export class RotatingFileHandler extends FileHandler {
if (this.level > logRecord.level) return;
const msg = this.format(logRecord);
- const currentFileSize = statSync(this._filename).size;
- if (currentFileSize + msg.length > this.#maxBytes) {
+ const msgByteLength = this.#encoder.encode(msg).byteLength + 1;
+ if (this.#currentFileSize + msgByteLength > this.#maxBytes) {
this.rotateLogFiles();
+ this.#currentFileSize = msgByteLength;
+ } else {
+ this.#currentFileSize += msgByteLength;
}
return this.log(msg);
diff --git a/std/log/handlers_test.ts b/std/log/handlers_test.ts
index c8b76347f..5275a5ca0 100644
--- a/std/log/handlers_test.ts
+++ b/std/log/handlers_test.ts
@@ -1,6 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
const { test } = Deno;
-import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
+import {
+ assert,
+ assertEquals,
+ assertThrowsAsync,
+ assertNotEquals,
+} from "../testing/asserts.ts";
import {
LogLevels,
LogLevelNames,
@@ -303,3 +308,29 @@ test({
);
},
});
+
+test({
+ name: "RotatingFileHandler fileSize equal to bytelength of message + 1",
+ async fn() {
+ const fileHandler = new RotatingFileHandler("WARNING", {
+ filename: LOG_FILE,
+ maxBytes: 100,
+ maxBackupCount: 1,
+ mode: "w",
+ });
+ await fileHandler.setup();
+
+ const msg = "。";
+ const msgLength = msg.length;
+ const msgByteLength = new TextEncoder().encode(msg).byteLength;
+ await fileHandler.log(msg);
+ const fileSzie = (await Deno.stat(LOG_FILE)).size;
+
+ assertEquals(fileSzie, msgByteLength + 1);
+ assertNotEquals(fileSzie, msgLength);
+ assertNotEquals(fileSzie, msgLength + 1);
+
+ await fileHandler.destroy();
+ Deno.removeSync(LOG_FILE);
+ },
+});