2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-04-25 05:13:26 -04:00
|
|
|
import { getLevelByName, LevelName, LogLevels } from "./levels.ts";
|
2019-01-02 09:12:48 -05:00
|
|
|
import { LogRecord } from "./logger.ts";
|
2019-08-24 13:38:18 -04:00
|
|
|
import { red, yellow, blue, bold } from "../fmt/colors.ts";
|
2020-04-09 07:45:24 -04:00
|
|
|
import { existsSync, exists } from "../fs/exists.ts";
|
2020-06-01 18:31:17 -04:00
|
|
|
import { BufWriterSync } from "../io/bufio.ts";
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
const DEFAULT_FORMATTER = "{levelName} {msg}";
|
|
|
|
type FormatterFunction = (logRecord: LogRecord) => string;
|
2020-04-09 07:45:24 -04:00
|
|
|
type LogMode = "a" | "w" | "x";
|
2019-01-19 13:46:46 -05:00
|
|
|
|
|
|
|
interface HandlerOptions {
|
|
|
|
formatter?: string | FormatterFunction;
|
|
|
|
}
|
|
|
|
|
2019-01-02 09:12:48 -05:00
|
|
|
export class BaseHandler {
|
|
|
|
level: number;
|
2020-04-25 05:13:26 -04:00
|
|
|
levelName: LevelName;
|
2019-01-19 13:46:46 -05:00
|
|
|
formatter: string | FormatterFunction;
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2020-04-25 05:13:26 -04:00
|
|
|
constructor(levelName: LevelName, 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
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
handle(logRecord: LogRecord): void {
|
2019-01-02 09:12:48 -05:00
|
|
|
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-11-13 13:42:34 -05:00
|
|
|
return this.formatter.replace(/{(\S+)}/g, (match, p1): string => {
|
|
|
|
const value = logRecord[p1 as keyof LogRecord];
|
2019-01-19 13:46:46 -05:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
// do not interpolate missing values
|
|
|
|
if (!value) {
|
|
|
|
return match;
|
2019-10-09 17:22:22 -04:00
|
|
|
}
|
2019-11-13 13:42:34 -05:00
|
|
|
|
|
|
|
return String(value);
|
|
|
|
});
|
2019-01-19 13:46:46 -05:00
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
log(_msg: string): void {}
|
|
|
|
async setup(): Promise<void> {}
|
|
|
|
async destroy(): Promise<void> {}
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ConsoleHandler extends BaseHandler {
|
2019-02-10 21:28:52 -05:00
|
|
|
format(logRecord: LogRecord): string {
|
|
|
|
let msg = super.format(logRecord);
|
|
|
|
|
|
|
|
switch (logRecord.level) {
|
2020-04-25 05:13:26 -04:00
|
|
|
case LogLevels.INFO:
|
2019-02-10 21:28:52 -05:00
|
|
|
msg = blue(msg);
|
|
|
|
break;
|
2020-04-25 05:13:26 -04:00
|
|
|
case LogLevels.WARNING:
|
2019-02-10 21:28:52 -05:00
|
|
|
msg = yellow(msg);
|
|
|
|
break;
|
2020-04-25 05:13:26 -04:00
|
|
|
case LogLevels.ERROR:
|
2019-02-10 21:28:52 -05:00
|
|
|
msg = red(msg);
|
|
|
|
break;
|
2020-04-25 05:13:26 -04:00
|
|
|
case LogLevels.CRITICAL:
|
2019-02-10 21:28:52 -05:00
|
|
|
msg = bold(red(msg));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
log(msg: string): void {
|
2019-01-02 09:12:48 -05:00
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class WriterHandler extends BaseHandler {
|
2020-06-12 15:23:38 -04:00
|
|
|
protected _writer!: Deno.Writer;
|
2020-04-09 07:45:24 -04:00
|
|
|
#encoder = new TextEncoder();
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2020-04-09 07:45:24 -04:00
|
|
|
abstract log(msg: string): void;
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
interface FileHandlerOptions extends HandlerOptions {
|
|
|
|
filename: string;
|
2020-04-09 07:45:24 -04:00
|
|
|
mode?: LogMode;
|
2019-01-19 13:46:46 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 09:12:48 -05:00
|
|
|
export class FileHandler extends WriterHandler {
|
2020-06-12 15:23:38 -04:00
|
|
|
protected _file: Deno.File | undefined;
|
2020-06-01 18:31:17 -04:00
|
|
|
protected _buf!: BufWriterSync;
|
2020-04-09 07:45:24 -04:00
|
|
|
protected _filename: string;
|
|
|
|
protected _mode: LogMode;
|
2020-06-12 15:23:38 -04:00
|
|
|
protected _openOptions: Deno.OpenOptions;
|
2020-06-01 18:31:17 -04:00
|
|
|
protected _encoder = new TextEncoder();
|
|
|
|
#unloadCallback = (): Promise<void> => this.destroy();
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2020-04-25 05:13:26 -04:00
|
|
|
constructor(levelName: LevelName, options: FileHandlerOptions) {
|
2019-01-19 13:46:46 -05:00
|
|
|
super(levelName, options);
|
|
|
|
this._filename = options.filename;
|
2020-04-09 07:45:24 -04:00
|
|
|
// default to append mode, write only
|
|
|
|
this._mode = options.mode ? options.mode : "a";
|
2020-04-24 18:45:55 -04:00
|
|
|
this._openOptions = {
|
|
|
|
createNew: this._mode === "x",
|
|
|
|
create: this._mode !== "x",
|
|
|
|
append: this._mode === "a",
|
|
|
|
truncate: this._mode !== "a",
|
|
|
|
write: true,
|
|
|
|
};
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
async setup(): Promise<void> {
|
2020-06-12 15:23:38 -04:00
|
|
|
this._file = await Deno.open(this._filename, this._openOptions);
|
2019-01-02 09:12:48 -05:00
|
|
|
this._writer = this._file;
|
2020-06-01 18:31:17 -04:00
|
|
|
this._buf = new BufWriterSync(this._file);
|
|
|
|
|
|
|
|
addEventListener("unload", this.#unloadCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
handle(logRecord: LogRecord): void {
|
|
|
|
super.handle(logRecord);
|
|
|
|
|
|
|
|
// Immediately flush if log level is higher than ERROR
|
|
|
|
if (logRecord.level > LogLevels.ERROR) {
|
|
|
|
this.flush();
|
|
|
|
}
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
|
|
|
|
2020-04-09 07:45:24 -04:00
|
|
|
log(msg: string): void {
|
2020-06-01 18:31:17 -04:00
|
|
|
this._buf.writeSync(this._encoder.encode(msg + "\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
flush(): void {
|
|
|
|
if (this._buf?.buffered() > 0) {
|
|
|
|
this._buf.flush();
|
|
|
|
}
|
2020-04-09 07:45:24 -04:00
|
|
|
}
|
|
|
|
|
2020-04-24 18:45:55 -04:00
|
|
|
destroy(): Promise<void> {
|
2020-06-01 18:31:17 -04:00
|
|
|
this.flush();
|
|
|
|
this._file?.close();
|
|
|
|
this._file = undefined;
|
|
|
|
removeEventListener("unload", this.#unloadCallback);
|
2020-04-24 18:45:55 -04:00
|
|
|
return Promise.resolve();
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
2019-01-06 14:19:15 -05:00
|
|
|
}
|
2020-04-09 07:45:24 -04:00
|
|
|
|
|
|
|
interface RotatingFileHandlerOptions extends FileHandlerOptions {
|
|
|
|
maxBytes: number;
|
|
|
|
maxBackupCount: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RotatingFileHandler extends FileHandler {
|
|
|
|
#maxBytes: number;
|
|
|
|
#maxBackupCount: number;
|
2020-05-29 02:39:33 -04:00
|
|
|
#currentFileSize = 0;
|
2020-04-09 07:45:24 -04:00
|
|
|
|
2020-04-25 05:13:26 -04:00
|
|
|
constructor(levelName: LevelName, options: RotatingFileHandlerOptions) {
|
2020-04-09 07:45:24 -04:00
|
|
|
super(levelName, options);
|
|
|
|
this.#maxBytes = options.maxBytes;
|
|
|
|
this.#maxBackupCount = options.maxBackupCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setup(): Promise<void> {
|
|
|
|
if (this.#maxBytes < 1) {
|
2020-06-01 18:31:17 -04:00
|
|
|
this.destroy();
|
2020-04-09 07:45:24 -04:00
|
|
|
throw new Error("maxBytes cannot be less than 1");
|
|
|
|
}
|
|
|
|
if (this.#maxBackupCount < 1) {
|
2020-06-01 18:31:17 -04:00
|
|
|
this.destroy();
|
2020-04-09 07:45:24 -04:00
|
|
|
throw new Error("maxBackupCount cannot be less than 1");
|
|
|
|
}
|
|
|
|
await super.setup();
|
|
|
|
|
|
|
|
if (this._mode === "w") {
|
|
|
|
// Remove old backups too as it doesn't make sense to start with a clean
|
|
|
|
// log file, but old backups
|
|
|
|
for (let i = 1; i <= this.#maxBackupCount; i++) {
|
|
|
|
if (await exists(this._filename + "." + i)) {
|
|
|
|
await Deno.remove(this._filename + "." + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (this._mode === "x") {
|
|
|
|
// Throw if any backups also exist
|
|
|
|
for (let i = 1; i <= this.#maxBackupCount; i++) {
|
|
|
|
if (await exists(this._filename + "." + i)) {
|
2020-06-01 18:31:17 -04:00
|
|
|
this.destroy();
|
2020-04-09 07:45:24 -04:00
|
|
|
throw new Deno.errors.AlreadyExists(
|
|
|
|
"Backup log file " + this._filename + "." + i + " already exists"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 02:39:33 -04:00
|
|
|
} else {
|
2020-06-12 15:23:38 -04:00
|
|
|
this.#currentFileSize = (await Deno.stat(this._filename)).size;
|
2020-04-09 07:45:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:31:17 -04:00
|
|
|
log(msg: string): void {
|
|
|
|
const msgByteLength = this._encoder.encode(msg).byteLength + 1;
|
2020-04-09 07:45:24 -04:00
|
|
|
|
2020-05-29 02:39:33 -04:00
|
|
|
if (this.#currentFileSize + msgByteLength > this.#maxBytes) {
|
2020-04-09 07:45:24 -04:00
|
|
|
this.rotateLogFiles();
|
2020-06-01 18:31:17 -04:00
|
|
|
this.#currentFileSize = 0;
|
2020-04-09 07:45:24 -04:00
|
|
|
}
|
|
|
|
|
2020-06-01 18:31:17 -04:00
|
|
|
this._buf.writeSync(this._encoder.encode(msg + "\n"));
|
|
|
|
this.#currentFileSize += msgByteLength;
|
2020-04-09 07:45:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rotateLogFiles(): void {
|
2020-06-01 18:31:17 -04:00
|
|
|
this._buf.flush();
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.close(this._file!.rid);
|
2020-04-09 07:45:24 -04:00
|
|
|
|
|
|
|
for (let i = this.#maxBackupCount - 1; i >= 0; i--) {
|
|
|
|
const source = this._filename + (i === 0 ? "" : "." + i);
|
|
|
|
const dest = this._filename + "." + (i + 1);
|
|
|
|
|
|
|
|
if (existsSync(source)) {
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.renameSync(source, dest);
|
2020-04-09 07:45:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
this._file = Deno.openSync(this._filename, this._openOptions);
|
2020-04-09 07:45:24 -04:00
|
|
|
this._writer = this._file;
|
2020-06-01 18:31:17 -04:00
|
|
|
this._buf = new BufWriterSync(this._file);
|
2020-04-09 07:45:24 -04:00
|
|
|
}
|
|
|
|
}
|