2019-01-02 09:12:48 -05:00
|
|
|
import { open, File, Writer } from "deno";
|
|
|
|
import { getLevelByName } from "./levels.ts";
|
|
|
|
import { LogRecord } from "./logger.ts";
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
const DEFAULT_FORMATTER = "{levelName} {msg}";
|
|
|
|
type FormatterFunction = (logRecord: LogRecord) => string;
|
|
|
|
|
|
|
|
interface HandlerOptions {
|
|
|
|
formatter?: string | FormatterFunction;
|
|
|
|
}
|
|
|
|
|
2019-01-02 09:12:48 -05:00
|
|
|
export class BaseHandler {
|
|
|
|
level: number;
|
|
|
|
levelName: string;
|
2019-01-19 13:46:46 -05:00
|
|
|
formatter: string | FormatterFunction;
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
constructor(levelName: string, options: HandlerOptions = {}) {
|
2019-01-02 09:12:48 -05:00
|
|
|
this.level = getLevelByName(levelName);
|
|
|
|
this.levelName = levelName;
|
2019-01-19 13:46:46 -05:00
|
|
|
|
|
|
|
this.formatter = options.formatter || DEFAULT_FORMATTER;
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
handle(logRecord: LogRecord) {
|
|
|
|
if (this.level > logRecord.level) return;
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
const msg = this.format(logRecord);
|
2019-01-02 09:12:48 -05:00
|
|
|
return this.log(msg);
|
|
|
|
}
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
format(logRecord: LogRecord): string {
|
|
|
|
if (this.formatter instanceof Function) {
|
|
|
|
return this.formatter(logRecord);
|
|
|
|
}
|
2019-01-21 10:35:07 -05:00
|
|
|
|
2019-01-19 13:46:46 -05: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-01-06 14:19:15 -05:00
|
|
|
log(msg: string) {}
|
|
|
|
async setup() {}
|
|
|
|
async destroy() {}
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ConsoleHandler extends BaseHandler {
|
|
|
|
log(msg: string) {
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class WriterHandler extends BaseHandler {
|
|
|
|
protected _writer: Writer;
|
2019-01-19 13:46:46 -05:00
|
|
|
private _encoder = new TextEncoder();
|
2019-01-02 09:12:48 -05:00
|
|
|
|
|
|
|
log(msg: string) {
|
2019-01-19 13:46:46 -05:00
|
|
|
this._writer.write(this._encoder.encode(msg + "\n"));
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
interface FileHandlerOptions extends HandlerOptions {
|
|
|
|
filename: string;
|
|
|
|
}
|
|
|
|
|
2019-01-02 09:12:48 -05:00
|
|
|
export class FileHandler extends WriterHandler {
|
|
|
|
private _file: File;
|
|
|
|
private _filename: string;
|
|
|
|
|
2019-01-21 10:35:07 -05:00
|
|
|
constructor(levelName: string, options: FileHandlerOptions) {
|
2019-01-19 13:46:46 -05:00
|
|
|
super(levelName, options);
|
|
|
|
this._filename = options.filename;
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async setup() {
|
|
|
|
// 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 09:12:48 -05:00
|
|
|
this._writer = this._file;
|
|
|
|
}
|
|
|
|
|
|
|
|
async destroy() {
|
|
|
|
await this._file.close();
|
|
|
|
}
|
2019-01-06 14:19:15 -05:00
|
|
|
}
|