deno.land / std@0.224.0 / log / rotating_file_handler_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, assertNotEquals, assertThrows,} from "../assert/mod.ts";import { LogLevels } from "./levels.ts";import { RotatingFileHandler } from "./rotating_file_handler.ts";import { LogRecord } from "./logger.ts";import { existsSync } from "../fs/exists.ts";
const LOG_FILE = "./rotating_file_handler_test_log.file";
Deno.test({ name: "RotatingFileHandler wipes existing log file clean and removes others with mode 'w'", async fn() { Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("hello world")); Deno.writeFileSync( LOG_FILE + ".1", new TextEncoder().encode("hello world"), ); Deno.writeFileSync( LOG_FILE + ".2", new TextEncoder().encode("hello world"), ); Deno.writeFileSync( LOG_FILE + ".3", new TextEncoder().encode("hello world"), );
const fileHandler = new RotatingFileHandler("WARN", { filename: LOG_FILE, maxBytes: 50, maxBackupCount: 3, mode: "w", }); fileHandler.setup(); fileHandler.destroy();
assertEquals((await Deno.stat(LOG_FILE)).size, 0); assert(!existsSync(LOG_FILE + ".1")); assert(!existsSync(LOG_FILE + ".2")); assert(!existsSync(LOG_FILE + ".3"));
Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "RotatingFileHandler throws if any log file already exists with mode 'x'", fn() { Deno.writeFileSync( LOG_FILE + ".3", new TextEncoder().encode("hello world"), ); using fileHandler = new RotatingFileHandler("WARN", { filename: LOG_FILE, maxBytes: 50, maxBackupCount: 3, mode: "x", }); assertThrows( () => { fileHandler.setup(); }, Deno.errors.AlreadyExists, "Backup log file " + LOG_FILE + ".3 already exists", );
Deno.removeSync(LOG_FILE + ".3"); Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "RotatingFileHandler handles first rollover, monitor step by step", async fn() { using fileHandler = new RotatingFileHandler("WARN", { filename: LOG_FILE, maxBytes: 25, maxBackupCount: 3, mode: "w", }); fileHandler.setup();
fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); // 'ERROR AAA\n' = 10 bytes fileHandler.flush(); assertEquals((await Deno.stat(LOG_FILE)).size, 10); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); fileHandler.flush(); assertEquals((await Deno.stat(LOG_FILE)).size, 20); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); fileHandler.flush(); // Rollover occurred. Log file now has 1 record, rollover file has the original 2 assertEquals((await Deno.stat(LOG_FILE)).size, 10); assertEquals((await Deno.stat(LOG_FILE + ".1")).size, 20);
Deno.removeSync(LOG_FILE); Deno.removeSync(LOG_FILE + ".1"); },});
Deno.test({ name: "RotatingFileHandler handles first rollover, check all at once", async fn() { const fileHandler = new RotatingFileHandler("WARN", { filename: LOG_FILE, maxBytes: 25, maxBackupCount: 3, mode: "w", }); fileHandler.setup();
fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); // 'ERROR AAA\n' = 10 bytes fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), );
fileHandler.destroy();
assertEquals((await Deno.stat(LOG_FILE)).size, 10); assertEquals((await Deno.stat(LOG_FILE + ".1")).size, 20);
Deno.removeSync(LOG_FILE); Deno.removeSync(LOG_FILE + ".1"); },});
Deno.test({ name: "RotatingFileHandler handles all backups rollover", fn() { Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("original log file")); Deno.writeFileSync( LOG_FILE + ".1", new TextEncoder().encode("original log.1 file"), ); Deno.writeFileSync( LOG_FILE + ".2", new TextEncoder().encode("original log.2 file"), ); Deno.writeFileSync( LOG_FILE + ".3", new TextEncoder().encode("original log.3 file"), );
const fileHandler = new RotatingFileHandler("WARN", { filename: LOG_FILE, maxBytes: 2, maxBackupCount: 3, mode: "a", }); fileHandler.setup(); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); // 'ERROR AAA\n' = 10 bytes fileHandler.destroy();
const decoder = new TextDecoder(); assertEquals(decoder.decode(Deno.readFileSync(LOG_FILE)), "ERROR AAA\n"); assertEquals( decoder.decode(Deno.readFileSync(LOG_FILE + ".1")), "original log file", ); assertEquals( decoder.decode(Deno.readFileSync(LOG_FILE + ".2")), "original log.1 file", ); assertEquals( decoder.decode(Deno.readFileSync(LOG_FILE + ".3")), "original log.2 file", ); assert(!existsSync(LOG_FILE + ".4"));
Deno.removeSync(LOG_FILE); Deno.removeSync(LOG_FILE + ".1"); Deno.removeSync(LOG_FILE + ".2"); Deno.removeSync(LOG_FILE + ".3"); },});
Deno.test({ name: "RotatingFileHandler handles maxBytes less than 1", fn() { assertThrows( () => { const fileHandler = new RotatingFileHandler("WARN", { filename: LOG_FILE, maxBytes: 0, maxBackupCount: 3, mode: "w", }); fileHandler.setup(); }, Error, "maxBytes cannot be less than 1", ); },});
Deno.test({ name: "RotatingFileHandler handles maxBackupCount less than 1", fn() { assertThrows( () => { const fileHandler = new RotatingFileHandler("WARN", { filename: LOG_FILE, maxBytes: 50, maxBackupCount: 0, mode: "w", }); fileHandler.setup(); }, Error, "maxBackupCount cannot be less than 1", ); },});
Deno.test({ name: "RotatingFileHandler rotates on byte length, not msg length", async fn() { const fileHandler = new RotatingFileHandler("WARN", { filename: LOG_FILE, maxBytes: 7, maxBackupCount: 1, mode: "w", }); fileHandler.setup();
const msg = "。"; const msgLength = msg.length; const msgByteLength = new TextEncoder().encode(msg).byteLength; assertNotEquals(msgLength, msgByteLength); assertEquals(msgLength, 1); assertEquals(msgByteLength, 3);
fileHandler.log(msg); // logs 4 bytes (including '\n') fileHandler.log(msg); // max bytes is 7, but this would be 8. Rollover.
fileHandler.destroy();
const fileSize1 = (await Deno.stat(LOG_FILE)).size; const fileSize2 = (await Deno.stat(LOG_FILE + ".1")).size;
assertEquals(fileSize1, msgByteLength + 1); assertEquals(fileSize2, msgByteLength + 1);
Deno.removeSync(LOG_FILE); 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); },});
Version Info