summaryrefslogtreecommitdiff
path: root/logging/test.ts
blob: b96a046ddcbf28ebdf0c3646d661181d238aa24c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { remove, open, readAll } from "deno";
import { assertEqual, test } from "../testing/mod.ts";
import * as log from "index.ts";
import { FileHandler } from "./handlers.ts";

// TODO: establish something more sophisticated
let testOutput = "";

class TestHandler extends log.handlers.BaseHandler {
  constructor(levelName: string) {
    super(levelName);
  }

  log(msg: string) {
    testOutput += `${msg}\n`;
  }
}

test(function testDefaultlogMethods() {
  log.debug("Foobar");
  log.info("Foobar");
  log.warning("Foobar");
  log.error("Foobar");
  log.critical("Foobar");

  const logger = log.getLogger('');
  console.log(logger);
});

test(async function basicTest() {
  const testFile = './log.txt';

  await log.setup({
    handlers: {
      debug: new TestHandler("DEBUG"),
      info: new TestHandler("INFO"),
      file: new FileHandler("DEBUG", testFile),
    },

    loggers: {
      foo: {
        level: "DEBUG",
        handlers: ["debug", "file"]
      },

      bar: {
        level: "INFO",
        handlers: ["info"]
      }
    }
  });

  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 =
    "DEBUG I should be logged.\n" +
    "DEBUG I should be logged.\n" +
    "INFO And I should be logged as well.\n";

  // TODO(ry) Re-enable this test. Disabled because it was failing on Linux.
  // 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);
});