1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-14 18:08:52 -05:00

Implement formatters for log (denoland/deno_std#127)

Original: c304ee503a
This commit is contained in:
Bartek Iwańczuk 2019-01-19 19:46:46 +01:00 committed by Ryan Dahl
parent a489edc355
commit 4cc5839154
3 changed files with 117 additions and 16 deletions

View file

@ -2,24 +2,49 @@ import { open, File, Writer } from "deno";
import { getLevelByName } from "./levels.ts"; import { getLevelByName } from "./levels.ts";
import { LogRecord } from "./logger.ts"; import { LogRecord } from "./logger.ts";
const DEFAULT_FORMATTER = "{levelName} {msg}";
type FormatterFunction = (logRecord: LogRecord) => string;
interface HandlerOptions {
formatter?: string | FormatterFunction;
}
export class BaseHandler { export class BaseHandler {
level: number; level: number;
levelName: string; levelName: string;
formatter: string | FormatterFunction;
constructor(levelName: string) { constructor(levelName: string, options: HandlerOptions = {}) {
this.level = getLevelByName(levelName); this.level = getLevelByName(levelName);
this.levelName = levelName; this.levelName = levelName;
this.formatter = options.formatter || DEFAULT_FORMATTER;
} }
handle(logRecord: LogRecord) { handle(logRecord: LogRecord) {
if (this.level > logRecord.level) return; if (this.level > logRecord.level) return;
// TODO: implement formatter const msg = this.format(logRecord);
const msg = `${logRecord.levelName} ${logRecord.msg}`;
return this.log(msg); return this.log(msg);
} }
format(logRecord: LogRecord): string {
if (this.formatter instanceof Function) {
return this.formatter(logRecord);
}
return this.formatter.replace(/{(\S+)}/g, (match, p1) => {
const value = logRecord[p1];
// do not interpolate missing values
if (!value) {
return match;
}
return value;
});
}
log(msg: string) {} log(msg: string) {}
async setup() {} async setup() {}
async destroy() {} async destroy() {}
@ -33,21 +58,27 @@ export class ConsoleHandler extends BaseHandler {
export abstract class WriterHandler extends BaseHandler { export abstract class WriterHandler extends BaseHandler {
protected _writer: Writer; protected _writer: Writer;
private _encoder = new TextEncoder();
log(msg: string) { log(msg: string) {
const encoder = new TextEncoder(); this._writer.write(this._encoder.encode(msg + "\n"));
// promise is intentionally not awaited
this._writer.write(encoder.encode(msg + "\n"));
} }
} }
interface FileHandlerOptions extends HandlerOptions {
filename: string;
}
export class FileHandler extends WriterHandler { export class FileHandler extends WriterHandler {
private _file: File; private _file: File;
private _filename: string; private _filename: string;
constructor(levelName: string, filename: string) { constructor(
super(levelName); levelName: string,
this._filename = filename; options: FileHandlerOptions,
) {
super(levelName, options);
this._filename = options.filename;
} }
async setup() { async setup() {

View file

@ -76,6 +76,10 @@ export function getLogger(name?: string) {
return state.loggers.get(name); return state.loggers.get(name);
} }
export function getHandler(name: string) {
return state.handlers.get(name);
}
export async function setup(config: LogConfig) { export async function setup(config: LogConfig) {
state.config = config; state.config = config;

View file

@ -3,16 +3,18 @@ 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 { FileHandler } from "./handlers.ts";
// TODO: establish something more sophisticated // constructor(levelName: string, options: HandlerOptions = {}) {
let testOutput = ""; // this.level = getLevelByName(levelName);
// this.levelName = levelName;
// this.formatter = options.formatter || DEFAULT_FORMATTER;
// }
class TestHandler extends log.handlers.BaseHandler { class TestHandler extends log.handlers.BaseHandler {
constructor(levelName: string) { testOutput = "";
super(levelName);
}
log(msg: string) { log(msg: string) {
testOutput += `${msg}\n`; this.testOutput += `${msg}\n`;
} }
} }
@ -26,3 +28,67 @@ test(function testDefaultlogMethods() {
const logger = log.getLogger(""); const logger = log.getLogger("");
console.log(logger); console.log(logger);
}); });
test(async function testDefaultFormatter() {
await log.setup({
handlers: {
test: new TestHandler("DEBUG"),
},
loggers: {
test: {
level: "DEBUG",
handlers: ["test"],
},
},
});
const logger = log.getLogger("test");
const handler = log.getHandler("test");
logger.debug("Hello, world!");
assertEqual(handler.testOutput, "DEBUG Hello, world!\n");
});
test(async function testFormatterAsString() {
await log.setup({
handlers: {
test: new TestHandler("DEBUG", {
formatter: "test {levelName} {msg}",
}),
},
loggers: {
test: {
level: "DEBUG",
handlers: ["test"],
},
},
});
const logger = log.getLogger("test");
const handler = log.getHandler("test");
logger.debug("Hello, world!");
assertEqual(handler.testOutput, "test DEBUG Hello, world!\n");
});
test(async function testFormatterAsFunction() {
await log.setup({
handlers: {
test: new TestHandler("DEBUG", {
formatter: logRecord => `fn formmatter ${logRecord.levelName} ${logRecord.msg}`,
}),
},
loggers: {
test: {
level: "DEBUG",
handlers: ["test"],
},
},
});
const logger = log.getLogger("test");
const handler = log.getHandler("test");
logger.error("Hello, world!");
assertEqual(handler.testOutput, "fn formmatter ERROR Hello, world!\n");
});