summaryrefslogtreecommitdiff
path: root/std/log
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2020-06-12 20:23:38 +0100
committerGitHub <noreply@github.com>2020-06-12 15:23:38 -0400
commit1fff6f55c3ba98a10018c6d374795e612061e9b6 (patch)
tree12074b6d44736b11513d857e437f9e30a6bf65a4 /std/log
parent26bf56afdaf16634ffbaa23684faf3a44cc10f62 (diff)
refactor: Don't destructure the Deno namespace (#6268)
Diffstat (limited to 'std/log')
-rw-r--r--std/log/handlers.ts20
-rw-r--r--std/log/handlers_test.ts31
-rw-r--r--std/log/logger_test.ts233
-rw-r--r--std/log/mod_test.ts5
-rw-r--r--std/log/test.ts11
5 files changed, 151 insertions, 149 deletions
diff --git a/std/log/handlers.ts b/std/log/handlers.ts
index 5e72ff582..1b152c273 100644
--- a/std/log/handlers.ts
+++ b/std/log/handlers.ts
@@ -1,8 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-const { open, openSync, close, renameSync, stat } = Deno;
-type File = Deno.File;
-type Writer = Deno.Writer;
-type OpenOptions = Deno.OpenOptions;
import { getLevelByName, LevelName, LogLevels } from "./levels.ts";
import { LogRecord } from "./logger.ts";
import { red, yellow, blue, bold } from "../fmt/colors.ts";
@@ -88,7 +84,7 @@ export class ConsoleHandler extends BaseHandler {
}
export abstract class WriterHandler extends BaseHandler {
- protected _writer!: Writer;
+ protected _writer!: Deno.Writer;
#encoder = new TextEncoder();
abstract log(msg: string): void;
@@ -100,11 +96,11 @@ interface FileHandlerOptions extends HandlerOptions {
}
export class FileHandler extends WriterHandler {
- protected _file: File | undefined;
+ protected _file: Deno.File | undefined;
protected _buf!: BufWriterSync;
protected _filename: string;
protected _mode: LogMode;
- protected _openOptions: OpenOptions;
+ protected _openOptions: Deno.OpenOptions;
protected _encoder = new TextEncoder();
#unloadCallback = (): Promise<void> => this.destroy();
@@ -123,7 +119,7 @@ export class FileHandler extends WriterHandler {
}
async setup(): Promise<void> {
- this._file = await open(this._filename, this._openOptions);
+ this._file = await Deno.open(this._filename, this._openOptions);
this._writer = this._file;
this._buf = new BufWriterSync(this._file);
@@ -204,7 +200,7 @@ export class RotatingFileHandler extends FileHandler {
}
}
} else {
- this.#currentFileSize = (await stat(this._filename)).size;
+ this.#currentFileSize = (await Deno.stat(this._filename)).size;
}
}
@@ -222,18 +218,18 @@ export class RotatingFileHandler extends FileHandler {
rotateLogFiles(): void {
this._buf.flush();
- close(this._file!.rid);
+ Deno.close(this._file!.rid);
for (let i = this.#maxBackupCount - 1; i >= 0; i--) {
const source = this._filename + (i === 0 ? "" : "." + i);
const dest = this._filename + "." + (i + 1);
if (existsSync(source)) {
- renameSync(source, dest);
+ Deno.renameSync(source, dest);
}
}
- this._file = openSync(this._filename, this._openOptions);
+ this._file = Deno.openSync(this._filename, this._openOptions);
this._writer = this._file;
this._buf = new BufWriterSync(this._file);
}
diff --git a/std/log/handlers_test.ts b/std/log/handlers_test.ts
index cb73fa56f..f7714dae3 100644
--- a/std/log/handlers_test.ts
+++ b/std/log/handlers_test.ts
@@ -1,5 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-const { test } = Deno;
import {
assert,
assertEquals,
@@ -27,7 +26,7 @@ class TestHandler extends BaseHandler {
}
}
-test("simpleHandler", function (): void {
+Deno.test("simpleHandler", function (): void {
const cases = new Map<number, string[]>([
[
LogLevels.DEBUG,
@@ -73,7 +72,7 @@ test("simpleHandler", function (): void {
}
});
-test("testFormatterAsString", function (): void {
+Deno.test("testFormatterAsString", function (): void {
const handler = new TestHandler("DEBUG", {
formatter: "test {levelName} {msg}",
});
@@ -83,7 +82,7 @@ test("testFormatterAsString", function (): void {
assertEquals(handler.messages, ["test DEBUG Hello, world!"]);
});
-test("testFormatterAsFunction", function (): void {
+Deno.test("testFormatterAsFunction", function (): void {
const handler = new TestHandler("DEBUG", {
formatter: (logRecord): string =>
`fn formatter ${logRecord.levelName} ${logRecord.msg}`,
@@ -94,7 +93,7 @@ test("testFormatterAsFunction", function (): void {
assertEquals(handler.messages, ["fn formatter ERROR Hello, world!"]);
});
-test({
+Deno.test({
name: "FileHandler with mode 'w' will wipe clean existing log file",
async fn() {
const fileHandler = new FileHandler("WARNING", {
@@ -117,7 +116,7 @@ test({
},
});
-test({
+Deno.test({
name: "FileHandler with mode 'x' will throw if log file already exists",
async fn() {
const fileHandler = new FileHandler("WARNING", {
@@ -136,7 +135,7 @@ test({
},
});
-test({
+Deno.test({
name:
"RotatingFileHandler with mode 'w' will wipe clean existing log file and remove others",
async fn() {
@@ -172,7 +171,7 @@ test({
},
});
-test({
+Deno.test({
name:
"RotatingFileHandler with mode 'x' will throw if any log file already exists",
async fn() {
@@ -200,7 +199,7 @@ test({
},
});
-test({
+Deno.test({
name: "RotatingFileHandler with first rollover, monitor step by step",
async fn() {
const fileHandler = new RotatingFileHandler("WARNING", {
@@ -229,7 +228,7 @@ test({
},
});
-test({
+Deno.test({
name: "RotatingFileHandler with first rollover, check all at once",
async fn() {
const fileHandler = new RotatingFileHandler("WARNING", {
@@ -254,7 +253,7 @@ test({
},
});
-test({
+Deno.test({
name: "RotatingFileHandler with all backups rollover",
async fn() {
Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("original log file"));
@@ -304,7 +303,7 @@ test({
},
});
-test({
+Deno.test({
name: "RotatingFileHandler maxBytes cannot be less than 1",
async fn() {
await assertThrowsAsync(
@@ -323,7 +322,7 @@ test({
},
});
-test({
+Deno.test({
name: "RotatingFileHandler maxBackupCount cannot be less than 1",
async fn() {
await assertThrowsAsync(
@@ -342,7 +341,7 @@ test({
},
});
-test({
+Deno.test({
name: "Window unload flushes buffer",
async fn() {
const fileHandler = new FileHandler("WARNING", {
@@ -360,7 +359,7 @@ test({
},
});
-test({
+Deno.test({
name: "RotatingFileHandler: rotate on byte length, not msg length",
async fn() {
const fileHandler = new RotatingFileHandler("WARNING", {
@@ -394,7 +393,7 @@ test({
},
});
-test({
+Deno.test({
name: "FileHandler: Critical logs trigger immediate flush",
async fn() {
const fileHandler = new FileHandler("WARNING", {
diff --git a/std/log/logger_test.ts b/std/log/logger_test.ts
index b2e3cdab1..2425c15e6 100644
--- a/std/log/logger_test.ts
+++ b/std/log/logger_test.ts
@@ -1,5 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-const { test } = Deno;
import { assertEquals, assert } from "../testing/asserts.ts";
import { LogRecord, Logger } from "./logger.ts";
import { LogLevels, LevelName } from "./levels.ts";
@@ -19,7 +18,7 @@ class TestHandler extends BaseHandler {
}
}
-test("simpleLogger", function (): void {
+Deno.test("simpleLogger", function (): void {
const handler = new TestHandler("DEBUG");
let logger = new Logger("DEBUG");
@@ -32,7 +31,7 @@ test("simpleLogger", function (): void {
assertEquals(logger.handlers, [handler]);
});
-test("customHandler", function (): void {
+Deno.test("customHandler", function (): void {
const handler = new TestHandler("DEBUG");
const logger = new Logger("DEBUG", [handler]);
@@ -48,7 +47,7 @@ test("customHandler", function (): void {
assertEquals(inlineData!, "foo");
});
-test("logFunctions", function (): void {
+Deno.test("logFunctions", function (): void {
const doLog = (level: LevelName): TestHandler => {
const handler = new TestHandler(level);
const logger = new Logger(level, [handler]);
@@ -98,22 +97,29 @@ test("logFunctions", function (): void {
assertEquals(handler.messages, ["CRITICAL doo"]);
});
-test("String resolver fn will not execute if msg will not be logged", function (): void {
- const handler = new TestHandler("ERROR");
- const logger = new Logger("ERROR", [handler]);
- let called = false;
-
- const expensiveFunction = (): string => {
- called = true;
- return "expensive function result";
- };
-
- const inlineData: string | undefined = logger.debug(expensiveFunction, 1, 2);
- assert(!called);
- assertEquals(inlineData, undefined);
-});
+Deno.test(
+ "String resolver fn will not execute if msg will not be logged",
+ function (): void {
+ const handler = new TestHandler("ERROR");
+ const logger = new Logger("ERROR", [handler]);
+ let called = false;
+
+ const expensiveFunction = (): string => {
+ called = true;
+ return "expensive function result";
+ };
+
+ const inlineData: string | undefined = logger.debug(
+ expensiveFunction,
+ 1,
+ 2
+ );
+ assert(!called);
+ assertEquals(inlineData, undefined);
+ }
+);
-test("String resolver fn resolves as expected", function (): void {
+Deno.test("String resolver fn resolves as expected", function (): void {
const handler = new TestHandler("ERROR");
const logger = new Logger("ERROR", [handler]);
const expensiveFunction = (x: number): string => {
@@ -126,96 +132,99 @@ test("String resolver fn resolves as expected", function (): void {
assertEquals(secondInlineData, "expensive function result 12");
});
-test("All types map correctly to log strings and are returned as is", function (): void {
- const handler = new TestHandler("DEBUG");
- const logger = new Logger("DEBUG", [handler]);
- const sym = Symbol();
- const syma = Symbol("a");
- const fn = (): string => {
- return "abc";
- };
-
- // string
- const data1: string = logger.debug("abc");
- assertEquals(data1, "abc");
- const data2: string = logger.debug("def", 1);
- assertEquals(data2, "def");
- assertEquals(handler.messages[0], "DEBUG abc");
- assertEquals(handler.messages[1], "DEBUG def");
-
- // null
- const data3: null = logger.info(null);
- assertEquals(data3, null);
- const data4: null = logger.info(null, 1);
- assertEquals(data4, null);
- assertEquals(handler.messages[2], "INFO null");
- assertEquals(handler.messages[3], "INFO null");
-
- // number
- const data5: number = logger.warning(3);
- assertEquals(data5, 3);
- const data6: number = logger.warning(3, 1);
- assertEquals(data6, 3);
- assertEquals(handler.messages[4], "WARNING 3");
- assertEquals(handler.messages[5], "WARNING 3");
-
- // bigint
- const data7: bigint = logger.error(5n);
- assertEquals(data7, 5n);
- const data8: bigint = logger.error(5n, 1);
- assertEquals(data8, 5n);
- assertEquals(handler.messages[6], "ERROR 5");
- assertEquals(handler.messages[7], "ERROR 5");
-
- // boolean
- const data9: boolean = logger.critical(true);
- assertEquals(data9, true);
- const data10: boolean = logger.critical(false, 1);
- assertEquals(data10, false);
- assertEquals(handler.messages[8], "CRITICAL true");
- assertEquals(handler.messages[9], "CRITICAL false");
-
- // undefined
- const data11: undefined = logger.debug(undefined);
- assertEquals(data11, undefined);
- const data12: undefined = logger.debug(undefined, 1);
- assertEquals(data12, undefined);
- assertEquals(handler.messages[10], "DEBUG undefined");
- assertEquals(handler.messages[11], "DEBUG undefined");
-
- // symbol
- const data13: symbol = logger.info(sym);
- assertEquals(data13, sym);
- const data14: symbol = logger.info(syma, 1);
- assertEquals(data14, syma);
- assertEquals(handler.messages[12], "INFO Symbol()");
- assertEquals(handler.messages[13], "INFO Symbol(a)");
-
- // function
- const data15: string | undefined = logger.warning(fn);
- assertEquals(data15, "abc");
- const data16: string | undefined = logger.warning(fn, 1);
- assertEquals(data16, "abc");
- assertEquals(handler.messages[14], "WARNING abc");
- assertEquals(handler.messages[15], "WARNING abc");
-
- // object
- const data17: { payload: string; other: number } = logger.error({
- payload: "data",
- other: 123,
- });
- assertEquals(data17, {
- payload: "data",
- other: 123,
- });
- const data18: { payload: string; other: number } = logger.error(
- { payload: "data", other: 123 },
- 1
- );
- assertEquals(data18, {
- payload: "data",
- other: 123,
- });
- assertEquals(handler.messages[16], 'ERROR {"payload":"data","other":123}');
- assertEquals(handler.messages[17], 'ERROR {"payload":"data","other":123}');
-});
+Deno.test(
+ "All types map correctly to log strings and are returned as is",
+ function (): void {
+ const handler = new TestHandler("DEBUG");
+ const logger = new Logger("DEBUG", [handler]);
+ const sym = Symbol();
+ const syma = Symbol("a");
+ const fn = (): string => {
+ return "abc";
+ };
+
+ // string
+ const data1: string = logger.debug("abc");
+ assertEquals(data1, "abc");
+ const data2: string = logger.debug("def", 1);
+ assertEquals(data2, "def");
+ assertEquals(handler.messages[0], "DEBUG abc");
+ assertEquals(handler.messages[1], "DEBUG def");
+
+ // null
+ const data3: null = logger.info(null);
+ assertEquals(data3, null);
+ const data4: null = logger.info(null, 1);
+ assertEquals(data4, null);
+ assertEquals(handler.messages[2], "INFO null");
+ assertEquals(handler.messages[3], "INFO null");
+
+ // number
+ const data5: number = logger.warning(3);
+ assertEquals(data5, 3);
+ const data6: number = logger.warning(3, 1);
+ assertEquals(data6, 3);
+ assertEquals(handler.messages[4], "WARNING 3");
+ assertEquals(handler.messages[5], "WARNING 3");
+
+ // bigint
+ const data7: bigint = logger.error(5n);
+ assertEquals(data7, 5n);
+ const data8: bigint = logger.error(5n, 1);
+ assertEquals(data8, 5n);
+ assertEquals(handler.messages[6], "ERROR 5");
+ assertEquals(handler.messages[7], "ERROR 5");
+
+ // boolean
+ const data9: boolean = logger.critical(true);
+ assertEquals(data9, true);
+ const data10: boolean = logger.critical(false, 1);
+ assertEquals(data10, false);
+ assertEquals(handler.messages[8], "CRITICAL true");
+ assertEquals(handler.messages[9], "CRITICAL false");
+
+ // undefined
+ const data11: undefined = logger.debug(undefined);
+ assertEquals(data11, undefined);
+ const data12: undefined = logger.debug(undefined, 1);
+ assertEquals(data12, undefined);
+ assertEquals(handler.messages[10], "DEBUG undefined");
+ assertEquals(handler.messages[11], "DEBUG undefined");
+
+ // symbol
+ const data13: symbol = logger.info(sym);
+ assertEquals(data13, sym);
+ const data14: symbol = logger.info(syma, 1);
+ assertEquals(data14, syma);
+ assertEquals(handler.messages[12], "INFO Symbol()");
+ assertEquals(handler.messages[13], "INFO Symbol(a)");
+
+ // function
+ const data15: string | undefined = logger.warning(fn);
+ assertEquals(data15, "abc");
+ const data16: string | undefined = logger.warning(fn, 1);
+ assertEquals(data16, "abc");
+ assertEquals(handler.messages[14], "WARNING abc");
+ assertEquals(handler.messages[15], "WARNING abc");
+
+ // object
+ const data17: { payload: string; other: number } = logger.error({
+ payload: "data",
+ other: 123,
+ });
+ assertEquals(data17, {
+ payload: "data",
+ other: 123,
+ });
+ const data18: { payload: string; other: number } = logger.error(
+ { payload: "data", other: 123 },
+ 1
+ );
+ assertEquals(data18, {
+ payload: "data",
+ other: 123,
+ });
+ assertEquals(handler.messages[16], 'ERROR {"payload":"data","other":123}');
+ assertEquals(handler.messages[17], 'ERROR {"payload":"data","other":123}');
+ }
+);
diff --git a/std/log/mod_test.ts b/std/log/mod_test.ts
index 21b944fb5..98ac093c8 100644
--- a/std/log/mod_test.ts
+++ b/std/log/mod_test.ts
@@ -1,5 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-const { test } = Deno;
import { assert, assertEquals } from "../testing/asserts.ts";
import { getLogger, debug, info, warning, error, critical } from "./mod.ts";
import { Logger } from "./logger.ts";
@@ -13,11 +12,11 @@ try {
// Pass
}
-test("logger is initialized", function (): void {
+Deno.test("logger is initialized", function (): void {
assert(logger instanceof Logger);
});
-test("default loggers work as expected", function (): void {
+Deno.test("default loggers work as expected", function (): void {
const sym = Symbol("a");
const debugData: string = debug("foo");
const debugResolver: string | undefined = debug(() => "foo");
diff --git a/std/log/test.ts b/std/log/test.ts
index 2a51de6b5..0c0ab0703 100644
--- a/std/log/test.ts
+++ b/std/log/test.ts
@@ -1,5 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-const { test } = Deno;
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import * as log from "./mod.ts";
import {
@@ -17,7 +16,7 @@ class TestHandler extends log.handlers.BaseHandler {
}
}
-test("defaultHandlers", async function (): Promise<void> {
+Deno.test("defaultHandlers", async function (): Promise<void> {
const loggers: {
[key: string]: (msg: string, ...args: unknown[]) => void;
} = {
@@ -55,7 +54,7 @@ test("defaultHandlers", async function (): Promise<void> {
}
});
-test("getLogger", async function (): Promise<void> {
+Deno.test("getLogger", async function (): Promise<void> {
const handler = new TestHandler("DEBUG");
await log.setup({
@@ -76,7 +75,7 @@ test("getLogger", async function (): Promise<void> {
assertEquals(logger.handlers, [handler]);
});
-test("getLoggerWithName", async function (): Promise<void> {
+Deno.test("getLoggerWithName", async function (): Promise<void> {
const fooHandler = new TestHandler("DEBUG");
await log.setup({
@@ -97,7 +96,7 @@ test("getLoggerWithName", async function (): Promise<void> {
assertEquals(logger.handlers, [fooHandler]);
});
-test("getLoggerUnknown", async function (): Promise<void> {
+Deno.test("getLoggerUnknown", async function (): Promise<void> {
await log.setup({
handlers: {},
loggers: {},
@@ -109,7 +108,7 @@ test("getLoggerUnknown", async function (): Promise<void> {
assertEquals(logger.handlers, []);
});
-test("getInvalidLoggerLevels", function (): void {
+Deno.test("getInvalidLoggerLevels", function (): void {
assertThrows(() => getLevelByName("FAKE_LOG_LEVEL" as LevelName));
assertThrows(() => getLevelName(5000));
});