befly 3.10.12 → 3.10.13
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/befly.config.ts +3 -1
- package/lib/dbHelper.ts +10 -9
- package/lib/logger.ts +14 -7
- package/package.json +2 -2
- package/types/logger.d.ts +15 -0
package/befly.config.ts
CHANGED
|
@@ -26,7 +26,9 @@ const defaultOptions: BeflyOptions = {
|
|
|
26
26
|
excludeFields: ["password", "token", "secret"],
|
|
27
27
|
dir: "./logs",
|
|
28
28
|
console: 1,
|
|
29
|
-
maxSize: 10485760 // 10MB
|
|
29
|
+
maxSize: 10485760, // 10MB
|
|
30
|
+
maxStringLen: 100,
|
|
31
|
+
maxArrayItems: 100
|
|
30
32
|
},
|
|
31
33
|
|
|
32
34
|
// ========== 数据库配置 ==========
|
package/lib/dbHelper.ts
CHANGED
|
@@ -185,14 +185,13 @@ export class DbHelper {
|
|
|
185
185
|
const duration = Date.now() - startTime;
|
|
186
186
|
|
|
187
187
|
if (this.debug === 1) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
Logger.info(
|
|
188
|
+
Logger.debug(
|
|
191
189
|
{
|
|
192
190
|
subsystem: "db",
|
|
193
|
-
event: "
|
|
191
|
+
event: "db_sql",
|
|
192
|
+
dbEvent: "query",
|
|
194
193
|
duration: duration,
|
|
195
|
-
sqlPreview:
|
|
194
|
+
sqlPreview: sqlStr,
|
|
196
195
|
paramsCount: (params || []).length,
|
|
197
196
|
params: params || []
|
|
198
197
|
},
|
|
@@ -205,7 +204,8 @@ export class DbHelper {
|
|
|
205
204
|
Logger.warn(
|
|
206
205
|
{
|
|
207
206
|
subsystem: "db",
|
|
208
|
-
event: "
|
|
207
|
+
event: "db_sql",
|
|
208
|
+
dbEvent: "slow",
|
|
209
209
|
duration: duration,
|
|
210
210
|
sqlPreview: sqlStr,
|
|
211
211
|
params: params || [],
|
|
@@ -218,12 +218,13 @@ export class DbHelper {
|
|
|
218
218
|
return result;
|
|
219
219
|
} catch (error: any) {
|
|
220
220
|
const duration = Date.now() - startTime;
|
|
221
|
-
|
|
222
|
-
const sqlPreview = sqlStr.length > 200 ? sqlStr.substring(0, 200) + "..." : sqlStr;
|
|
223
221
|
Logger.error(
|
|
224
222
|
{
|
|
223
|
+
subsystem: "db",
|
|
224
|
+
event: "db_sql",
|
|
225
|
+
dbEvent: "error",
|
|
225
226
|
err: error,
|
|
226
|
-
sqlPreview:
|
|
227
|
+
sqlPreview: sqlStr,
|
|
227
228
|
params: params || [],
|
|
228
229
|
duration: duration
|
|
229
230
|
},
|
package/lib/logger.ts
CHANGED
|
@@ -19,8 +19,11 @@ import { getCtx } from "./asyncContext.js";
|
|
|
19
19
|
// 为避免相对路径的 logs 目录随着 cwd 变化,使用模块加载时的初始 cwd 作为锚点。
|
|
20
20
|
const INITIAL_CWD = process.cwd();
|
|
21
21
|
|
|
22
|
-
const
|
|
23
|
-
const
|
|
22
|
+
const DEFAULT_LOG_STRING_LEN = 100;
|
|
23
|
+
const DEFAULT_LOG_ARRAY_ITEMS = 100;
|
|
24
|
+
|
|
25
|
+
let maxLogStringLen = DEFAULT_LOG_STRING_LEN;
|
|
26
|
+
let maxLogArrayItems = DEFAULT_LOG_ARRAY_ITEMS;
|
|
24
27
|
|
|
25
28
|
// 为避免递归导致栈溢出/性能抖动:使用非递归遍历,并对深度/节点数做硬限制。
|
|
26
29
|
// 说明:这不是业务数据结构的“真实深度”,而是日志清洗的最大深入层级(越大越重)。
|
|
@@ -145,6 +148,10 @@ export function configure(cfg: LoggerConfig): void {
|
|
|
145
148
|
sanitizeNodesLimit = normalizePositiveInt(config.sanitizeNodes, DEFAULT_LOG_SANITIZE_NODES, 50, 20000);
|
|
146
149
|
sanitizeObjectKeysLimit = normalizePositiveInt(config.sanitizeObjectKeys, DEFAULT_LOG_OBJECT_KEYS, 10, 5000);
|
|
147
150
|
|
|
151
|
+
// 运行时截断上限(可配置)
|
|
152
|
+
maxLogStringLen = normalizePositiveInt(config.maxStringLen, DEFAULT_LOG_STRING_LEN, 20, 200000);
|
|
153
|
+
maxLogArrayItems = normalizePositiveInt(config.maxArrayItems, DEFAULT_LOG_ARRAY_ITEMS, 10, 5000);
|
|
154
|
+
|
|
148
155
|
// 仅支持数组配置:excludeFields?: string[]
|
|
149
156
|
const userPatterns = Array.isArray(config.excludeFields) ? config.excludeFields : [];
|
|
150
157
|
const patterns = [...BUILTIN_SENSITIVE_KEYS, ...userPatterns]
|
|
@@ -325,9 +332,9 @@ function getErrorLogger(): pino.Logger {
|
|
|
325
332
|
}
|
|
326
333
|
|
|
327
334
|
function truncateString(val: string, stats: Record<string, number>): string {
|
|
328
|
-
if (val.length <=
|
|
335
|
+
if (val.length <= maxLogStringLen) return val;
|
|
329
336
|
stats.truncatedStrings = (stats.truncatedStrings || 0) + 1;
|
|
330
|
-
return val.slice(0,
|
|
337
|
+
return val.slice(0, maxLogStringLen);
|
|
331
338
|
}
|
|
332
339
|
|
|
333
340
|
function isSensitiveKey(key: string): boolean {
|
|
@@ -518,15 +525,15 @@ function sanitizeValueLimited(val: any, visited: WeakSet<object>, stats: Record<
|
|
|
518
525
|
if (Array.isArray(frame.src)) {
|
|
519
526
|
const arr = frame.src as any[];
|
|
520
527
|
const len = arr.length;
|
|
521
|
-
const limit = len >
|
|
528
|
+
const limit = len > maxLogArrayItems ? maxLogArrayItems : len;
|
|
522
529
|
|
|
523
530
|
for (let i = 0; i < limit; i++) {
|
|
524
531
|
tryAssign(frame.dst, i, arr[i], frame.depth);
|
|
525
532
|
}
|
|
526
533
|
|
|
527
|
-
if (len >
|
|
534
|
+
if (len > maxLogArrayItems) {
|
|
528
535
|
stats.arraysTruncated = (stats.arraysTruncated || 0) + 1;
|
|
529
|
-
stats.arrayItemsOmitted = (stats.arrayItemsOmitted || 0) + (len -
|
|
536
|
+
stats.arrayItemsOmitted = (stats.arrayItemsOmitted || 0) + (len - maxLogArrayItems);
|
|
530
537
|
}
|
|
531
538
|
|
|
532
539
|
continue;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.10.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "3.10.13",
|
|
4
|
+
"gitHead": "391e4e5cacb1d46cf37b5aa1179c9fe0a602337d",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
7
7
|
"keywords": [
|
package/types/logger.d.ts
CHANGED
|
@@ -28,6 +28,21 @@ export interface LoggerConfig {
|
|
|
28
28
|
/** 单个日志文件最大大小 (MB) @default 10 */
|
|
29
29
|
maxSize?: number;
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* 字符串最大长度(超过会被截断)
|
|
33
|
+
* - 影响所有结构化字段中的 string(包括 db.debug 打印的 sqlPreview)
|
|
34
|
+
* - 生产环境建议保持较小值;需要更完整 SQL 时可在开发环境调大
|
|
35
|
+
* @default 100
|
|
36
|
+
*/
|
|
37
|
+
maxStringLen?: number;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 数组最多保留的元素数量(超出部分丢弃)
|
|
41
|
+
* - 仅影响结构化日志清洗(数组裁剪)
|
|
42
|
+
* @default 100
|
|
43
|
+
*/
|
|
44
|
+
maxArrayItems?: number;
|
|
45
|
+
|
|
31
46
|
/**
|
|
32
47
|
* 结构化日志清洗:最大清洗深度(非递归遍历的 depth 上限)
|
|
33
48
|
* - 值越大,日志更“完整”,但 CPU/内存开销更高
|