1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-16 19:04:02 -05:00
denoland-deno/log/handlers.ts

118 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-02-07 19:45:47 +03:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
const { open } = Deno;
type File = Deno.File;
type Writer = Deno.Writer;
2019-02-11 05:28:52 +03:00
import { getLevelByName, LogLevel } from "./levels.ts";
2019-01-02 15:12:48 +01:00
import { LogRecord } from "./logger.ts";
2019-02-11 05:28:52 +03:00
import { red, yellow, blue, bold } from "../colors/mod.ts";
2019-01-02 15:12:48 +01:00
2019-01-19 19:46:46 +01:00
const DEFAULT_FORMATTER = "{levelName} {msg}";
type FormatterFunction = (logRecord: LogRecord) => string;
interface HandlerOptions {
formatter?: string | FormatterFunction;
}
2019-01-02 15:12:48 +01:00
export class BaseHandler {
level: number;
levelName: string;
2019-01-19 19:46:46 +01:00
formatter: string | FormatterFunction;
2019-01-02 15:12:48 +01:00
2019-01-19 19:46:46 +01:00
constructor(levelName: string, options: HandlerOptions = {}) {
2019-01-02 15:12:48 +01:00
this.level = getLevelByName(levelName);
this.levelName = levelName;
2019-01-19 19:46:46 +01:00
this.formatter = options.formatter || DEFAULT_FORMATTER;
2019-01-02 15:12:48 +01:00
}
2019-03-05 11:53:35 +11:00
handle(logRecord: LogRecord): void {
2019-01-02 15:12:48 +01:00
if (this.level > logRecord.level) return;
2019-01-19 19:46:46 +01:00
const msg = this.format(logRecord);
2019-01-02 15:12:48 +01:00
return this.log(msg);
}
2019-01-19 19:46:46 +01:00
format(logRecord: LogRecord): string {
if (this.formatter instanceof Function) {
return this.formatter(logRecord);
}
2019-01-21 23:35:07 +08:00
2019-01-19 19:46:46 +01:00
return this.formatter.replace(/{(\S+)}/g, (match, p1) => {
const value = logRecord[p1];
// do not interpolate missing values
if (!value) {
return match;
}
return value;
});
}
2019-03-05 11:53:35 +11:00
log(_msg: string): void {}
async setup(): Promise<void> {}
async destroy(): Promise<void> {}
2019-01-02 15:12:48 +01:00
}
export class ConsoleHandler extends BaseHandler {
2019-02-11 05:28:52 +03:00
format(logRecord: LogRecord): string {
let msg = super.format(logRecord);
switch (logRecord.level) {
case LogLevel.INFO:
msg = blue(msg);
break;
case LogLevel.WARNING:
msg = yellow(msg);
break;
case LogLevel.ERROR:
msg = red(msg);
break;
case LogLevel.CRITICAL:
msg = bold(red(msg));
break;
default:
break;
}
return msg;
}
2019-03-05 11:53:35 +11:00
log(msg: string): void {
2019-01-02 15:12:48 +01:00
console.log(msg);
}
}
export abstract class WriterHandler extends BaseHandler {
protected _writer: Writer;
2019-01-19 19:46:46 +01:00
private _encoder = new TextEncoder();
2019-01-02 15:12:48 +01:00
2019-03-05 11:53:35 +11:00
log(msg: string): void {
2019-01-19 19:46:46 +01:00
this._writer.write(this._encoder.encode(msg + "\n"));
2019-01-02 15:12:48 +01:00
}
}
2019-01-19 19:46:46 +01:00
interface FileHandlerOptions extends HandlerOptions {
filename: string;
}
2019-01-02 15:12:48 +01:00
export class FileHandler extends WriterHandler {
private _file: File;
private _filename: string;
2019-01-21 23:35:07 +08:00
constructor(levelName: string, options: FileHandlerOptions) {
2019-01-19 19:46:46 +01:00
super(levelName, options);
this._filename = options.filename;
2019-01-02 15:12:48 +01:00
}
2019-03-05 11:53:35 +11:00
async setup(): Promise<void> {
2019-01-02 15:12:48 +01:00
// open file in append mode - write only
2019-01-06 14:19:15 -05:00
this._file = await open(this._filename, "a");
2019-01-02 15:12:48 +01:00
this._writer = this._file;
}
2019-03-05 11:53:35 +11:00
async destroy(): Promise<void> {
2019-01-02 15:12:48 +01:00
await this._file.close();
}
2019-01-06 14:19:15 -05:00
}