mirror of
https://github.com/denoland/deno.git
synced 2024-11-27 16:10:57 -05:00
Implement formatters for log (#127)
This commit is contained in:
parent
fa5b3dfc9c
commit
c304ee503a
3 changed files with 117 additions and 16 deletions
|
@ -2,24 +2,49 @@ import { open, File, Writer } from "deno";
|
|||
import { getLevelByName } from "./levels.ts";
|
||||
import { LogRecord } from "./logger.ts";
|
||||
|
||||
const DEFAULT_FORMATTER = "{levelName} {msg}";
|
||||
type FormatterFunction = (logRecord: LogRecord) => string;
|
||||
|
||||
interface HandlerOptions {
|
||||
formatter?: string | FormatterFunction;
|
||||
}
|
||||
|
||||
export class BaseHandler {
|
||||
level: number;
|
||||
levelName: string;
|
||||
formatter: string | FormatterFunction;
|
||||
|
||||
constructor(levelName: string) {
|
||||
constructor(levelName: string, options: HandlerOptions = {}) {
|
||||
this.level = getLevelByName(levelName);
|
||||
this.levelName = levelName;
|
||||
|
||||
this.formatter = options.formatter || DEFAULT_FORMATTER;
|
||||
}
|
||||
|
||||
handle(logRecord: LogRecord) {
|
||||
if (this.level > logRecord.level) return;
|
||||
|
||||
// TODO: implement formatter
|
||||
const msg = `${logRecord.levelName} ${logRecord.msg}`;
|
||||
|
||||
const msg = this.format(logRecord);
|
||||
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) {}
|
||||
async setup() {}
|
||||
async destroy() {}
|
||||
|
@ -33,21 +58,27 @@ export class ConsoleHandler extends BaseHandler {
|
|||
|
||||
export abstract class WriterHandler extends BaseHandler {
|
||||
protected _writer: Writer;
|
||||
private _encoder = new TextEncoder();
|
||||
|
||||
log(msg: string) {
|
||||
const encoder = new TextEncoder();
|
||||
// promise is intentionally not awaited
|
||||
this._writer.write(encoder.encode(msg + "\n"));
|
||||
this._writer.write(this._encoder.encode(msg + "\n"));
|
||||
}
|
||||
}
|
||||
|
||||
interface FileHandlerOptions extends HandlerOptions {
|
||||
filename: string;
|
||||
}
|
||||
|
||||
export class FileHandler extends WriterHandler {
|
||||
private _file: File;
|
||||
private _filename: string;
|
||||
|
||||
constructor(levelName: string, filename: string) {
|
||||
super(levelName);
|
||||
this._filename = filename;
|
||||
constructor(
|
||||
levelName: string,
|
||||
options: FileHandlerOptions,
|
||||
) {
|
||||
super(levelName, options);
|
||||
this._filename = options.filename;
|
||||
}
|
||||
|
||||
async setup() {
|
||||
|
|
|
@ -76,6 +76,10 @@ export function getLogger(name?: string) {
|
|||
return state.loggers.get(name);
|
||||
}
|
||||
|
||||
export function getHandler(name: string) {
|
||||
return state.handlers.get(name);
|
||||
}
|
||||
|
||||
export async function setup(config: LogConfig) {
|
||||
state.config = config;
|
||||
|
||||
|
|
78
log/test.ts
78
log/test.ts
|
@ -3,16 +3,18 @@ import { assertEqual, test } from "../testing/mod.ts";
|
|||
import * as log from "./mod.ts";
|
||||
import { FileHandler } from "./handlers.ts";
|
||||
|
||||
// TODO: establish something more sophisticated
|
||||
let testOutput = "";
|
||||
// constructor(levelName: string, options: HandlerOptions = {}) {
|
||||
// this.level = getLevelByName(levelName);
|
||||
// this.levelName = levelName;
|
||||
|
||||
// this.formatter = options.formatter || DEFAULT_FORMATTER;
|
||||
// }
|
||||
|
||||
class TestHandler extends log.handlers.BaseHandler {
|
||||
constructor(levelName: string) {
|
||||
super(levelName);
|
||||
}
|
||||
testOutput = "";
|
||||
|
||||
log(msg: string) {
|
||||
testOutput += `${msg}\n`;
|
||||
this.testOutput += `${msg}\n`;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,3 +28,67 @@ test(function testDefaultlogMethods() {
|
|||
const logger = log.getLogger("");
|
||||
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");
|
||||
});
|
Loading…
Reference in a new issue