Separate logger into multiple sub-modules

This commit is contained in:
MrKBear 2021-11-27 18:15:39 +08:00
parent 28ed5ad0c4
commit cf88f314f1
15 changed files with 1021 additions and 126 deletions

3
.gitignore vendored
View File

@ -12,3 +12,6 @@ $RECYCLE.BIN/
# Node.js
node_modules/
# wxss
*.wxss

View File

@ -1,5 +1,5 @@
import {Logger} from "./utils/Logger";
import * as label from "./utils/LogLabel";
import { Logger, LevelLogLabel } from "./logger/index";
import { Level } from "./utils/LogLabel";
App<IAppOption>({
@ -14,14 +14,24 @@ App<IAppOption>({
*/
onLaunch() {
Logger.log("hh",
label.FatalLabel,label.ErrorLabel,label.WarnLabel,
label.InfoLabel,label.DebugLabel,label.TraceLabel
);
console.log(Logger.l({val:"hh"},
LevelLogLabel.FatalLabel,LevelLogLabel.ErrorLabel,LevelLogLabel.WarnLabel,
LevelLogLabel.InfoLabel,LevelLogLabel.DebugLabel,LevelLogLabel.TraceLabel
));
Logger.logM(
[label.FatalLabel,label.ErrorLabel,label.WarnLabel,
label.InfoLabel,label.DebugLabel,label.TraceLabel], "hh"
);
console.log(Logger.m(
[LevelLogLabel.FatalLabel,LevelLogLabel.ErrorLabel,LevelLogLabel.WarnLabel,
LevelLogLabel.InfoLabel,LevelLogLabel.DebugLabel,LevelLogLabel.TraceLabel], {val:"hh"}, "hh"
));
console.log(Logger.ll({val:"hh"},
LevelLogLabel.FatalLabel,LevelLogLabel.ErrorLabel,LevelLogLabel.WarnLabel,
LevelLogLabel.InfoLabel,LevelLogLabel.DebugLabel,LevelLogLabel.TraceLabel
));
console.log(Logger.lm(
[LevelLogLabel.FatalLabel,LevelLogLabel.ErrorLabel,LevelLogLabel.WarnLabel,
LevelLogLabel.InfoLabel,LevelLogLabel.DebugLabel,LevelLogLabel.TraceLabel], {val:"hh"}, "hh"
));
}
})

View File

@ -0,0 +1,61 @@
import { LogStyle } from "./LogStyle";
import { LogLabel } from "./LogLabel";
import { StackInfo } from "./StackInfo";
/**
* LogLabel
*/
class InternalLogLabel {
/**
*
*/
public static readonly normalStyle:LogStyle = new LogStyle()
.setColor("#CCCCCC").setBorder("4px", "1px solid #979797").setBlank("0 5px");
/**
*
*/
public static readonly blankLabel = new LogLabel("\n\r",
new LogStyle(), false, true, true);
/**
* label
*/
public static get fileNameLabel():LogLabel {
// 获得调用堆栈
let stack = StackInfo.getFirstStack();
return new LogLabel(stack?.fileName ?? "Unknown file name",
InternalLogLabel.normalStyle, false, true, true);
}
/**
* URL label
*/
public static get urlLabel():LogLabel {
// 获得调用堆栈
let stack = StackInfo.getFirstStack();
return new LogLabel(stack?.url ?? "Unknown url",
InternalLogLabel.normalStyle, false, true, true);
}
/**
* filter URL label
*/
public static get filterUrlLabel():LogLabel {
// 获得调用堆栈
let stack = StackInfo.getFirstStack();
return new LogLabel(stack?.url ?? "Unknown url",
InternalLogLabel.normalStyle, true, false, true);
}
}
export default InternalLogLabel;
export {InternalLogLabel};

View File

@ -0,0 +1,61 @@
import { LogStyle } from "./LogStyle";
import { LogLabel } from "./LogLabel";
/**
*
*/
const normalStyleGen = (color:string):LogStyle => {
return new LogStyle().setBorder("4px", `1px solid ${color}`)
.setColor(color).setBlank("0 5px");
}
/**
*
*/
class LevelLogLabel {
/**
*
*/
static readonly FatalLabel = new LogLabel(
"FATAL", normalStyleGen("#FF00CC")
);
/**
*
*/
static readonly ErrorLabel = new LogLabel(
"ERROR", normalStyleGen("#FF0000")
);
/**
*
*/
static readonly WarnLabel = new LogLabel(
"WARN", normalStyleGen("#FF9900")
);
/**
*
*/
static readonly InfoLabel = new LogLabel(
"INFO", normalStyleGen("#99FF00")
);
/**
*
*/
static readonly DebugLabel = new LogLabel(
"DEBUG", normalStyleGen("#00FF99")
);
/**
*
*/
static readonly TraceLabel = new LogLabel(
"TRACE", normalStyleGen("#00CCFF")
);
}
export default LevelLogLabel;
export { LevelLogLabel };

View File

@ -0,0 +1,93 @@
import { LogStyle } from "./LogStyle";
/**
*
*/
class LogLabel {
/**
*
*
*/
public key:string;
/**
*
*/
public style:LogStyle;
/**
*
*/
public checked:boolean;
/**
*
*/
public display:boolean;
/**
*
*
*/
public attach:boolean;
/**
* @param key
* @param style
*/
constructor(key:string, style:LogStyle,
checked?:boolean, display?:boolean, attach?:boolean) {
this.key = key;
this.style = style;
this.checked = checked ?? true;
this.display = display ?? true;
this.attach = attach ?? false;
}
/**
* Logger 使
*/
public getLoggerOutput():string {
if(!this.display) return "";
return `%c${ this.key }`;
}
/**
* Text
*/
public getTextOutput():string {
if(!this.display) return "";
return `[${ this.key }]`;
}
/**
* style
*/
public getStyleOutput():string {
if(!this.display) return "";
return this.style.stringify();
}
/**
*
*/
public checking(src:RegExp | string):boolean {
let pass = false;
// 关闭校验
if(!this.checked) return pass;
if(src instanceof RegExp) {
pass = (src as RegExp).test(this.key)
} else {
pass = (src as string) === this.key;
}
return pass;
}
}
export default LogLabel;
export {LogLabel}

View File

@ -0,0 +1,138 @@
/**
*
*/
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;
/**
*
*/
private margin:string | undefined;
/**
*
*/
private padding:string | undefined;
/**
*
* @param color
* @param backgroundColor
*/
public setColor(color?:string, backgroundColor?:string):LogStyle {
this.color = color ?? this.color;
this.backgroundColor = backgroundColor ?? this.backgroundColor;
return this;
}
/**
*
* @param borderRadius
* @param border
*/
public setBorder(borderRadius?:string, border?:string):LogStyle {
this.borderRadius = borderRadius ?? this.borderRadius;
this.border = border ?? this.border;
return this;
}
/**
*
* @param weight
* @param family
*/
public setFont(weight?:string, family?:string):LogStyle {
this.weight = weight ?? this.weight;
this.family = family ?? this.family;
return this;
}
/**
*
* @param size
*/
public setSize(size?:string):LogStyle {
this.size = size ?? this.size;
return this;
}
/**
*
* @param padding
* @param margin
*/
public setBlank(padding?:string, margin?:string):LogStyle {
this.padding = padding ?? this.padding;
this.margin = margin ?? this.margin;
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 }`);
this.padding && stringArr.push(`padding:${ this.padding }`);
this.margin && stringArr.push(`margin:${ this.margin }`);
return stringArr.join(";");
}
/**
* LogStyle
*/
public clone():LogStyle {
return new LogStyle()
.setColor(this.color, this.backgroundColor)
.setBorder(this.borderRadius, this.border)
.setFont(this.weight, this.family)
.setBlank(this.padding, this.margin)
.setSize(this.size)
}
}
export default LogStyle;
export {LogStyle};

View File

@ -0,0 +1,179 @@
import {LOGGER_CONSOLE, LOGGER_FILTER} from "../Config";
import { InternalLogLabel } from "./InternalLogLabel";
import { LogLabel } from "./LogLabel";
import { MultipleLogContent } from "./MultipleLogContent";
/**
*
*/
class Logger {
/**
*
*/
public 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++) {
pass = labels[j].checking(filter[i]);
if(pass) break;
}
if(pass) passNum ++;
}
return passNum === filter.length;
}
/**
*
* @param labels 使
*/
public 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 labels 使
*/
public static calcStyle(...labels:LogLabel[]):[string[], string[]] {
// 过滤出需要显示的 Labels
let labelsNeedRender:LogLabel[] = labels.filter((label:LogLabel)=>{
return label.display
});
let consoleLabels:string[] = [];
let consoleStyles:string[] = [];
// 放置标签
for(let i = 0; i < labelsNeedRender.length; i++) {
consoleLabels.push(labels[i].getLoggerOutput());
if (i !== ( labelsNeedRender.length - 1))
consoleLabels.push("%c ");
consoleStyles.push(labelsNeedRender[i].getStyleOutput());
if (i !== ( labelsNeedRender.length - 1))
consoleStyles.push("");
}
return [consoleLabels, consoleStyles];
}
/**
*
* Log
* @param content
* @param label 使
* @param attachLabel
*/
public static logBase<T extends Array<any>>
(content:MultipleLogContent<T>, labels:LogLabel[], attachLabel:LogLabel[] = []):T {
// TODO: 这里可以添加一些钩子作为中间件处理日志输出
// 测试是否输出内容
if(!Logger.testLog(...labels, ...attachLabel, InternalLogLabel.filterUrlLabel))
return content.getContent();
// 计算收集样式
let [consoleLabels, consoleStyles]= Logger.calcStyle(...labels, ...attachLabel);
// 调试输出
console.log(consoleLabels.join(""), ...consoleStyles, ...content.getContent());
return content.getContent();
}
/**
*
* @param content
* @param label 使
*/
public static log<T>(content:T, ...labels:LogLabel[]):T {
return Logger.logBase<Array<T>>(
new MultipleLogContent<Array<T>>(content), labels,
[InternalLogLabel.fileNameLabel]
)[0];
}
/**
* Logger.log
*/
public static l:typeof Logger.log = Logger.log;
/**
*
* @param labels
* @param content 使
*/
public static logMultiple<T extends Array<any>>(labels:LogLabel[], ...content:T):T {
return Logger.logBase<T>(
new MultipleLogContent<T>(...content), labels,
[InternalLogLabel.fileNameLabel]
);
}
/**
* Logger.logMultiple
*/
public static m:typeof Logger.logMultiple = Logger.logMultiple;
/**
*
* @param content
* @param label 使
*/
public static logLine<T>(content:T, ...labels:LogLabel[]):T {
return Logger.logBase<Array<T>>(
new MultipleLogContent<Array<T>>(content), labels,
[InternalLogLabel.urlLabel, InternalLogLabel.blankLabel]
)[0];
}
/**
* Logger.logMultiple
*/
public static ll:typeof Logger.logLine = Logger.logLine;
/**
*
* @param labels
* @param content 使
*/
public static logLineMultiple<T extends Array<any>>(labels:LogLabel[], ...content:T):T {
return Logger.logBase<T>(
new MultipleLogContent<T>(...content), labels,
[InternalLogLabel.urlLabel, InternalLogLabel.blankLabel]
);
}
/**
* Logger.logLineMultiple
*/
public static lm:typeof Logger.logLineMultiple = Logger.logLineMultiple;
}
export default Logger;
export { Logger };

View File

@ -0,0 +1,29 @@
/**
*
* log
*/
class MultipleLogContent<T extends Array<any>> {
/**
*
*/
private readonly content:T;
/**
* @param content
*/
public constructor(...content:T) {
this.content = content;
}
/**
*
*/
public getContent():T {
return this.content;
}
}
export default MultipleLogContent;
export { MultipleLogContent };

View File

@ -0,0 +1,91 @@
/**
*
*/
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;
}
/**
*
*/
public 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]?.replace(/(\(|\))/g, "")
))
}
// console.log(callStack);
return callStack;
}
/**
*
*/
public static readonly excludeFile:RegExp = /^Logger\.js:\d+:\d+/;
/**
*
*/
public 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;
}
}
export default StackInfo;
export { StackInfo };

View File

@ -0,0 +1,11 @@
import Logger from "./Logger";
export default Logger;
export { Logger };
export { InternalLogLabel } from "./InternalLogLabel";
export { MultipleLogContent } from "./MultipleLogContent";
export { LevelLogLabel } from "./LevelLogLabel";
export { LogLabel } from "./LogLabel";
export { LogStyle } from "./LogStyle";
export { StackInfo } from "./StackInfo";

View File

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

View File

@ -1,4 +1,4 @@
import {LOGGER_CONSOLE, LOGGER_FILTER} from "./Config";
import {LOGGER_CONSOLE, LOGGER_FILTER} from "../Config";
/**
*
@ -40,14 +40,24 @@ class LogStyle {
*/
private border:string | undefined;
/**
*
*/
private margin:string | undefined;
/**
*
*/
private padding:string | undefined;
/**
*
* @param color
* @param backgroundColor
*/
public setColor(color:string, backgroundColor?:string):LogStyle {
this.color = color;
this.backgroundColor = backgroundColor;
public setColor(color?:string, backgroundColor?:string):LogStyle {
this.color = color ?? this.color;
this.backgroundColor = backgroundColor ?? this.backgroundColor;
return this;
}
@ -56,9 +66,9 @@ class LogStyle {
* @param borderRadius
* @param border
*/
public setBorder(borderRadius:string, border?:string):LogStyle {
this.borderRadius = borderRadius;
this.border = border;
public setBorder(borderRadius?:string, border?:string):LogStyle {
this.borderRadius = borderRadius ?? this.borderRadius;
this.border = border ?? this.border;
return this;
}
@ -67,9 +77,9 @@ class LogStyle {
* @param weight
* @param family
*/
public setFont(weight:string, family:string):LogStyle {
this.weight = weight;
this.family = family;
public setFont(weight?:string, family?:string):LogStyle {
this.weight = weight ?? this.weight;
this.family = family ?? this.family;
return this;
}
@ -77,8 +87,19 @@ class LogStyle {
*
* @param size
*/
public setSize(size:string):LogStyle {
this.size = size;
public setSize(size?:string):LogStyle {
this.size = size ?? this.size;
return this;
}
/**
*
* @param padding
* @param margin
*/
public setBlank(padding?:string, margin?:string):LogStyle {
this.padding = padding ?? this.padding;
this.margin = margin ?? this.margin;
return this;
}
@ -95,11 +116,23 @@ class LogStyle {
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`);
this.padding && stringArr.push(`padding:${ this.padding }`);
this.margin && stringArr.push(`margin:${ this.margin }`);
return stringArr.join(";");
}
/**
* LogStyle
*/
public clone():LogStyle {
return new LogStyle()
.setColor(this.color, this.backgroundColor)
.setBorder(this.borderRadius, this.border)
.setFont(this.weight, this.family)
.setBlank(this.padding, this.margin)
.setSize(this.size)
}
}
/**
@ -118,19 +151,40 @@ class LogLabel {
*/
public style:LogStyle;
/**
*
*/
public checked:boolean;
/**
*
*/
public display:boolean;
/**
*
*
*/
public attach:boolean;
/**
* @param key
* @param style
*/
constructor(key:string, style:LogStyle) {
constructor(key:string, style:LogStyle,
checked?:boolean, display?:boolean, attach?:boolean) {
this.key = key;
this.style = style;
this.checked = checked ?? true;
this.display = display ?? true;
this.attach = attach ?? false;
}
/**
* Logger 使
*/
public getLoggerOutput():string {
if(!this.display) return "";
return `%c${ this.key }`;
}
@ -138,6 +192,7 @@ class LogLabel {
* Text
*/
public getTextOutput():string {
if(!this.display) return "";
return `[${ this.key }]`;
}
@ -145,8 +200,28 @@ class LogLabel {
* style
*/
public getStyleOutput():string {
if(!this.display) return "";
return this.style.stringify();
}
/**
*
*/
public checking(src:RegExp | string):boolean {
let pass = false;
// 关闭校验
if(!this.checked) return pass;
if(src instanceof RegExp) {
pass = (src as RegExp).test(this.key)
} else {
pass = (src as string) === this.key;
}
return pass;
}
}
/**
@ -179,7 +254,7 @@ class StackInfo {
/**
*
*/
static getCallStack():StackInfo[] {
public static getCallStack():StackInfo[] {
// 获取堆栈信息
let stack:string | undefined = new Error().stack;
@ -203,22 +278,24 @@ class StackInfo {
if (fileName === null || matcher.length < 2) continue;
callStack.push(new StackInfo().setInfo(
matcher[1], fileName[1], matcher[2]
matcher[1], fileName[1], matcher[2]?.replace(/(\(|\))/g, "")
))
}
// console.log(callStack);
return callStack;
}
/**
*
*/
static readonly excludeFile:RegExp = /^Logger\.js:\d+:\d+/;
public static readonly excludeFile:RegExp = /^Logger\.js:\d+:\d+/;
/**
*
*/
static getFirstStack():StackInfo | undefined {
public static getFirstStack():StackInfo | undefined {
let callStack = this.getCallStack();
@ -235,28 +312,83 @@ class StackInfo {
}
}
/**
* LogLabel
*/
class InternalLogLabel {
/**
*
*/
public static readonly normalStyle:LogStyle = new LogStyle()
.setColor("#CCCCCC").setBorder("4px", "1px solid #979797").setBlank("0 5px");
/**
*
*/
public static readonly blankLabel = new LogLabel("\n\r",
new LogStyle(), false, true, true);
/**
* label
*/
public static get fileNameLabel():LogLabel {
// 获得调用堆栈
let stack = StackInfo.getFirstStack();
return new LogLabel(stack?.fileName ?? "Unknown file name",
InternalLogLabel.normalStyle, false, true, true);
}
/**
* URL label
*/
public static get urlLabel():LogLabel {
// 获得调用堆栈
let stack = StackInfo.getFirstStack();
return new LogLabel(stack?.url ?? "Unknown url",
InternalLogLabel.normalStyle, false, true, true);
}
/**
* filter URL label
*/
public static get filterUrlLabel():LogLabel {
// 获得调用堆栈
let stack = StackInfo.getFirstStack();
return new LogLabel(stack?.url ?? "Unknown url",
InternalLogLabel.normalStyle, true, false, true);
}
}
/**
*
* log
*/
class MultipleLogContent {
class MultipleLogContent<T extends Array<any>> {
/**
*
*/
private content:any[];
private readonly content:T;
/**
* @param content
*/
public constructor(...content:any[]) {
public constructor(...content:T) {
this.content = content;
}
/**
*
*/
public getContent():any[] {
public getContent():T {
return this.content;
}
}
@ -266,15 +398,10 @@ class MultipleLogContent {
*/
class Logger {
/**
*
*/
static readonly pathStyle:LogStyle = new LogStyle().setColor("#CCCCCC");
/**
*
*/
static filterLog(filter:Array<RegExp | string>, labels:LogLabel[]):boolean {
public static filterLog(filter:Array<RegExp | string>, labels:LogLabel[]):boolean {
let passNum:number = 0;
@ -283,12 +410,7 @@ class Logger {
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;
}
pass = labels[j].checking(filter[i]);
if(pass) break;
}
@ -302,7 +424,7 @@ class Logger {
*
* @param labels 使
*/
static testLog(...labels:LogLabel[]):boolean {
public static testLog(...labels:LogLabel[]):boolean {
if(!LOGGER_CONSOLE) return false;
@ -318,74 +440,130 @@ class Logger {
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[]] {
public static calcStyle(...labels:LogLabel[]):[string[], string[]] {
// 过滤出需要显示的 Labels
let labelsNeedRender:LogLabel[] = labels.filter((label:LogLabel)=>{
return label.display
});
let consoleLabels:string[] = [];
let consoleStyles:string[] = [];
// 放置标签
for(let i = 0; i < labels.length; i++) {
for(let i = 0; i < labelsNeedRender.length; i++) {
consoleLabels.push(labels[i].getLoggerOutput());
if (i !== ( labels.length - 1))
if (i !== ( labelsNeedRender.length - 1))
consoleLabels.push("%c ");
consoleStyles.push(labels[i].getStyleOutput());
consoleStyles.push(labelsNeedRender[i].getStyleOutput());
if (i !== ( labels.length - 1))
if (i !== ( labelsNeedRender.length - 1))
consoleStyles.push("");
}
return [consoleLabels, consoleStyles];
}
/**
*
* Log
* @param content
* @param label 使
* @param attachLabel
*/
public static logBase<T extends Array<any>>
(content:MultipleLogContent<T>, labels:LogLabel[], attachLabel:LogLabel[] = []):T {
// TODO: 这里可以添加一些钩子作为中间件处理日志输出
// 测试是否输出内容
if(!Logger.testLog(...labels, ...attachLabel, InternalLogLabel.filterUrlLabel))
return content.getContent();
// 计算收集样式
let [consoleLabels, consoleStyles]= Logger.calcStyle(...labels, ...attachLabel);
// 调试输出
console.log(consoleLabels.join(""), ...consoleStyles, ...content.getContent());
return content.getContent();
}
/**
*
* @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;
public static log<T>(content:T, ...labels:LogLabel[]):T {
return Logger.logBase<Array<T>>(
new MultipleLogContent<Array<T>>(content), labels,
[InternalLogLabel.fileNameLabel]
)[0];
}
/**
* Logger.log
*/
public static l:typeof Logger.log = Logger.log;
/**
*
* @param labels
* @param content 使
*/
static logM<T>(labels:LogLabel[], ...content:T[]):T[] {
return Logger.log<T[]>(content, ...labels);
public static logMultiple<T extends Array<any>>(labels:LogLabel[], ...content:T):T {
return Logger.logBase<T>(
new MultipleLogContent<T>(...content), labels,
[InternalLogLabel.fileNameLabel]
);
}
/**
* Logger.logMultiple
*/
public static m:typeof Logger.logMultiple = Logger.logMultiple;
/**
*
* @param content
* @param label 使
*/
public static logLine<T>(content:T, ...labels:LogLabel[]):T {
return Logger.logBase<Array<T>>(
new MultipleLogContent<Array<T>>(content), labels,
[InternalLogLabel.urlLabel, InternalLogLabel.blankLabel]
)[0];
}
/**
* Logger.logMultiple
*/
public static ll:typeof Logger.logLine = Logger.logLine;
/**
*
* @param labels
* @param content 使
*/
public static logLineMultiple<T extends Array<any>>(labels:LogLabel[], ...content:T):T {
return Logger.logBase<T>(
new MultipleLogContent<T>(...content), labels,
[InternalLogLabel.urlLabel, InternalLogLabel.blankLabel]
);
}
/**
* Logger.logLineMultiple
*/
public static lm:typeof Logger.logLineMultiple = Logger.logLineMultiple;
}
export default Logger;
export {Logger, LogStyle, LogLabel}
export {Logger, LogStyle, LogLabel, InternalLogLabel, StackInfo}

View File

@ -5,7 +5,7 @@
},
"miniprogramRoot": "miniprogram/",
"compileType": "miniprogram",
"libVersion": "2.21.0",
"libVersion": "2.16.1",
"projectname": "mini-dlpu-v3",
"setting": {
"urlCheck": false,
@ -51,5 +51,8 @@
"simulatorType": "wechat",
"simulatorPluginLibVersion": {},
"appid": "wx7d809f5e8955843d",
"scripts": {
"beforeCompile": ""
},
"condition": {}
}

View File

@ -19,7 +19,11 @@
"lib": ["ES2020"],
"typeRoots": [
"./typings"
]
],
"baseUrl": "./miniprogram/",
"paths": {
"@logger": ["logger/"]
}
},
"include": [
"./**/*.ts"