This commit is contained in:
+57
-34
@@ -1,63 +1,86 @@
|
||||
import colors from "ansi-colors";
|
||||
|
||||
const Level = {
|
||||
VERBOSE: -1,
|
||||
DEBUG: 0,
|
||||
INFO: 1,
|
||||
WARN: 2,
|
||||
ERROR: 3,
|
||||
};
|
||||
export enum Level {
|
||||
VERBOSE = -1,
|
||||
DEBUG = 0,
|
||||
INFO = 1,
|
||||
WARNING = 2,
|
||||
ERROR = 3,
|
||||
}
|
||||
|
||||
const level = process.argv.indexOf("--verbose") > -1 ? Level.VERBOSE : Level.INFO;
|
||||
export const DEFAULT_LEVEL: Level = process.argv.indexOf("--verbose") > -1 ? Level.VERBOSE : Level.INFO;
|
||||
|
||||
const formatTime = (date: Date): string => date.toTimeString().split(" ")[0];
|
||||
export type FormatPattern = [string, colors.StyleFunction];
|
||||
|
||||
export class Log {
|
||||
static level = level;
|
||||
static colorPatterns: [string, (m: string) => string][] = [
|
||||
["_", colors.cyan],
|
||||
["\\*", colors.magenta],
|
||||
["!", colors.red],
|
||||
];
|
||||
export const DEFAULT_PATTERNS: FormatPattern[] = [
|
||||
["_", colors.cyan],
|
||||
["\\*", colors.magenta],
|
||||
["!", colors.red],
|
||||
];
|
||||
|
||||
static colorize(pattern: string) {
|
||||
for (const item of this.colorPatterns) {
|
||||
export class Formatter {
|
||||
constructor(
|
||||
private patterns: FormatPattern[] = DEFAULT_PATTERNS,
|
||||
private time: boolean = true,
|
||||
) {}
|
||||
|
||||
colorize(text: string): string {
|
||||
for (const item of this.patterns) {
|
||||
const r = new RegExp(item[0] + "(.*?)" + item[0], "g");
|
||||
pattern = pattern.replace(r, item[1]("$1"));
|
||||
text = text.replace(r, item[1]("$1"));
|
||||
}
|
||||
return pattern;
|
||||
return text;
|
||||
}
|
||||
|
||||
static write(level: number, tag: string, pattern: string, args: any[]) {
|
||||
buildTime(): string {
|
||||
return new Date().toTimeString().split(" ")[0];
|
||||
}
|
||||
|
||||
format(message: string, tag: string, level: Level): string {
|
||||
const values = [];
|
||||
if (this.time) {
|
||||
values.push(`[${colors.gray(this.buildTime())}]`);
|
||||
}
|
||||
if (tag) {
|
||||
values.push(colors.green(tag));
|
||||
}
|
||||
values.push(this.colorize(message));
|
||||
return values.join(" ");
|
||||
}
|
||||
}
|
||||
|
||||
export const DEFAULT_FORMATTER = new Formatter();
|
||||
|
||||
export class Logger {
|
||||
constructor(
|
||||
private tag: string,
|
||||
private level: Level = DEFAULT_LEVEL,
|
||||
private formatter: Formatter = DEFAULT_FORMATTER,
|
||||
) {}
|
||||
|
||||
write(level: Level, message: string, args: any[]) {
|
||||
if (level >= this.level) {
|
||||
const message = `[${colors.gray(formatTime(new Date()))}] ${colors.green(tag)} ${this.colorize(pattern)}`;
|
||||
console.log(message, ...args);
|
||||
console.log(this.formatter.format(message, this.tag, level), ...args);
|
||||
}
|
||||
}
|
||||
|
||||
public tag: string;
|
||||
|
||||
constructor(tag: string) {
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
v(message: string, ...args: any) {
|
||||
Log.write(-1, this.tag, message, args);
|
||||
this.write(Level.VERBOSE, message, args);
|
||||
}
|
||||
|
||||
d(message: string, ...args: any) {
|
||||
Log.write(0, this.tag, message, args);
|
||||
this.write(Level.DEBUG, message, args);
|
||||
}
|
||||
|
||||
i(message: string, ...args: any) {
|
||||
Log.write(1, this.tag, message, args);
|
||||
this.write(Level.INFO, message, args);
|
||||
}
|
||||
|
||||
w(message: string, ...args: any) {
|
||||
Log.write(2, this.tag, message, args);
|
||||
this.write(Level.WARNING, message, args);
|
||||
}
|
||||
|
||||
e(message: string, ...args: any) {
|
||||
Log.write(3, this.tag, message, args);
|
||||
this.write(Level.ERROR, message, args);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user