Skip to content

Commit

Permalink
add timing、group、trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Shirtiny committed Apr 11, 2023
1 parent 4c0494c commit f0d9ee6
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ esbuild
host: "localhost",
},
{
entryPoints: [path.resolve(__dirname, "../src/main.ts")],
entryPoints: [path.resolve(__dirname, "../src/main.dev.ts")],
outfile: path.resolve(__dirname, "../dist/main.dev.js"),
platform: "browser",
globalName: "bvplayer_core",
Expand Down
52 changes: 52 additions & 0 deletions src/main.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* @Author: Shirtiny
* @Date: 2021-06-11 10:01:14
* @LastEditTime: 2021-12-11 11:41:23
* @Description:
*/
import { LEVELS, ShLogger } from "./model/shLogger";
import { Logger, LoggerOption, LoggerOptionParam } from "./model/logger";
import { css } from "./style/index";
import { Theme } from "./style/theme";
const theme = new Theme();

const logger = new ShLogger();

const request = () => {
return new Promise((r) => {
setTimeout(() => {
r("ok");
}, 300);
});
};

const run = () => {
logger.group("logger", () => {
logger.version("app-name", "1.1.2", { level: LEVELS.version });

logger.log(
"log",
{ level: LEVELS.log },
", logger default options: ",
logger.getLoggerOption(),
);

logger.debug("debug message", { level: LEVELS.debug });
logger.http("req and rep", { level: LEVELS.http });
logger.api("api message", { level: LEVELS.api });
logger.service("service data", { level: LEVELS.service });
logger.interval("interval task", { level: LEVELS.interval });
logger.key("ENTER", { level: LEVELS.key });
logger.warn("warnning message", { level: LEVELS.warn });
logger.error(new Error("error message"), { level: LEVELS.error });
});
};

logger.timing("run logger test", async (step) => {
step("run step start");
run();
const res = await request();
step("run step",["any data"], { res });
logger.trace("test for trace", { a: "any data" });
step("run step end");
});
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* @Description:
*/
import { LEVELS, ShLogger } from "./model/shLogger";
import { Logger } from "./model/logger";
import { Logger, LoggerOption, LoggerOptionParam } from "./model/logger";
import { css } from "./style/index";
import { Theme } from "./style/theme";

const theme = new Theme();

const sh_log = new ShLogger();

export { theme, ShLogger, css, Logger, LEVELS };
export { theme, ShLogger, css, Logger, LEVELS, LoggerOption, LoggerOptionParam };

export default sh_log;
77 changes: 73 additions & 4 deletions src/model/baseLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,64 @@ export interface ILog {
(...data: any[]): void;
}

export interface ILogGroup {
(...label: any[]): void;
}

export interface ILogGroupEnd {
(): void;
}

export interface ILogTime {
(label?: string): void;
}

export interface ILogTimeStep {
(label?: string, ...data: any[]): void;
}

export interface ILogTimeEnd {
(label?: string): void;
}

export interface ILogTrace {
(...data: any[]): void;
}

export interface IBaseLoggerOption {
enable?: boolean;
log?: ILog;
logGroup?: ILogGroup;
logGroupEnd?: ILogGroupEnd;
logTime?: ILogTime;
logTimeStep?: ILogTimeStep;
logTimeEnd?: ILogTimeEnd;
logTrace?: ILogTrace;
}

export interface ILogger {
log: ILog;
logGroup: ILogGroup;
logGroupEnd: ILogGroupEnd;
logTime: ILogTime;
logTimeStep: ILogTimeStep;
logTimeEnd: ILogTimeEnd;
logTrace: ILogTrace;
}

export class BaseLogger implements ILogger {
private _option: IBaseLoggerOption;

constructor(option?: IBaseLoggerOption) {
this._option = {
enable:
typeof option?.enable === "boolean"
? option.enable
: true,
enable: typeof option?.enable === "boolean" ? option.enable : true,
log: option?.log,
logGroup: option?.logGroup,
logGroupEnd: option?.logGroupEnd,
logTime: option?.logTime,
logTimeStep: option?.logTimeStep,
logTimeEnd: option?.logTimeEnd,
logTrace: option?.logTrace,
};
}

Expand All @@ -36,6 +75,36 @@ export class BaseLogger implements ILogger {
enable && log && log(...data);
}

logGroup(...label: any[]): void {
const { enable, logGroup } = this._option;
enable && logGroup && logGroup(...label);
}

logGroupEnd() {
const { enable, logGroupEnd } = this._option;
enable && logGroupEnd && logGroupEnd();
}

logTime(label?: string): void {
const { enable, logTime } = this._option;
enable && logTime && logTime(label);
}

logTimeStep(label?: string, ...data: any[]): void {
const { enable, logTimeStep } = this._option;
enable && logTimeStep && logTimeStep(label, ...data);
}

logTimeEnd(label?: string) {
const { enable, logTimeEnd } = this._option;
enable && logTimeEnd && logTimeEnd(label);
}

logTrace(...data: any[]) {
const { enable, logTrace } = this._option;
enable && logTrace && logTrace(...data);
}

set baseOption(option: IBaseLoggerOption) {
this._option = option;
}
Expand Down
125 changes: 113 additions & 12 deletions src/model/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Shape } from "./../style/shape";
import { BaseLogger, IBaseLoggerOption } from "./baseLogger";
import {
BaseLogger,
IBaseLoggerOption,
ILogGroup,
ILogGroupEnd,
ILogTime,
ILogTimeStep,
ILogTimeEnd,
ILogTrace,
} from "./baseLogger";
import { Theme } from "./../style/theme";
import { ILog } from "./baseLogger";
/*
Expand All @@ -9,38 +18,86 @@ import { ILog } from "./baseLogger";
* @Description:
*/

type LoggerOptionParam = {
export type TitlePairs = { str: string; style?: string }[];

export type LoggerOptionParam = {
enable?: boolean;

// TODO: console adapter console方法由adapter实现
log?: ILog;
logGroup?: ILogGroup;
logGroupCollapsed?: ILogGroup;
logGroupEnd?: ILogGroupEnd;
logTime?: ILogTime;
logTimeStep?: ILogTimeStep;
logTimeEnd?: ILogTimeEnd;
logTrace?: ILogTrace;

level?: number;
shape?: Shape;
isCollapsed?: boolean;
};

class LoggerOption implements IBaseLoggerOption {
export class LoggerOption implements IBaseLoggerOption {
enable?: boolean;
log?: ILog;
logGroup?: ILogGroup;
logGroupCollapsed?: ILogGroup;
logGroupEnd?: ILogGroupEnd;
logTime?: ILogTime;
logTimeStep?: ILogTimeStep;
logTimeEnd?: ILogTimeEnd;
logTrace?: ILogTrace;
level?: number;
shape?: Shape;
isCollapsed?: boolean;

constructor(param?: LoggerOptionParam) {
const defaultOptionParam = {
enable: true,
log: (...args: any) => window?.console?.log(...args),
log: (...args: any[]) => window?.console?.log(...args),
logGroup: (...label: any[]) => window?.console?.group(...label),
logGroupCollapsed: (...label: any[]) =>
window?.console?.groupCollapsed(...label),
logGroupEnd: () => window?.console?.groupEnd(),
logTime: (label?: string) => window?.console?.time(label),
logTimeStep: (label?: string, ...data: any[]) =>
window?.console?.timeLog(label, ...data),
logTimeEnd: (label?: string) => window?.console?.timeEnd(label),
logTrace: (...data: any[]) => window?.console?.trace(...data),
// miku!
level: 39,
shape: new Theme().shapes.slider,
isCollapsed: true,
};

this.enable =
param && typeof param.enable === "boolean"
? param.enable
: defaultOptionParam.enable;
this.log = (param && param.log) || defaultOptionParam.log;
this.logGroup = (param && param.logGroup) || defaultOptionParam.logGroup;
this.logGroupCollapsed =
(param && param.logGroupCollapsed) ||
defaultOptionParam.logGroupCollapsed;
this.logGroupEnd =
(param && param.logGroupEnd) || defaultOptionParam.logGroupEnd;
this.logTime = (param && param.logTime) || defaultOptionParam.logTime;
this.logTimeStep =
(param && param.logTimeStep) || defaultOptionParam.logTimeStep;
this.logTimeEnd =
(param && param.logTimeEnd) || defaultOptionParam.logTimeEnd;
this.logTrace = (param && param.logTrace) || defaultOptionParam.logTrace;

this.level =
param && typeof param.level === "number"
? param.level
: defaultOptionParam.level;
this.shape = param && param.shape ? param.shape : defaultOptionParam.shape;
this.isCollapsed =
param && param.isCollapsed
? param.isCollapsed
: defaultOptionParam.isCollapsed;
}
}

Expand All @@ -56,7 +113,17 @@ export class Logger extends BaseLogger {
setLoggerOption(option: object) {
const newOption: LoggerOption = { ...this.loggerOption, ...option };
this.loggerOption = newOption;
super.baseOption = { enable: newOption.enable, log: newOption.log };
super.baseOption = {
enable: newOption.enable,
log: newOption.log,
logGroup: newOption.isCollapsed
? newOption.logGroupCollapsed
: newOption.logGroup,
logGroupEnd: newOption.logGroupEnd,
logTime: newOption.logTime,
logTimeEnd: newOption.logTimeEnd,
logTrace: newOption.logTrace,
};
}

getLoggerOption() {
Expand All @@ -74,15 +141,11 @@ export class Logger extends BaseLogger {
return true;
}

protected customFormat(
level: number,
pairs: ({ str: string; style?: string })[],
...data: any[]
) {
protected customFormat(level: number, pairs: TitlePairs, ...data: any[]) {
if (!this.isLevelAllowed(level)) return;
const content = "%c" + pairs.map((p) => p.str).join("%c");
const descStyles = pairs.map((p) => p.style);
super.log(content, ...descStyles, ...data);
this.log(content, ...descStyles, ...data);
}

protected formatLog(
Expand All @@ -94,7 +157,45 @@ export class Logger extends BaseLogger {
...data: any[]
) {
if (!this.isLevelAllowed(level)) return;
super.log(`%c${title}%c${message}`, titleCss, messageCss, ...data);
this.log(`%c${title}%c${message}`, titleCss, messageCss, ...data);
}

protected formatGroup(
level: number,
label: string | TitlePairs,
logs: () => void,
) {
if (!this.isLevelAllowed(level)) return;
if (Array.isArray(label)) {
const title = "%c" + label.map((p) => p.str).join("%c");
const descStyles = label.map((p) => p.style);
this.logGroup(title, ...descStyles);
} else {
this.logGroup(String(label));
}

logs();
this.logGroupEnd();
}

protected async formatTiming(
level: number,
label: string,
logs: (step: (...data: any[]) => void) => any,
stepSplit: string = "<---",
) {
if (!this.isLevelAllowed(level)) return;
this.logTime(label);

await logs((...data: any[]) => {
this.logTimeStep(label, stepSplit, ...data);
});
this.logTimeEnd(label);
}

protected formatTrace(level: number, ...data: any[]) {
if (!this.isLevelAllowed(level)) return;
this.logTrace(...data);
}
}

Expand Down
Loading

0 comments on commit f0d9ee6

Please sign in to comment.