refactor: format log.ts
This commit is contained in:
+16
-15
@@ -14,20 +14,21 @@ const formatTime = (date: Date): string => date.toTimeString().split(" ")[0];
|
|||||||
|
|
||||||
export class Log {
|
export class Log {
|
||||||
static level = level;
|
static level = level;
|
||||||
|
static colorPatterns: [string, (m: string) => string][] = [
|
||||||
static colorize(pattern: string) {
|
|
||||||
for (const item of [
|
|
||||||
["_", colors.cyan],
|
["_", colors.cyan],
|
||||||
["\\*", colors.magenta],
|
["\\*", colors.magenta],
|
||||||
["!", colors.red],
|
["!", colors.red],
|
||||||
]) {
|
];
|
||||||
|
|
||||||
|
static colorize(pattern: string) {
|
||||||
|
for (const item of this.colorPatterns) {
|
||||||
const r = new RegExp(item[0] + "(.*?)" + item[0], "g");
|
const r = new RegExp(item[0] + "(.*?)" + item[0], "g");
|
||||||
pattern = pattern.replace(r, item[1]("$1"));
|
pattern = pattern.replace(r, item[1]("$1"));
|
||||||
}
|
}
|
||||||
return pattern;
|
return pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
static write(level: number, tag: string, pattern: string, ...args: any) {
|
static write(level: number, tag: string, pattern: string, args: any[]) {
|
||||||
if (level >= this.level) {
|
if (level >= this.level) {
|
||||||
const message = `[${colors.gray(formatTime(new Date()))}] ${colors.green(tag)} ${this.colorize(pattern)}`;
|
const message = `[${colors.gray(formatTime(new Date()))}] ${colors.green(tag)} ${this.colorize(pattern)}`;
|
||||||
console.log(message, ...args);
|
console.log(message, ...args);
|
||||||
@@ -40,23 +41,23 @@ export class Log {
|
|||||||
this.tag = tag;
|
this.tag = tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
v(...args: any) {
|
v(message: string, ...args: any) {
|
||||||
Log.write(-1, this.tag, ...args);
|
Log.write(-1, this.tag, message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
d(...args: any) {
|
d(message: string, ...args: any) {
|
||||||
Log.write(0, this.tag, ...args);
|
Log.write(0, this.tag, message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
i(...args: any) {
|
i(message: string, ...args: any) {
|
||||||
Log.write(1, this.tag, ...args);
|
Log.write(1, this.tag, message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
w(...args: any) {
|
w(message: string, ...args: any) {
|
||||||
Log.write(2, this.tag, ...args);
|
Log.write(2, this.tag, message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
e(...args: any) {
|
e(message: string, ...args: any) {
|
||||||
Log.write(3, this.tag, ...args);
|
Log.write(3, this.tag, message, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user