1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -05:00
Original: 4b054d69ad
This commit is contained in:
James Garbutt 2019-01-27 15:21:00 +00:00 committed by Ryan Dahl
parent 75dab82adb
commit b79d2a9d41
5 changed files with 289 additions and 64 deletions

95
log/handlers_test.ts Normal file
View file

@ -0,0 +1,95 @@
import { assertEqual, test } from "../testing/mod.ts";
import { LogRecord, Logger } from "./logger.ts";
import { LogLevel, getLevelName, getLevelByName } from "./levels.ts";
import { BaseHandler } from "./handlers.ts";
class TestHandler extends BaseHandler {
public messages: string[] = [];
public log(str: string): void {
this.messages.push(str);
}
}
test(function simpleHandler() {
const cases = new Map<number, string[]>([
[LogLevel.DEBUG, [
"DEBUG debug-test",
"INFO info-test",
"WARNING warning-test",
"ERROR error-test",
"CRITICAL critical-test"
]],
[LogLevel.INFO, [
"INFO info-test",
"WARNING warning-test",
"ERROR error-test",
"CRITICAL critical-test"
]],
[LogLevel.WARNING, [
"WARNING warning-test",
"ERROR error-test",
"CRITICAL critical-test"
]],
[LogLevel.ERROR, [
"ERROR error-test",
"CRITICAL critical-test"
]],
[LogLevel.CRITICAL, [
"CRITICAL critical-test"
]]
]);
for (const [testCase, messages] of cases.entries()) {
const testLevel = getLevelName(testCase);
const handler = new TestHandler(testLevel);
for (const levelName in LogLevel) {
const level = getLevelByName(levelName);
handler.handle({
msg: `${levelName.toLowerCase()}-test`,
args: [],
datetime: new Date(),
level: level,
levelName: levelName
});
}
assertEqual(handler.level, testCase);
assertEqual(handler.levelName, testLevel);
assertEqual(handler.messages, messages);
}
});
test(function testFormatterAsString() {
const handler = new TestHandler("DEBUG", {
formatter: "test {levelName} {msg}"
});
handler.handle({
msg: "Hello, world!",
args: [],
datetime: new Date(),
level: LogLevel.DEBUG,
levelName: "DEBUG"
});
assertEqual(handler.messages, ["test DEBUG Hello, world!"]);
});
test(function testFormatterAsFunction() {
const handler = new TestHandler("DEBUG", {
formatter: logRecord =>
`fn formmatter ${logRecord.levelName} ${logRecord.msg}`
});
handler.handle({
msg: "Hello, world!",
args: [],
datetime: new Date(),
level: LogLevel.ERROR,
levelName: "ERROR"
});
assertEqual(handler.messages, ["fn formmatter ERROR Hello, world!"]);
});

104
log/logger_test.ts Normal file
View file

@ -0,0 +1,104 @@
import { assertEqual, test } from "../testing/mod.ts";
import { LogRecord, Logger } from "./logger.ts";
import { LogLevel } from "./levels.ts";
import { BaseHandler } from "./handlers.ts";
class TestHandler extends BaseHandler {
public messages: string[] = [];
public records: LogRecord[] = [];
handle(record: LogRecord): void {
this.records.push({...record, datetime: null });
super.handle(record);
}
public log(str: string): void {
this.messages.push(str);
}
}
test(function simpleLogger() {
const handler = new TestHandler("DEBUG");
let logger = new Logger("DEBUG");
assertEqual(logger.level, LogLevel.DEBUG);
assertEqual(logger.levelName, "DEBUG");
assertEqual(logger.handlers, []);
logger = new Logger("DEBUG", [handler]);
assertEqual(logger.handlers, [handler]);
});
test(function customHandler() {
const handler = new TestHandler("DEBUG");
const logger = new Logger("DEBUG", [handler]);
logger.debug("foo", 1, 2);
assertEqual(handler.records, [
{
msg: "foo",
args: [1, 2],
datetime: null,
level: LogLevel.DEBUG,
levelName: "DEBUG"
}
]);
assertEqual(handler.messages, ["DEBUG foo"]);
});
test(function logFunctions() {
let handler: TestHandler;
const doLog = (level: string) => {
handler = new TestHandler(level);
let logger = new Logger(level, [handler]);
logger.debug("foo");
logger.info("bar");
logger.warning("baz");
logger.error("boo");
logger.critical("doo");
};
doLog("DEBUG");
assertEqual(handler.messages, [
"DEBUG foo",
"INFO bar",
"WARNING baz",
"ERROR boo",
"CRITICAL doo"
]);
doLog("INFO");
assertEqual(handler.messages, [
"INFO bar",
"WARNING baz",
"ERROR boo",
"CRITICAL doo"
]);
doLog("WARNING");
assertEqual(handler.messages, [
"WARNING baz",
"ERROR boo",
"CRITICAL doo"
]);
doLog("ERROR");
assertEqual(handler.messages, [
"ERROR boo",
"CRITICAL doo"
]);
doLog("CRITICAL");
assertEqual(handler.messages, [
"CRITICAL doo"
]);
});

View file

@ -21,24 +21,20 @@ export interface LogConfig {
} }
const DEFAULT_LEVEL = "INFO"; const DEFAULT_LEVEL = "INFO";
const DEFAULT_NAME = "";
const DEFAULT_CONFIG: LogConfig = { const DEFAULT_CONFIG: LogConfig = {
handlers: {}, handlers: {
"default": new ConsoleHandler(DEFAULT_LEVEL)
},
loggers: { loggers: {
"": { "default": {
level: "INFO", level: DEFAULT_LEVEL,
handlers: [""] handlers: ["default"]
} }
} }
}; };
const defaultHandler = new ConsoleHandler("INFO");
const defaultLogger = new Logger("INFO", [defaultHandler]);
const state = { const state = {
defaultHandler,
defaultLogger,
handlers: new Map(), handlers: new Map(),
loggers: new Map(), loggers: new Map(),
config: DEFAULT_CONFIG config: DEFAULT_CONFIG
@ -52,19 +48,19 @@ export const handlers = {
}; };
export const debug = (msg: string, ...args: any[]) => export const debug = (msg: string, ...args: any[]) =>
defaultLogger.debug(msg, ...args); getLogger('default').debug(msg, ...args);
export const info = (msg: string, ...args: any[]) => export const info = (msg: string, ...args: any[]) =>
defaultLogger.info(msg, ...args); getLogger('default').info(msg, ...args);
export const warning = (msg: string, ...args: any[]) => export const warning = (msg: string, ...args: any[]) =>
defaultLogger.warning(msg, ...args); getLogger('default').warning(msg, ...args);
export const error = (msg: string, ...args: any[]) => export const error = (msg: string, ...args: any[]) =>
defaultLogger.error(msg, ...args); getLogger('default').error(msg, ...args);
export const critical = (msg: string, ...args: any[]) => export const critical = (msg: string, ...args: any[]) =>
defaultLogger.critical(msg, ...args); getLogger('default').critical(msg, ...args);
export function getLogger(name?: string) { export function getLogger(name?: string) {
if (!name) { if (!name) {
return defaultLogger; return state.loggers.get('default');
} }
if (!state.loggers.has(name)) { if (!state.loggers.has(name)) {
@ -81,7 +77,10 @@ export function getHandler(name: string) {
} }
export async function setup(config: LogConfig) { export async function setup(config: LogConfig) {
state.config = config; state.config = {
handlers: {...DEFAULT_CONFIG.handlers, ...config.handlers},
loggers: {...DEFAULT_CONFIG.loggers, ...config.loggers}
};
// tear down existing handlers // tear down existing handlers
state.handlers.forEach(handler => { state.handlers.forEach(handler => {

View file

@ -1,7 +1,8 @@
import { remove, open, readAll } from "deno";
import { assertEqual, test } from "../testing/mod.ts"; import { assertEqual, test } from "../testing/mod.ts";
import * as log from "./mod.ts"; import * as log from "./mod.ts";
import { FileHandler } from "./handlers.ts"; import { BaseHandler } from "./handlers.ts";
import { LogRecord } from "./logger.ts";
import { LogLevel } from "./levels.ts";
// constructor(levelName: string, options: HandlerOptions = {}) { // constructor(levelName: string, options: HandlerOptions = {}) {
// this.level = getLevelByName(levelName); // this.level = getLevelByName(levelName);
@ -11,85 +12,109 @@ import { FileHandler } from "./handlers.ts";
// } // }
class TestHandler extends log.handlers.BaseHandler { class TestHandler extends log.handlers.BaseHandler {
testOutput = ""; public messages: string[] = [];
log(msg: string) { log(msg: string) {
this.testOutput += `${msg}\n`; this.messages.push(msg);
} }
} }
test(function testDefaultlogMethods() { test(async function defaultHandlers() {
log.debug("Foobar"); const loggers = {
log.info("Foobar"); DEBUG: log.debug,
log.warning("Foobar"); INFO: log.info,
log.error("Foobar"); WARNING: log.warning,
log.critical("Foobar"); ERROR: log.error,
CRITICAL: log.critical
};
const logger = log.getLogger(""); for (const levelName in LogLevel) {
console.log(logger); 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);
assertEqual(handler.messages, [
`${levelName} foo`,
`${levelName} bar`
]);
}
}); });
test(async function testDefaultFormatter() { test(async function getLogger() {
const handler = new TestHandler("DEBUG");
await log.setup({ await log.setup({
handlers: { handlers: {
test: new TestHandler("DEBUG") default: handler
}, },
loggers: { loggers: {
test: { default: {
level: "DEBUG", level: "DEBUG",
handlers: ["test"] handlers: ["default"]
} }
} }
}); });
const logger = log.getLogger("test"); const logger = log.getLogger();
const handler = log.getHandler("test");
logger.debug("Hello, world!"); assertEqual(logger.levelName, "DEBUG");
assertEqual(handler.testOutput, "DEBUG Hello, world!\n"); assertEqual(logger.handlers, [
handler
]);
}); });
test(async function testFormatterAsString() { test(async function getLoggerWithName() {
const fooHandler = new TestHandler("DEBUG");
await log.setup({ await log.setup({
handlers: { handlers: {
test: new TestHandler("DEBUG", { foo: fooHandler
formatter: "test {levelName} {msg}"
})
}, },
loggers: { loggers: {
test: { bar: {
level: "DEBUG", level: "INFO",
handlers: ["test"] handlers: ["foo"]
} }
} }
}); });
const logger = log.getLogger("test"); const logger = log.getLogger("bar");
const handler = log.getHandler("test");
logger.debug("Hello, world!"); assertEqual(logger.levelName, "INFO");
assertEqual(handler.testOutput, "test DEBUG Hello, world!\n"); assertEqual(logger.handlers, [
fooHandler
]);
}); });
test(async function testFormatterAsFunction() { test(async function getLoggerUnknown() {
await log.setup({ await log.setup({
handlers: { handlers: {
test: new TestHandler("DEBUG", {
formatter: logRecord =>
`fn formmatter ${logRecord.levelName} ${logRecord.msg}`
})
}, },
loggers: { loggers: {
test: {
level: "DEBUG",
handlers: ["test"]
}
} }
}); });
const logger = log.getLogger("test"); const logger = log.getLogger("nonexistent");
const handler = log.getHandler("test");
logger.error("Hello, world!"); assertEqual(logger.levelName, "NOTSET");
assertEqual(handler.testOutput, "fn formmatter ERROR Hello, world!\n"); assertEqual(logger.handlers, []);
}); });

View file

@ -18,6 +18,8 @@ import "io/util_test.ts";
import "http/http_test.ts"; import "http/http_test.ts";
import "http/file_server_test.ts"; import "http/file_server_test.ts";
import "log/test.ts"; import "log/test.ts";
import "log/handlers_test.ts";
import "log/logger_test.ts";
import "media_types/test.ts"; import "media_types/test.ts";
import "testing/test.ts"; import "testing/test.ts";
import "textproto/test.ts"; import "textproto/test.ts";