summaryrefslogtreecommitdiff
path: root/logging/test.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-01-02 15:12:48 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-01-02 09:12:48 -0500
commit4659271518b71b90eb82b05b8aeb655c82a8a93e (patch)
tree629d81dad90ec6106f13901e25f2f5142e195026 /logging/test.ts
parentbc4635a5938513886c7c6d1801e6ebcddf62b699 (diff)
Improve logging module (denoland/deno_std#51)
Original: https://github.com/denoland/deno_std/commit/439885c756615f4da4953460c47d58cc9cc5bd2b
Diffstat (limited to 'logging/test.ts')
-rw-r--r--logging/test.ts97
1 files changed, 64 insertions, 33 deletions
diff --git a/logging/test.ts b/logging/test.ts
index 365064cbf..4232a968c 100644
--- a/logging/test.ts
+++ b/logging/test.ts
@@ -1,53 +1,84 @@
+import { remove, open, readAll } from "deno";
import { assertEqual, test } from "https://deno.land/x/testing/testing.ts";
-import * as logging from "index.ts";
+import * as log from "index.ts";
+import { FileHandler } from "./handlers.ts";
// TODO: establish something more sophisticated
-
let testOutput = "";
-class TestHandler extends logging.handlers.BaseHandler {
- _log(level, ...args) {
- testOutput += `${level} ${args[0]}\n`;
+class TestHandler extends log.handlers.BaseHandler {
+ constructor(levelName: string) {
+ super(levelName);
+ }
+
+ log(msg: string) {
+ testOutput += `${msg}\n`;
}
}
-logging.setup({
- handlers: {
- debug: {
- level: "DEBUG",
- class: TestHandler
- },
+test(function testDefaultlogMethods() {
+ log.debug("Foobar");
+ log.info("Foobar");
+ log.warning("Foobar");
+ log.error("Foobar");
+ log.critical("Foobar");
- info: {
- level: "INFO",
- class: TestHandler
- }
- },
+ const logger = log.getLogger('');
+ console.log(logger);
+});
+
+test(async function basicTest() {
+ const testFile = './log.txt';
- loggers: {
- default: {
- level: "DEBUG",
- handlers: ["debug"]
+ await log.setup({
+ handlers: {
+ debug: new TestHandler("DEBUG"),
+ info: new TestHandler("INFO"),
+ file: new FileHandler("DEBUG", testFile),
},
- info: {
- level: "INFO",
- handlers: ["info"]
- }
- }
-});
+ loggers: {
+ foo: {
+ level: "DEBUG",
+ handlers: ["debug", "file"]
+ },
-const logger = logging.getLogger("default");
-const unknownLogger = logging.getLogger("info");
+ bar: {
+ level: "INFO",
+ handlers: ["info"]
+ }
+ }
+ });
-test(function basicTest() {
- logger.debug("I should be printed.");
- unknownLogger.debug("I should not be printed.");
- unknownLogger.info("And I should be printed as well.");
+ const fooLogger = log.getLogger("foo");
+ const barLogger = log.getLogger("bar");
+ const bazzLogger = log.getLogger("bazz");
+
+ 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.")
+
const expectedOutput =
- "10 I should be printed.\n20 And I should be printed as well.\n";
+ "DEBUG I should be logged.\n" +
+ "DEBUG I should be logged.\n" +
+ "INFO And I should be logged as well.\n";
assertEqual(testOutput, expectedOutput);
+
+ // 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);
});