mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
core: 整合tag与category
This commit is contained in:
1 parent
454afbcb08
commit
7a41fecb0e
15 files changed
+116
-117
No files matched your search
@@ -1,11 +1,19 @@
|
||||
import { inspect, format } from 'util';
|
||||
import { inspect, format, InspectOptions } from 'util';
|
||||
import { argv } from 'yargs';
|
||||
import { Time } from './utils';
|
||||
|
||||
const colors = [
|
||||
20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62,
|
||||
63, 68, 69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113,
|
||||
129, 134, 135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168,
|
||||
169, 170, 171, 172, 173, 178, 179, 184, 185, 196, 197, 198, 199, 200,
|
||||
201, 202, 203, 204, 205, 206, 207, 208, 209, 214, 215, 220, 221,
|
||||
];
|
||||
const instances: Record<string, Logger> = {};
|
||||
type LogFunction = (format: any, ...param: any[]) => boolean;
|
||||
type LogFunction = (format: any, ...param: any[]) => void;
|
||||
type LogType = 'success' | 'error' | 'info' | 'warn' | 'debug';
|
||||
export interface Logger extends Record<LogType, LogFunction> { }
|
||||
|
||||
export class Logger {
|
||||
static readonly SUCCESS = 1;
|
||||
static readonly ERROR = 1;
|
||||
@@ -16,10 +24,24 @@ export class Logger {
|
||||
static showDiff = false;
|
||||
static levels: Record<string, number> = {};
|
||||
static lastTime = 0;
|
||||
static formatters: Record<string, (this: Logger, value: any) => string> = {
|
||||
o: (value) => inspect(value).replace(/\s*\n\s*/g, ' '),
|
||||
private code: number;
|
||||
private displayName: string;
|
||||
public stream: NodeJS.WritableStream = process.stderr;
|
||||
static options: InspectOptions = {
|
||||
colors: process.stderr.isTTY,
|
||||
};
|
||||
|
||||
static formatters: Record<string, (this: Logger, value: any) => string> = {
|
||||
c: Logger.prototype.color,
|
||||
C: (value) => Logger.color(15, value, ';1'),
|
||||
o: (value) => inspect(value, Logger.options).replace(/\s*\n\s*/g, ' '),
|
||||
};
|
||||
|
||||
static color(code: number, value: any, decoration = '') {
|
||||
if (!Logger.options.colors) return `${value}`;
|
||||
return `\u001B[3${code < 8 ? code : `8;5;${code}`}${decoration}m${value}\u001B[0m`;
|
||||
}
|
||||
|
||||
constructor(public name: string, private showDiff = false) {
|
||||
if (name in instances) return instances[name];
|
||||
let hash = 0;
|
||||
@@ -28,6 +50,8 @@ export class Logger {
|
||||
hash |= 0;
|
||||
}
|
||||
instances[name] = this;
|
||||
this.code = colors[Math.abs(hash) % colors.length];
|
||||
this.displayName = name ? this.color(`${name} `, ';1') : '';
|
||||
this.createMethod('success', '[S] ', Logger.SUCCESS);
|
||||
this.createMethod('error', '[E] ', Logger.ERROR);
|
||||
this.createMethod('info', '[I] ', Logger.INFO);
|
||||
@@ -35,10 +59,14 @@ export class Logger {
|
||||
this.createMethod('debug', '[D] ', Logger.DEBUG);
|
||||
}
|
||||
|
||||
private color(value: any, decoration = '') {
|
||||
return Logger.color(this.code, value, decoration);
|
||||
}
|
||||
|
||||
private createMethod(name: LogType, prefix: string, minLevel: number) {
|
||||
this[name] = (...args: [any, ...any[]]) => {
|
||||
if (this.level < minLevel) return false;
|
||||
const msg = `${prefix} ${this.name} ${this.format(...args)}`;
|
||||
const msg = `${prefix} ${this.displayName} ${this.format(...args)}`;
|
||||
if (process.send) process.send({ event: 'message/log', payload: [msg] });
|
||||
else global.Hydro.service.bus.parallel('message/log', msg);
|
||||
return true;
|
||||
@@ -68,7 +96,9 @@ export class Logger {
|
||||
}).split('\n').join('\n ');
|
||||
if (Logger.showDiff || this.showDiff) {
|
||||
const now = Date.now();
|
||||
if (Logger.lastTime) args.push(`+${Time.formatTimeShort(now - Logger.lastTime)}`);
|
||||
if (Logger.lastTime) {
|
||||
args.push(this.color(`+${Time.formatTimeShort(now - Logger.lastTime)}`));
|
||||
}
|
||||
Logger.lastTime = now;
|
||||
}
|
||||
return format(...args);
|
||||
|
||||
Reference in new issue
Block a user