@anjianshi/utils 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/env-node/logging.d.ts +1 -1
- package/env-node/logging.js +4 -3
- package/lang/string.d.ts +5 -0
- package/lang/string.js +9 -0
- package/package.json +1 -1
- package/src/env-node/logging.ts +4 -3
- package/src/lang/string.ts +12 -0
package/env-node/logging.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export declare class FileHandler extends LogHandler {
|
|
|
47
47
|
readonly options: FileHandlerOptions;
|
|
48
48
|
constructor(options?: Partial<FileHandlerOptions>);
|
|
49
49
|
log(info: LogInfo): void;
|
|
50
|
-
protected
|
|
50
|
+
protected formatDataItem(item: unknown): string;
|
|
51
51
|
private buffer;
|
|
52
52
|
private bufferSize;
|
|
53
53
|
protected pushBuffer(...strings: string[]): void;
|
package/env-node/logging.js
CHANGED
|
@@ -7,6 +7,7 @@ import path from 'node:path';
|
|
|
7
7
|
import { fileURLToPath } from 'node:url';
|
|
8
8
|
import chalk from 'chalk';
|
|
9
9
|
import dayjs from 'dayjs';
|
|
10
|
+
import { removeANSIColor } from '../lang/index.js';
|
|
10
11
|
import { logger as defaultLogger, LogLevel, LogHandler, formatters, } from '../logging/index.js';
|
|
11
12
|
export * from '../logging/index.js';
|
|
12
13
|
/**
|
|
@@ -87,7 +88,7 @@ export class FileHandler extends LogHandler {
|
|
|
87
88
|
const itemStrings = [];
|
|
88
89
|
let totalLength = prefix.length;
|
|
89
90
|
for (const item of args) {
|
|
90
|
-
const itemString = this.
|
|
91
|
+
const itemString = this.formatDataItem(item);
|
|
91
92
|
// 截断过长的日志内容 Truncate overly long log messages
|
|
92
93
|
if (totalLength + itemString.length < this.options.maxLength) {
|
|
93
94
|
itemStrings.push((totalLength === prefix.length ? '' : ' ') + itemString);
|
|
@@ -100,9 +101,9 @@ export class FileHandler extends LogHandler {
|
|
|
100
101
|
}
|
|
101
102
|
this.pushBuffer(prefix, ...itemStrings, '\n');
|
|
102
103
|
}
|
|
103
|
-
|
|
104
|
+
formatDataItem(item) {
|
|
104
105
|
if (typeof item === 'string')
|
|
105
|
-
return item;
|
|
106
|
+
return removeANSIColor(item);
|
|
106
107
|
if (item instanceof Error)
|
|
107
108
|
return item.stack ?? String(item);
|
|
108
109
|
try {
|
package/lang/string.d.ts
CHANGED
|
@@ -23,3 +23,8 @@ export declare function safeParseFloat(value: string | number, fallback: number)
|
|
|
23
23
|
* dp: how many decimal places to keep(保留几位小数)
|
|
24
24
|
*/
|
|
25
25
|
export declare function readableSize(bytes: number, si?: boolean, dp?: number): string;
|
|
26
|
+
/**
|
|
27
|
+
* 移除字符串中的 ASNI Color 修饰符
|
|
28
|
+
* https://stackoverflow.com/a/7150870/2815178
|
|
29
|
+
*/
|
|
30
|
+
export declare function removeANSIColor(string: string): string;
|
package/lang/string.js
CHANGED
|
@@ -79,3 +79,12 @@ export function readableSize(bytes, si = false, dp = 1) {
|
|
|
79
79
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
|
80
80
|
return `${bytes.toFixed(dp)} ${units[u]}`;
|
|
81
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* 移除字符串中的 ASNI Color 修饰符
|
|
84
|
+
* https://stackoverflow.com/a/7150870/2815178
|
|
85
|
+
*/
|
|
86
|
+
export function removeANSIColor(string) {
|
|
87
|
+
return string.replace(
|
|
88
|
+
// eslint-disable-next-line no-control-regex
|
|
89
|
+
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
|
|
90
|
+
}
|
package/package.json
CHANGED
package/src/env-node/logging.ts
CHANGED
|
@@ -7,6 +7,7 @@ import path from 'node:path'
|
|
|
7
7
|
import { fileURLToPath } from 'node:url'
|
|
8
8
|
import chalk from 'chalk'
|
|
9
9
|
import dayjs from 'dayjs'
|
|
10
|
+
import { removeANSIColor } from '../lang/index.js'
|
|
10
11
|
import {
|
|
11
12
|
logger as defaultLogger,
|
|
12
13
|
type Logger,
|
|
@@ -120,7 +121,7 @@ export class FileHandler extends LogHandler {
|
|
|
120
121
|
const itemStrings: string[] = []
|
|
121
122
|
let totalLength = prefix.length
|
|
122
123
|
for (const item of args) {
|
|
123
|
-
const itemString = this.
|
|
124
|
+
const itemString = this.formatDataItem(item)
|
|
124
125
|
|
|
125
126
|
// 截断过长的日志内容 Truncate overly long log messages
|
|
126
127
|
if (totalLength + itemString.length < this.options.maxLength) {
|
|
@@ -137,8 +138,8 @@ export class FileHandler extends LogHandler {
|
|
|
137
138
|
this.pushBuffer(prefix, ...itemStrings, '\n')
|
|
138
139
|
}
|
|
139
140
|
|
|
140
|
-
protected
|
|
141
|
-
if (typeof item === 'string') return item
|
|
141
|
+
protected formatDataItem(item: unknown) {
|
|
142
|
+
if (typeof item === 'string') return removeANSIColor(item)
|
|
142
143
|
if (item instanceof Error) return item.stack ?? String(item)
|
|
143
144
|
try {
|
|
144
145
|
const json = JSON.stringify(item) as string | undefined
|
package/src/lang/string.ts
CHANGED
|
@@ -82,3 +82,15 @@ export function readableSize(bytes: number, si = false, dp = 1) {
|
|
|
82
82
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1)
|
|
83
83
|
return `${bytes.toFixed(dp)} ${units[u]!}`
|
|
84
84
|
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 移除字符串中的 ASNI Color 修饰符
|
|
88
|
+
* https://stackoverflow.com/a/7150870/2815178
|
|
89
|
+
*/
|
|
90
|
+
export function removeANSIColor(string: string) {
|
|
91
|
+
return string.replace(
|
|
92
|
+
// eslint-disable-next-line no-control-regex
|
|
93
|
+
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
|
|
94
|
+
'',
|
|
95
|
+
)
|
|
96
|
+
}
|