58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import colors from "ansi-colors";
|
|
import { Formatter, Level, Logger } from "./log";
|
|
|
|
export class OutputFormatter extends Formatter {
|
|
private colorMap: Record<Level, colors.StyleFunction> = {
|
|
[Level.VERBOSE]: colors.white,
|
|
[Level.DEBUG]: colors.white,
|
|
[Level.INFO]: colors.cyan,
|
|
[Level.ERROR]: colors.red,
|
|
[Level.WARNING]: colors.yellow,
|
|
};
|
|
|
|
format(message: string, tag: string, level: Level): string {
|
|
const color = this.colorMap[level];
|
|
if (message[0] === "\t" || message.startsWith("Called from ")) {
|
|
return color(message);
|
|
} else {
|
|
const result = message.split(" ");
|
|
return colors.gray(result.slice(0, 4).join(" ")) + " " + color(result.slice(4).join(" "));
|
|
}
|
|
}
|
|
}
|
|
|
|
export const DEFAULT_OUTPUT_FORMATTER = new OutputFormatter();
|
|
|
|
export class OutputLogger extends Logger {
|
|
private levelMap: Record<string, Level> = {
|
|
"[DEBUG]": Level.DEBUG,
|
|
"[INFO]": Level.INFO,
|
|
"[ERROR]": Level.ERROR,
|
|
"Called from ": Level.ERROR,
|
|
"[WARNING]": Level.WARNING,
|
|
};
|
|
|
|
constructor() {
|
|
super("", Level.VERBOSE, DEFAULT_OUTPUT_FORMATTER);
|
|
}
|
|
|
|
detectLevel(line: string): number {
|
|
for (const [tag, color] of Object.entries(this.levelMap)) {
|
|
if (line.indexOf(tag) > -1) {
|
|
return color;
|
|
}
|
|
}
|
|
return Level.DEBUG;
|
|
}
|
|
|
|
log(line: string) {
|
|
this.write(this.detectLevel(line), line, []);
|
|
}
|
|
}
|
|
|
|
export const OUTPUT_LOGGER = new OutputLogger();
|
|
|
|
export const output = (line: string) => OUTPUT_LOGGER.log(line);
|
|
|
|
export default output;
|