1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-18 20:04:03 -05:00
denoland-deno/logging/test.ts

85 lines
2 KiB
TypeScript
Raw Normal View History

2019-01-02 15:12:48 +01:00
import { remove, open, readAll } from "deno";
2018-12-19 19:16:45 +01:00
import { assertEqual, test } from "https://deno.land/x/testing/testing.ts";
2019-01-02 15:12:48 +01:00
import * as log from "index.ts";
import { FileHandler } from "./handlers.ts";
2018-12-19 19:16:45 +01:00
// TODO: establish something more sophisticated
let testOutput = "";
2019-01-02 15:12:48 +01:00
class TestHandler extends log.handlers.BaseHandler {
constructor(levelName: string) {
super(levelName);
}
log(msg: string) {
testOutput += `${msg}\n`;
2018-12-19 19:16:45 +01:00
}
}
2019-01-02 15:12:48 +01:00
test(function testDefaultlogMethods() {
log.debug("Foobar");
log.info("Foobar");
log.warning("Foobar");
log.error("Foobar");
log.critical("Foobar");
2018-12-19 19:16:45 +01:00
2019-01-02 15:12:48 +01:00
const logger = log.getLogger('');
console.log(logger);
});
test(async function basicTest() {
const testFile = './log.txt';
2018-12-19 19:16:45 +01:00
2019-01-02 15:12:48 +01:00
await log.setup({
handlers: {
debug: new TestHandler("DEBUG"),
info: new TestHandler("INFO"),
file: new FileHandler("DEBUG", testFile),
2018-12-19 19:16:45 +01:00
},
2019-01-02 15:12:48 +01:00
loggers: {
foo: {
level: "DEBUG",
handlers: ["debug", "file"]
},
2018-12-19 19:16:45 +01:00
2019-01-02 15:12:48 +01:00
bar: {
level: "INFO",
handlers: ["info"]
}
}
});
2018-12-19 19:16:45 +01:00
2019-01-02 15:12:48 +01:00
const fooLogger = log.getLogger("foo");
const barLogger = log.getLogger("bar");
const bazzLogger = log.getLogger("bazz");
2018-12-19 19:16:45 +01:00
2019-01-02 15:12:48 +01:00
fooLogger.debug("I should be logged.");
fooLogger.debug("I should be logged.");
barLogger.debug("I should not be logged.");
barLogger.info("And I should be logged as well.");
bazzLogger.critical("I shouldn't be logged neither.")
2018-12-19 19:16:45 +01:00
const expectedOutput =
2019-01-02 15:12:48 +01:00
"DEBUG I should be logged.\n" +
"DEBUG I should be logged.\n" +
"INFO And I should be logged as well.\n";
2018-12-19 19:16:45 +01:00
assertEqual(testOutput, expectedOutput);
2019-01-02 15:12:48 +01:00
// same check for file handler
const f = await open(testFile);
const bytes = await readAll(f);
const fileOutput = new TextDecoder().decode(bytes);
await f.close();
await remove(testFile);
const fileExpectedOutput =
"DEBUG I should be logged.\n" +
"DEBUG I should be logged.\n";
assertEqual(fileOutput, fileExpectedOutput);
2018-12-19 19:16:45 +01:00
});