Add Logger Component

This commit is contained in:
MrKBear 2021-11-26 19:05:25 +08:00
parent 70cf5cb1f5
commit 28ed5ad0c4
6 changed files with 481 additions and 3 deletions

View File

@ -1,4 +1,7 @@
// app.ts
import {Logger} from "./utils/Logger";
import * as label from "./utils/LogLabel";
App<IAppOption>({
/**
@ -10,5 +13,15 @@ App<IAppOption>({
*
*/
onLaunch() {
},
Logger.log("hh",
label.FatalLabel,label.ErrorLabel,label.WarnLabel,
label.InfoLabel,label.DebugLabel,label.TraceLabel
);
Logger.logM(
[label.FatalLabel,label.ErrorLabel,label.WarnLabel,
label.InfoLabel,label.DebugLabel,label.TraceLabel], "hh"
);
}
})

View File

@ -0,0 +1,31 @@
// APP 全局配置文件
/**
*
*
*
*
* 1Release false
* 2
*/
export const LOGGER_CONSOLE:boolean = true;
/**
*
* LogLabel
*
*
* 1 ||
* 2 &&
* 3
* 4使 ===
* 5 Filter
*/
export const LOGGER_FILTER:Array<RegExp | string>[] = [
// 调试输出全部内容
[/.*/],
// 输出警告和错误
// ["WARN", "ERROR", "FATAL"],
];

View File

@ -0,0 +1,43 @@
import {LogLabel, LogStyle} from "./Logger";
// 成功
export const SuccessLabel = new LogLabel(
"SUCCESS", new LogStyle().setColor("#FFFFFF", "#EE113D").setBorder("5px")
);
// 失败
export const FailedLabel = new LogLabel(
"SUCCESS", new LogStyle().setColor("#FFFFFF", "#33ff66").setBorder("3px")
);
// 致命
export const FatalLabel = new LogLabel(
"FATAL", new LogStyle().setColor("#FFFFFF", "#FF00CC").setBorder("3px")
);
// 错误
export const ErrorLabel = new LogLabel(
"ERROR", new LogStyle().setColor("#FFFFFF", "#FF0000").setBorder("3px")
);
// 警告
export const WarnLabel = new LogLabel(
"WARN", new LogStyle().setColor("#FFFFFF", "#FF9900").setBorder("3px")
);
// 消息
export const InfoLabel = new LogLabel(
"INFO", new LogStyle().setColor("#FFFFFF", "#99FF00").setBorder("3px")
);
// 调试
export const DebugLabel = new LogLabel(
"DEBUG", new LogStyle().setColor("#FFFFFF", "#00FF99").setBorder("3px")
);
// 追踪
export const TraceLabel = new LogLabel(
"TRACE", new LogStyle().setColor("#FFFFFF", "#00CCFF").setBorder("3px")
);

391
miniprogram/utils/Logger.ts Normal file
View File

@ -0,0 +1,391 @@
import {LOGGER_CONSOLE, LOGGER_FILTER} from "./Config";
/**
*
*/
class LogStyle {
/**
*
*/
private color:string | undefined;
/**
*
*/
private backgroundColor:string | undefined;
/**
*
*/
private weight:string | undefined;
/**
*
*/
private size:string | undefined;
/**
*
*/
private family:string | undefined;
/**
*
*/
private borderRadius:string | undefined;
/**
*
*/
private border:string | undefined;
/**
*
* @param color
* @param backgroundColor
*/
public setColor(color:string, backgroundColor?:string):LogStyle {
this.color = color;
this.backgroundColor = backgroundColor;
return this;
}
/**
*
* @param borderRadius
* @param border
*/
public setBorder(borderRadius:string, border?:string):LogStyle {
this.borderRadius = borderRadius;
this.border = border;
return this;
}
/**
*
* @param weight
* @param family
*/
public setFont(weight:string, family:string):LogStyle {
this.weight = weight;
this.family = family;
return this;
}
/**
*
* @param size
*/
public setSize(size:string):LogStyle {
this.size = size;
return this;
}
/**
*
*/
public stringify():string {
let stringArr:string[] = [];
this.color && stringArr.push(`color:${ this.color }`);
this.backgroundColor && stringArr.push(`background-color:${ this.backgroundColor }`);
this.weight && stringArr.push(`font-weight:${ this.weight }`);
this.family && stringArr.push(`font-family:${ this.family }`);
this.borderRadius && stringArr.push(`border-radius:${ this.borderRadius }`);
this.border && stringArr.push(`border:${ this.border }`);
this.size && stringArr.push(`font-size:${ this.size }`);
stringArr.push(`margin-bottom:5px`);
return stringArr.join(";");
}
}
/**
*
*/
class LogLabel {
/**
*
*
*/
public key:string;
/**
*
*/
public style:LogStyle;
/**
* @param key
* @param style
*/
constructor(key:string, style:LogStyle) {
this.key = key;
this.style = style;
}
/**
* Logger 使
*/
public getLoggerOutput():string {
return `%c ${ this.key } `;
}
/**
* Text
*/
public getTextOutput():string {
return `[${ this.key }]`;
}
/**
* style
*/
public getStyleOutput():string {
return this.style.stringify();
}
}
/**
*
*/
class StackInfo {
/**
*
*/
public functionName:string | undefined;
/**
*
*/
public fileName:string | undefined;
/**
*
*/
public url:string | undefined;
public setInfo(functionName:string, fileName:string, url:string):StackInfo {
this.functionName = functionName;
this.fileName = fileName;
this.url = url;
return this;
}
/**
*
*/
static getCallStack():StackInfo[] {
// 获取堆栈信息
let stack:string | undefined = new Error().stack;
if (stack === void 0) return [];
// 去除 Error
stack = stack.replace(/^(Error)\s/, "");
// 获取堆栈信息
let stackList:string[] = stack.split(/\n/);
let callStack:StackInfo[] = [];
for(let i = 0; i < stackList.length; i++) {
let matcher = stackList[i].match(/^\s+at\s+(.+)\s(\(.+\))/);
if (matcher === null || matcher.length < 3) continue;
let fileName = matcher[2].match(/.+\/(.+\..+:\d+:\d+)\)/);
if (fileName === null || matcher.length < 2) continue;
callStack.push(new StackInfo().setInfo(
matcher[1], fileName[1], matcher[2]
))
}
return callStack;
}
/**
*
*/
static readonly excludeFile:RegExp = /^Logger\.js:\d+:\d+/;
/**
*
*/
static getFirstStack():StackInfo | undefined {
let callStack = this.getCallStack();
for(let i = 0; i < callStack.length; i++) {
if(!callStack[i].fileName) continue;
if(!StackInfo.excludeFile.test(callStack[i].fileName ?? "")) {
return callStack[i];
}
}
return;
}
}
/**
*
* log
*/
class MultipleLogContent {
/**
*
*/
private content:any[];
/**
* @param content
*/
public constructor(...content:any[]) {
this.content = content;
}
/**
*
*/
public getContent():any[] {
return this.content;
}
}
/**
*
*/
class Logger {
/**
*
*/
static readonly pathStyle:LogStyle = new LogStyle().setColor("#CCCCCC");
/**
*
*/
static filterLog(filter:Array<RegExp | string>, labels:LogLabel[]):boolean {
let passNum:number = 0;
for(let i = 0; i < filter.length; i++) {
let pass:boolean = false;
for(let j = 0; j < labels.length; j++) {
if(filter[i] instanceof RegExp) {
pass = (filter[i] as RegExp).test(labels[j].key)
} else {
pass = (filter[i] as String) === labels[j].key;
}
if(pass) break;
}
if(pass) passNum ++;
}
return passNum === filter.length;
}
/**
*
* @param labels 使
*/
static testLog(...labels:LogLabel[]):boolean {
if(!LOGGER_CONSOLE) return false;
let isLogging = false;
for(let i = 0; i < LOGGER_FILTER.length; i++) {
// 判断是否进行输出
isLogging = Logger.filterLog(LOGGER_FILTER[i], labels);
if(isLogging) break;
}
return isLogging;
}
/**
*
* @param styledLabel calcStyle的处理结果
*/
static addFileNameLabel():LogLabel {
// 获得调用堆栈
let stack = StackInfo.getFirstStack();
return new LogLabel(stack?.fileName ?? "", Logger.pathStyle);
}
/**
*
* @param labels 使
*/
static calcStyle(...labels:LogLabel[]):[string[], string[]] {
let consoleLabels:string[] = [];
let consoleStyles:string[] = [];
// 放置标签
for(let i = 0; i < labels.length; i++) {
consoleLabels.push(labels[i].getLoggerOutput());
if (i !== ( labels.length - 1))
consoleLabels.push("%c ");
consoleStyles.push(labels[i].getStyleOutput());
if (i !== ( labels.length - 1))
consoleStyles.push("");
}
return [consoleLabels, consoleStyles];
}
/**
*
* @param content
* @param label 使
*/
static log<T>(content:T, ...labels:LogLabel[]):T {
let fileNameLabel = Logger.addFileNameLabel();
if(!Logger.testLog(...labels, fileNameLabel)) return content;
let styledLabel = Logger.calcStyle(...labels);
console.log(
styledLabel[0].join("") + fileNameLabel.getLoggerOutput(),
...[...styledLabel[1], fileNameLabel.getStyleOutput()],
content
);
return content;
}
/**
*
* @param labels
* @param content 使
*/
static logM<T>(labels:LogLabel[], ...content:T[]):T[] {
return Logger.log<T[]>(content, ...labels);
}
}
export default Logger;
export {Logger, LogStyle, LogLabel}

View File

@ -17,7 +17,7 @@
"newFeature": false,
"coverView": true,
"nodeModules": false,
"autoAudits": true,
"autoAudits": false,
"showShadowRootInWxmlPanel": true,
"scopeDataCheck": false,
"uglifyFileName": true,