1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 17:49:08 -05:00
denoland-deno/log/test.ts

108 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-02-07 11:45:47 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2019-03-06 16:39:50 -05:00
import { test } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import * as log from "./mod.ts";
2019-01-27 10:21:00 -05:00
import { LogLevel } from "./levels.ts";
2018-12-19 13:16:45 -05:00
2019-02-08 12:23:51 -05:00
import "./handlers_test.ts";
import "./logger_test.ts";
2019-01-02 09:12:48 -05:00
class TestHandler extends log.handlers.BaseHandler {
2019-01-27 10:21:00 -05:00
public messages: string[] = [];
2019-01-02 09:12:48 -05:00
2019-03-04 19:53:35 -05:00
log(msg: string): void {
2019-01-27 10:21:00 -05:00
this.messages.push(msg);
2018-12-19 13:16:45 -05:00
}
}
2019-04-24 07:41:23 -04:00
test(async function defaultHandlers(): Promise<void> {
2019-01-27 10:21:00 -05:00
const loggers = {
DEBUG: log.debug,
INFO: log.info,
WARNING: log.warning,
ERROR: log.error,
CRITICAL: log.critical
};
2018-12-19 13:16:45 -05:00
2019-01-27 10:21:00 -05:00
for (const levelName in LogLevel) {
if (levelName === "NOTSET") {
continue;
}
const level = LogLevel[levelName];
const logger = loggers[levelName];
const handler = new TestHandler(level);
await log.setup({
handlers: {
default: handler
},
loggers: {
default: {
level: levelName,
handlers: ["default"]
}
}
});
logger("foo");
logger("bar", 1, 2);
assertEquals(handler.messages, [`${levelName} foo`, `${levelName} bar`]);
2019-01-27 10:21:00 -05:00
}
2019-01-02 09:12:48 -05:00
});
2019-01-19 13:46:46 -05:00
2019-04-24 07:41:23 -04:00
test(async function getLogger(): Promise<void> {
2019-01-27 10:21:00 -05:00
const handler = new TestHandler("DEBUG");
2019-01-19 13:46:46 -05:00
await log.setup({
handlers: {
default: handler
2019-01-19 13:46:46 -05:00
},
loggers: {
2019-01-27 10:21:00 -05:00
default: {
2019-01-19 13:46:46 -05:00
level: "DEBUG",
2019-01-27 10:21:00 -05:00
handlers: ["default"]
2019-01-21 10:35:07 -05:00
}
}
2019-01-19 13:46:46 -05:00
});
2019-01-27 10:21:00 -05:00
const logger = log.getLogger();
assertEquals(logger.levelName, "DEBUG");
assertEquals(logger.handlers, [handler]);
2019-01-19 13:46:46 -05:00
});
2019-04-24 07:41:23 -04:00
test(async function getLoggerWithName(): Promise<void> {
2019-01-27 10:21:00 -05:00
const fooHandler = new TestHandler("DEBUG");
2019-01-19 13:46:46 -05:00
await log.setup({
handlers: {
2019-01-27 10:21:00 -05:00
foo: fooHandler
2019-01-19 13:46:46 -05:00
},
loggers: {
2019-01-27 10:21:00 -05:00
bar: {
level: "INFO",
handlers: ["foo"]
2019-01-21 10:35:07 -05:00
}
}
2019-01-19 13:46:46 -05:00
});
2019-01-27 10:21:00 -05:00
const logger = log.getLogger("bar");
assertEquals(logger.levelName, "INFO");
assertEquals(logger.handlers, [fooHandler]);
2019-01-19 13:46:46 -05:00
});
2019-04-24 07:41:23 -04:00
test(async function getLoggerUnknown(): Promise<void> {
2019-01-19 13:46:46 -05:00
await log.setup({
handlers: {},
loggers: {}
2019-01-19 13:46:46 -05:00
});
2019-01-27 10:21:00 -05:00
const logger = log.getLogger("nonexistent");
assertEquals(logger.levelName, "NOTSET");
assertEquals(logger.handlers, []);
2019-01-21 10:35:07 -05:00
});