This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
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 default (line: string) => OUTPUT_LOGGER.log(line);
|
||||
Reference in New Issue
Block a user