Skip to content

Commit

Permalink
🐞 fix(logger): 修复过滤日志后类型错误的问题
Browse files Browse the repository at this point in the history
现在使用过滤函数 filter 后不会导致类型变成 FrameCollection 而是保持原来的类型
  • Loading branch information
batu1579 committed Nov 22, 2022
1 parent 2533bd5 commit 2658be2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
24 changes: 17 additions & 7 deletions src/lib/docs/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

> 注意: 一般来说不需要手动清空。
### FrameCollection.filter(callbackFn: (frame: FrameType, index: number, array: FrameType[]) => boolean): FrameCollection\<FrameType\>
## class TraceCollection

调用堆栈集合,继承自 [FrameCollection](#class-framecollectionframetype)\<[TraceStackFrame](#class-tracestackframe)\> 。在外部使用该类型的参数时需要使用 `TraceCollectionType`

> 注意: `TraceCollection` 类并未导出,只能通过 `getStackTrace()` 方法来获取当前的调用堆栈集合。
### TraceCollection.filter(callbackFn: (frame: FrameType, index: number, array: FrameType[]) => boolean): TraceCollection

- `callbackFn` 用来测试数组中每个元素的函数。返回 `true` 表示该元素通过测试,保留该元素, `false` 则不保留。它接受以下三个参数:

Expand All @@ -24,12 +30,6 @@

从当前的日志集合当中过滤符合条件的栈帧,并返回他们组成的新栈帧集合。

## class TraceCollection

调用堆栈集合,继承自 [FrameCollection](#class-framecollectionframetype)\<[TraceStackFrame](#class-tracestackframe)\> 。在外部使用该类型的参数时需要使用 `TraceCollectionType`

> 注意: `TraceCollection` 类并未导出,只能通过 `getStackTrace()` 方法来获取当前的调用堆栈集合。
### TraceCollection.toString(format?: [TraceFormatter](#traceformatter)): String

- `format` 用于规定转换后的字符串格式的回调方法,默认转换格式的默认转换格式类似 Python 。
Expand All @@ -42,6 +42,16 @@

> 注意: `LogCollection` 类并未导出,只能通过 [logStack](#const-logstack) 对象来获取他的子集。
### LogCollection.filter(callbackFn: (frame: FrameType, index: number, array: FrameType[]) => boolean): LogCollection

- `callbackFn` 用来测试数组中每个元素的函数。返回 `true` 表示该元素通过测试,保留该元素, `false` 则不保留。它接受以下三个参数:

- `element` 数组中当前正在处理的元素。
- `index` 正在处理的元素在数组中的索引。
- `array` 调用了 `filter()` 的数组本身。

从当前的日志集合当中过滤符合条件的栈帧,并返回他们组成的新栈帧集合。

### LogCollection.toHtmlString(): string

将全部日志堆栈中的数据转换为 HTML 文档。用于美化远程发送的样式。
Expand Down
35 changes: 28 additions & 7 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: BATU1579
* @CreateDate: 2022-02-05 04:00:16
* @LastEditor: BATU1579
* @LastTime: 2022-09-25 22:54:28
* @LastTime: 2022-11-22 16:27:08
* @FilePath: \\src\\lib\\logger.ts
* @Description: 存放关于日志和调试信息的预制方法。
*/
Expand Down Expand Up @@ -33,7 +33,9 @@ class FrameCollection<FrameType> {
public push(frame: FrameType): void {
this.frames.push(frame);
}
}

class TraceCollection extends FrameCollection<TraceStackFrame> {
/**
* @description: 从当前的集合当中过滤符合条件的栈帧。
* @param {Function} callbackFn 用来测试数组中每个元素的函数。返回 `true` 表示该元素通过测试,保留该元素, `false` 则不保留。它接受以下三个参数:
Expand All @@ -42,9 +44,9 @@ class FrameCollection<FrameType> {
* - `array` 调用了 `filter()` 的数组本身。
* @return {LogCollection} 过滤出的符合条件的栈帧组成的新栈帧集合。
*/
public filter(callbackFn: (frame: FrameType, index: number, array: FrameType[]) => boolean): FrameCollection<FrameType> {
let result = new FrameCollection<FrameType>();
let tempFrame: FrameType;
public filter(callbackFn: (frame: TraceStackFrame, index: number, array: TraceStackFrame[]) => boolean): TraceCollection {
let result = new TraceCollection();
let tempFrame: TraceStackFrame;

for (let i = 0; i < this.frames.length; i++) {
tempFrame = this.frames[i];
Expand All @@ -55,9 +57,7 @@ class FrameCollection<FrameType> {

return result;
}
}

class TraceCollection extends FrameCollection<TraceStackFrame> {
/**
* @description: 将调用堆栈集合转换为字符串。
* @param {TraceFormatter} [format] 用于规定转换后的字符串格式的回调方法,默认转换格式的默认转换格式类似 Python 。
Expand All @@ -75,6 +75,27 @@ class TraceCollection extends FrameCollection<TraceStackFrame> {
}

class LogCollection extends FrameCollection<LogStackFrame> {
/**
* @description: 从当前的集合当中过滤符合条件的栈帧。
* @param {Function} callbackFn 用来测试数组中每个元素的函数。返回 `true` 表示该元素通过测试,保留该元素, `false` 则不保留。它接受以下三个参数:
* - `element` 数组中当前正在处理的元素。
* - `index` 正在处理的元素在数组中的索引。
* - `array` 调用了 `filter()` 的数组本身。
* @return {LogCollection} 过滤出的符合条件的栈帧组成的新栈帧集合。
*/
public filter(callbackFn: (frame: LogStackFrame, index: number, array: LogStackFrame[]) => boolean): LogCollection {
let result = new LogCollection();
let tempFrame: LogStackFrame;

for (let i = 0; i < this.frames.length; i++) {
tempFrame = this.frames[i];
if (callbackFn(tempFrame, i, this.frames)) {
result.push(tempFrame);
}
}

return result;
}

/**
* @description: 将日志堆栈转换为 html 字符串用于发送日志。
Expand Down Expand Up @@ -357,7 +378,7 @@ export class Record {
*/
private static DISPLAY_LEVEL: number = LogLevel.Debug;

private constructor() {}
private constructor() { }

/**
* @description: 设置记录的日志级别,低于设置的级别的日志都不会记录。
Expand Down

0 comments on commit 2658be2

Please sign in to comment.