2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-12 09:27:41 -04:00
|
|
|
import { assertEquals, assert } from "../testing/asserts.ts";
|
2019-01-27 10:21:00 -05:00
|
|
|
import { LogRecord, Logger } from "./logger.ts";
|
2020-04-25 05:13:26 -04:00
|
|
|
import { LogLevels, LevelName } from "./levels.ts";
|
2019-01-27 10:21:00 -05:00
|
|
|
import { BaseHandler } from "./handlers.ts";
|
|
|
|
|
|
|
|
class TestHandler extends BaseHandler {
|
|
|
|
public messages: string[] = [];
|
|
|
|
public records: LogRecord[] = [];
|
|
|
|
|
|
|
|
handle(record: LogRecord): void {
|
2020-04-09 07:45:24 -04:00
|
|
|
this.records.push(record);
|
2019-01-27 10:21:00 -05:00
|
|
|
super.handle(record);
|
|
|
|
}
|
|
|
|
|
|
|
|
public log(str: string): void {
|
|
|
|
this.messages.push(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 06:50:18 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "Logger names can be output in logs",
|
|
|
|
fn() {
|
|
|
|
const handlerNoName = new TestHandler("DEBUG");
|
|
|
|
const handlerWithLoggerName = new TestHandler("DEBUG", {
|
|
|
|
formatter: "[{loggerName}] {levelName} {msg}",
|
|
|
|
});
|
|
|
|
|
|
|
|
const logger = new Logger("config", "DEBUG", {
|
|
|
|
handlers: [handlerNoName, handlerWithLoggerName],
|
|
|
|
});
|
|
|
|
logger.debug("hello");
|
|
|
|
assertEquals(handlerNoName.messages[0], "DEBUG hello");
|
|
|
|
assertEquals(handlerWithLoggerName.messages[0], "[config] DEBUG hello");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("simpleLogger", function (): void {
|
2019-01-27 10:21:00 -05:00
|
|
|
const handler = new TestHandler("DEBUG");
|
2020-06-18 06:50:18 -04:00
|
|
|
let logger = new Logger("default", "DEBUG");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2020-04-25 05:13:26 -04:00
|
|
|
assertEquals(logger.level, LogLevels.DEBUG);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(logger.levelName, "DEBUG");
|
|
|
|
assertEquals(logger.handlers, []);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2020-06-18 06:50:18 -04:00
|
|
|
logger = new Logger("default", "DEBUG", { handlers: [handler] });
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(logger.handlers, [handler]);
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("customHandler", function (): void {
|
2019-01-27 10:21:00 -05:00
|
|
|
const handler = new TestHandler("DEBUG");
|
2020-06-18 06:50:18 -04:00
|
|
|
const logger = new Logger("default", "DEBUG", { handlers: [handler] });
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2020-06-12 09:27:41 -04:00
|
|
|
const inlineData: string = logger.debug("foo", 1, 2);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
const record = handler.records[0];
|
|
|
|
assertEquals(record.msg, "foo");
|
|
|
|
assertEquals(record.args, [1, 2]);
|
2020-04-25 05:13:26 -04:00
|
|
|
assertEquals(record.level, LogLevels.DEBUG);
|
2019-05-30 08:59:30 -04:00
|
|
|
assertEquals(record.levelName, "DEBUG");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["DEBUG foo"]);
|
2020-06-12 09:27:41 -04:00
|
|
|
assertEquals(inlineData!, "foo");
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("logFunctions", function (): void {
|
2020-04-25 05:13:26 -04:00
|
|
|
const doLog = (level: LevelName): TestHandler => {
|
2019-05-30 08:59:30 -04:00
|
|
|
const handler = new TestHandler(level);
|
2020-06-18 06:50:18 -04:00
|
|
|
const logger = new Logger("default", level, { handlers: [handler] });
|
2020-06-12 09:27:41 -04:00
|
|
|
const debugData = logger.debug("foo");
|
|
|
|
const infoData = logger.info("bar");
|
|
|
|
const warningData = logger.warning("baz");
|
|
|
|
const errorData = logger.error("boo");
|
|
|
|
const criticalData = logger.critical("doo");
|
|
|
|
assertEquals(debugData, "foo");
|
|
|
|
assertEquals(infoData, "bar");
|
|
|
|
assertEquals(warningData, "baz");
|
|
|
|
assertEquals(errorData, "boo");
|
|
|
|
assertEquals(criticalData, "doo");
|
2019-05-30 08:59:30 -04:00
|
|
|
return handler;
|
2019-01-27 10:21:00 -05:00
|
|
|
};
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
let handler: TestHandler;
|
|
|
|
handler = doLog("DEBUG");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, [
|
2019-01-27 10:21:00 -05:00
|
|
|
"DEBUG foo",
|
|
|
|
"INFO bar",
|
|
|
|
"WARNING baz",
|
|
|
|
"ERROR boo",
|
2020-03-28 13:03:49 -04:00
|
|
|
"CRITICAL doo",
|
2019-01-27 10:21:00 -05:00
|
|
|
]);
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
handler = doLog("INFO");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, [
|
2019-01-27 10:21:00 -05:00
|
|
|
"INFO bar",
|
|
|
|
"WARNING baz",
|
|
|
|
"ERROR boo",
|
2020-03-28 13:03:49 -04:00
|
|
|
"CRITICAL doo",
|
2019-01-27 10:21:00 -05:00
|
|
|
]);
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
handler = doLog("WARNING");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["WARNING baz", "ERROR boo", "CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
handler = doLog("ERROR");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["ERROR boo", "CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
handler = doLog("CRITICAL");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|
2020-06-12 09:27:41 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"String resolver fn will not execute if msg will not be logged",
|
|
|
|
function (): void {
|
|
|
|
const handler = new TestHandler("ERROR");
|
2020-06-18 06:50:18 -04:00
|
|
|
const logger = new Logger("default", "ERROR", { handlers: [handler] });
|
2020-06-12 15:23:38 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
2020-06-12 09:27:41 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("String resolver fn resolves as expected", function (): void {
|
2020-06-12 09:27:41 -04:00
|
|
|
const handler = new TestHandler("ERROR");
|
2020-06-18 06:50:18 -04:00
|
|
|
const logger = new Logger("default", "ERROR", { handlers: [handler] });
|
2020-06-12 09:27:41 -04:00
|
|
|
const expensiveFunction = (x: number): string => {
|
|
|
|
return "expensive function result " + x;
|
|
|
|
};
|
|
|
|
|
|
|
|
const firstInlineData = logger.error(() => expensiveFunction(5));
|
|
|
|
const secondInlineData = logger.error(() => expensiveFunction(12), 1, "abc");
|
|
|
|
assertEquals(firstInlineData, "expensive function result 5");
|
|
|
|
assertEquals(secondInlineData, "expensive function result 12");
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"All types map correctly to log strings and are returned as is",
|
|
|
|
function (): void {
|
|
|
|
const handler = new TestHandler("DEBUG");
|
2020-06-18 06:50:18 -04:00
|
|
|
const logger = new Logger("default", "DEBUG", { handlers: [handler] });
|
2020-06-12 15:23:38 -04:00
|
|
|
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}');
|
|
|
|
}
|
|
|
|
);
|