2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-01 22:46:17 -05:00
|
|
|
import { assertEqual, test } from "../testing/mod.ts";
|
2019-01-12 16:50:04 -05:00
|
|
|
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-19 13:46:46 -05:00
|
|
|
// constructor(levelName: string, options: HandlerOptions = {}) {
|
|
|
|
// this.level = getLevelByName(levelName);
|
|
|
|
// this.levelName = levelName;
|
|
|
|
|
|
|
|
// this.formatter = options.formatter || DEFAULT_FORMATTER;
|
|
|
|
// }
|
2018-12-19 13:16:45 -05:00
|
|
|
|
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-01-27 10:21:00 -05:00
|
|
|
test(async function defaultHandlers() {
|
|
|
|
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);
|
|
|
|
|
2019-01-28 11:17:00 -05:00
|
|
|
assertEqual(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-01-27 10:21:00 -05:00
|
|
|
test(async function getLogger() {
|
|
|
|
const handler = new TestHandler("DEBUG");
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
await log.setup({
|
|
|
|
handlers: {
|
2019-01-28 11:17:00 -05:00
|
|
|
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();
|
|
|
|
|
|
|
|
assertEqual(logger.levelName, "DEBUG");
|
2019-01-28 11:17:00 -05:00
|
|
|
assertEqual(logger.handlers, [handler]);
|
2019-01-19 13:46:46 -05:00
|
|
|
});
|
|
|
|
|
2019-01-27 10:21:00 -05:00
|
|
|
test(async function getLoggerWithName() {
|
|
|
|
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");
|
|
|
|
|
|
|
|
assertEqual(logger.levelName, "INFO");
|
2019-01-28 11:17:00 -05:00
|
|
|
assertEqual(logger.handlers, [fooHandler]);
|
2019-01-19 13:46:46 -05:00
|
|
|
});
|
|
|
|
|
2019-01-27 10:21:00 -05:00
|
|
|
test(async function getLoggerUnknown() {
|
2019-01-19 13:46:46 -05:00
|
|
|
await log.setup({
|
2019-01-28 11:17:00 -05:00
|
|
|
handlers: {},
|
|
|
|
loggers: {}
|
2019-01-19 13:46:46 -05:00
|
|
|
});
|
|
|
|
|
2019-01-27 10:21:00 -05:00
|
|
|
const logger = log.getLogger("nonexistent");
|
|
|
|
|
|
|
|
assertEqual(logger.levelName, "NOTSET");
|
|
|
|
assertEqual(logger.handlers, []);
|
2019-01-21 10:35:07 -05:00
|
|
|
});
|