Skip to content
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

fix(log): rotating file handler sync setup and destroy #3543

Merged
merged 4 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions log/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { getLevelByName, LevelName, LogLevels } from "./levels.ts";
import type { LogRecord } from "./logger.ts";
import { blue, bold, red, yellow } from "../fmt/colors.ts";
import { exists, existsSync } from "../fs/exists.ts";
import { existsSync } from "../fs/exists.ts";
import { BufWriterSync } from "../io/buf_writer.ts";
import type { Writer } from "../types.d.ts";

Expand Down Expand Up @@ -235,7 +235,7 @@ export class RotatingFileHandler extends FileHandler {
this.#maxBackupCount = options.maxBackupCount;
}

override async setup() {
override setup() {
if (this.#maxBytes < 1) {
this.destroy();
throw new Error("maxBytes cannot be less than 1");
Expand All @@ -244,14 +244,14 @@ export class RotatingFileHandler extends FileHandler {
this.destroy();
throw new Error("maxBackupCount cannot be less than 1");
}
await super.setup();
super.setup();

if (this._mode === "w") {
// Remove old backups too as it doesn't make sense to start with a clean
// log file, but old backups
for (let i = 1; i <= this.#maxBackupCount; i++) {
try {
await Deno.remove(this._filename + "." + i);
Deno.removeSync(this._filename + "." + i);
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
throw error;
Expand All @@ -261,15 +261,15 @@ export class RotatingFileHandler extends FileHandler {
} else if (this._mode === "x") {
// Throw if any backups also exist
for (let i = 1; i <= this.#maxBackupCount; i++) {
if (await exists(this._filename + "." + i)) {
if (existsSync(this._filename + "." + i)) {
this.destroy();
throw new Deno.errors.AlreadyExists(
"Backup log file " + this._filename + "." + i + " already exists",
);
}
}
} else {
this.#currentFileSize = (await Deno.stat(this._filename)).size;
this.#currentFileSize = (Deno.statSync(this._filename)).size;
}
}

Expand Down
82 changes: 41 additions & 41 deletions log/handlers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
assert,
assertEquals,
assertNotEquals,
assertRejects,
assertThrows,
} from "../assert/mod.ts";
import {
getLevelByName,
Expand Down Expand Up @@ -148,7 +148,7 @@ Deno.test("testFormatterAsFunction", function () {

Deno.test({
name: "FileHandler Shouldn't Have Broken line",
async fn() {
fn() {
class TestFileHandler extends FileHandler {
override flush() {
super.flush();
Expand All @@ -158,17 +158,17 @@ Deno.test({
assertEquals(text.slice(-1), "\n");
}

override async destroy() {
await super.destroy();
await Deno.remove(LOG_FILE);
override destroy() {
super.destroy();
Deno.removeSync(LOG_FILE);
}
}

const testFileHandler = new TestFileHandler("WARNING", {
filename: LOG_FILE,
mode: "w",
});
await testFileHandler.setup();
testFileHandler.setup();

for (let i = 0; i < 300; i++) {
testFileHandler.handle(
Expand All @@ -181,7 +181,7 @@ Deno.test({
);
}

await testFileHandler.destroy();
testFileHandler.destroy();
},
});

Expand All @@ -193,7 +193,7 @@ Deno.test({
mode: "w",
});

await fileHandler.setup();
fileHandler.setup();
fileHandler.handle(
new LogRecord({
msg: "Hello World",
Expand All @@ -202,10 +202,10 @@ Deno.test({
loggerName: "default",
}),
);
await fileHandler.destroy();
fileHandler.destroy();
const firstFileSize = (await Deno.stat(LOG_FILE)).size;

await fileHandler.setup();
fileHandler.setup();
fileHandler.handle(
new LogRecord({
msg: "Hello World",
Expand All @@ -214,7 +214,7 @@ Deno.test({
loggerName: "default",
}),
);
await fileHandler.destroy();
fileHandler.destroy();
const secondFileSize = (await Deno.stat(LOG_FILE)).size;

assertEquals(secondFileSize, firstFileSize);
Expand All @@ -224,18 +224,18 @@ Deno.test({

Deno.test({
name: "FileHandler with mode 'x' will throw if log file already exists",
async fn() {
fn() {
const fileHandler = new FileHandler("WARNING", {
filename: LOG_FILE,
mode: "x",
});
Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("hello world"));

await assertRejects(async () => {
await fileHandler.setup();
assertThrows(() => {
fileHandler.setup();
}, Deno.errors.AlreadyExists);

await fileHandler.destroy();
fileHandler.destroy();

Deno.removeSync(LOG_FILE);
},
Expand Down Expand Up @@ -265,8 +265,8 @@ Deno.test({
maxBackupCount: 3,
mode: "w",
});
await fileHandler.setup();
await fileHandler.destroy();
fileHandler.setup();
fileHandler.destroy();

assertEquals((await Deno.stat(LOG_FILE)).size, 0);
assert(!existsSync(LOG_FILE + ".1"));
Expand All @@ -280,7 +280,7 @@ Deno.test({
Deno.test({
name:
"RotatingFileHandler with mode 'x' will throw if any log file already exists",
async fn() {
fn() {
Deno.writeFileSync(
LOG_FILE + ".3",
new TextEncoder().encode("hello world"),
Expand All @@ -291,9 +291,9 @@ Deno.test({
maxBackupCount: 3,
mode: "x",
});
await assertRejects(
async () => {
await fileHandler.setup();
assertThrows(
() => {
fileHandler.setup();
},
Deno.errors.AlreadyExists,
"Backup log file " + LOG_FILE + ".3 already exists",
Expand All @@ -314,7 +314,7 @@ Deno.test({
maxBackupCount: 3,
mode: "w",
});
await fileHandler.setup();
fileHandler.setup();

fileHandler.handle(
new LogRecord({
Expand Down Expand Up @@ -348,7 +348,7 @@ Deno.test({
// 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);
await fileHandler.destroy();
fileHandler.destroy();

Deno.removeSync(LOG_FILE);
Deno.removeSync(LOG_FILE + ".1");
Expand All @@ -364,7 +364,7 @@ Deno.test({
maxBackupCount: 3,
mode: "w",
});
await fileHandler.setup();
fileHandler.setup();

fileHandler.handle(
new LogRecord({
Expand All @@ -391,7 +391,7 @@ Deno.test({
}),
);

await fileHandler.destroy();
fileHandler.destroy();

assertEquals((await Deno.stat(LOG_FILE)).size, 10);
assertEquals((await Deno.stat(LOG_FILE + ".1")).size, 20);
Expand All @@ -403,7 +403,7 @@ Deno.test({

Deno.test({
name: "RotatingFileHandler with all backups rollover",
async fn() {
fn() {
Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("original log file"));
Deno.writeFileSync(
LOG_FILE + ".1",
Expand All @@ -424,7 +424,7 @@ Deno.test({
maxBackupCount: 3,
mode: "a",
});
await fileHandler.setup();
fileHandler.setup();
fileHandler.handle(
new LogRecord({
msg: "AAA",
Expand All @@ -433,7 +433,7 @@ Deno.test({
loggerName: "default",
}),
); // 'ERROR AAA\n' = 10 bytes
await fileHandler.destroy();
fileHandler.destroy();

const decoder = new TextDecoder();
assertEquals(decoder.decode(Deno.readFileSync(LOG_FILE)), "ERROR AAA\n");
Expand All @@ -460,16 +460,16 @@ Deno.test({

Deno.test({
name: "RotatingFileHandler maxBytes cannot be less than 1",
async fn() {
await assertRejects(
async () => {
fn() {
assertThrows(
() => {
const fileHandler = new RotatingFileHandler("WARNING", {
filename: LOG_FILE,
maxBytes: 0,
maxBackupCount: 3,
mode: "w",
});
await fileHandler.setup();
fileHandler.setup();
},
Error,
"maxBytes cannot be less than 1",
Expand All @@ -479,16 +479,16 @@ Deno.test({

Deno.test({
name: "RotatingFileHandler maxBackupCount cannot be less than 1",
async fn() {
await assertRejects(
async () => {
fn() {
assertThrows(
() => {
const fileHandler = new RotatingFileHandler("WARNING", {
filename: LOG_FILE,
maxBytes: 50,
maxBackupCount: 0,
mode: "w",
});
await fileHandler.setup();
fileHandler.setup();
},
Error,
"maxBackupCount cannot be less than 1",
Expand All @@ -503,7 +503,7 @@ Deno.test({
filename: LOG_FILE,
mode: "w",
});
await fileHandler.setup();
fileHandler.setup();
fileHandler.handle(
new LogRecord({
msg: "AAA",
Expand Down Expand Up @@ -531,7 +531,7 @@ Deno.test({
maxBackupCount: 1,
mode: "w",
});
await fileHandler.setup();
fileHandler.setup();

const msg = "。";
const msgLength = msg.length;
Expand All @@ -543,7 +543,7 @@ Deno.test({
fileHandler.log(msg); // logs 4 bytes (including '\n')
fileHandler.log(msg); // max bytes is 7, but this would be 8. Rollover.

await fileHandler.destroy();
fileHandler.destroy();

const fileSize1 = (await Deno.stat(LOG_FILE)).size;
const fileSize2 = (await Deno.stat(LOG_FILE + ".1")).size;
Expand All @@ -563,7 +563,7 @@ Deno.test({
filename: LOG_FILE,
mode: "w",
});
await fileHandler.setup();
fileHandler.setup();

fileHandler.handle(
new LogRecord({
Expand Down Expand Up @@ -592,7 +592,7 @@ Deno.test({
// ERROR record is 10 bytes, CRITICAL is 13 bytes
assertEquals(fileSize2, 23);

await fileHandler.destroy();
fileHandler.destroy();
Deno.removeSync(LOG_FILE);
},
});
8 changes: 4 additions & 4 deletions log/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
* import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts";
* import { sum } from "<the-awesome-module>/mod.ts";
*
* await log.setup({
* log.setup({
* handlers: {
* console: new log.handlers.ConsoleHandler("DEBUG"),
* },
Expand Down Expand Up @@ -108,7 +108,7 @@
* log.critical("500 Internal server error");
*
* // custom configuration with 2 loggers (the default and `tasks` loggers).
* await log.setup({
* log.setup({
* handlers: {
* console: new log.handlers.ConsoleHandler("DEBUG"),
*
Expand Down Expand Up @@ -157,7 +157,7 @@
* ```ts
* import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts";
*
* await log.setup({
* log.setup({
* handlers: {
* stringFmt: new log.handlers.ConsoleHandler("DEBUG", {
* formatter: "[{levelName}] {msg}",
Expand Down Expand Up @@ -225,7 +225,7 @@
* ```ts
* import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts";
*
* await log.setup({
* log.setup({
* handlers: {
* console: new log.handlers.ConsoleHandler("DEBUG"),
* },
Expand Down