@weapp-core/logger 3.1.0 → 3.1.2
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/dist/index.d.ts +22 -24
- package/dist/index.js +55 -69
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,38 +1,36 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { ConsolaInstance } from "consola";
|
|
2
|
+
import picocolors from "picocolors";
|
|
3
|
+
//#region src/index.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* @description 日志类型
|
|
6
6
|
*/
|
|
7
|
-
type LogType = 'error' | 'warn' | 'info';
|
|
7
|
+
export type LogType = 'error' | 'warn' | 'info';
|
|
8
8
|
/**
|
|
9
9
|
* @description 日志级别
|
|
10
10
|
*/
|
|
11
|
-
type LogLevel = LogType | 'silent';
|
|
11
|
+
export type LogLevel = LogType | 'silent';
|
|
12
12
|
/**
|
|
13
13
|
* @description 终端文本染色工具,基于 picocolors。
|
|
14
14
|
*/
|
|
15
|
-
declare const colors:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*/
|
|
30
|
-
tags?: Record<string, LogLevel | undefined>;
|
|
15
|
+
export declare const colors: typeof picocolors;
|
|
16
|
+
export interface LoggerConfig {
|
|
17
|
+
/**
|
|
18
|
+
* @description 全局日志级别
|
|
19
|
+
* @example
|
|
20
|
+
* level: 'info'
|
|
21
|
+
*/
|
|
22
|
+
level?: LogLevel;
|
|
23
|
+
/**
|
|
24
|
+
* @description 按 tag 覆盖日志级别
|
|
25
|
+
* @example
|
|
26
|
+
* tags: { build: 'warn', npm: 'silent' }
|
|
27
|
+
*/
|
|
28
|
+
tags?: Record<string, LogLevel | undefined>;
|
|
31
29
|
}
|
|
32
30
|
/**
|
|
33
31
|
* @description 设置全局 logger 配置
|
|
34
32
|
*/
|
|
35
|
-
declare function configureLogger(config?: LoggerConfig): void;
|
|
33
|
+
export declare function configureLogger(config?: LoggerConfig): void;
|
|
36
34
|
declare const logger: ConsolaInstance;
|
|
37
|
-
|
|
38
|
-
export {
|
|
35
|
+
//#endregion
|
|
36
|
+
export { logger as default };
|
package/dist/index.js
CHANGED
|
@@ -1,82 +1,68 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
1
|
import { createConsola } from "consola";
|
|
3
2
|
import picocolors from "picocolors";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description 终端文本染色工具,基于 picocolors。
|
|
6
|
+
*/
|
|
7
|
+
const colors = picocolors;
|
|
8
|
+
const baseLogger = createConsola();
|
|
9
|
+
const levelRank = {
|
|
10
|
+
silent: -1,
|
|
11
|
+
error: 0,
|
|
12
|
+
warn: 1,
|
|
13
|
+
info: 2
|
|
11
14
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
const typeLevel = {
|
|
16
|
+
fatal: 0,
|
|
17
|
+
error: 0,
|
|
18
|
+
warn: 1,
|
|
19
|
+
log: 2,
|
|
20
|
+
info: 2,
|
|
21
|
+
success: 2,
|
|
22
|
+
fail: 2,
|
|
23
|
+
ready: 2,
|
|
24
|
+
start: 2,
|
|
25
|
+
box: 2,
|
|
26
|
+
debug: 2,
|
|
27
|
+
trace: 2
|
|
25
28
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
const defaultLoggerConfig = {
|
|
30
|
+
level: "info",
|
|
31
|
+
tags: {}
|
|
29
32
|
};
|
|
30
|
-
|
|
33
|
+
let loggerConfig = { ...defaultLoggerConfig };
|
|
34
|
+
/**
|
|
35
|
+
* @description 设置全局 logger 配置
|
|
36
|
+
*/
|
|
31
37
|
function configureLogger(config = {}) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
loggerConfig = {
|
|
39
|
+
level: config.level ?? defaultLoggerConfig.level,
|
|
40
|
+
tags: config.tags ?? defaultLoggerConfig.tags
|
|
41
|
+
};
|
|
36
42
|
}
|
|
37
43
|
function resolveLogLevel(tag) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
return loggerConfig.level;
|
|
44
|
+
if (tag && loggerConfig.tags[tag]) return loggerConfig.tags[tag];
|
|
45
|
+
return loggerConfig.level;
|
|
42
46
|
}
|
|
43
47
|
function shouldLog(tag, type) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const requiredLevel = typeLevel[type] ?? levelRank.info;
|
|
49
|
-
return levelRank[level] >= requiredLevel;
|
|
48
|
+
const level = resolveLogLevel(tag);
|
|
49
|
+
if (level === "silent") return false;
|
|
50
|
+
const requiredLevel = typeLevel[type] ?? levelRank.info;
|
|
51
|
+
return levelRank[level] >= requiredLevel;
|
|
50
52
|
}
|
|
51
53
|
function createLogger(tag) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return (...args) => {
|
|
64
|
-
if (shouldLog(tag, prop)) {
|
|
65
|
-
return value.apply(targetLogger, args);
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
if (typeof value === "function") {
|
|
70
|
-
return value.bind(targetLogger);
|
|
71
|
-
}
|
|
72
|
-
return value;
|
|
73
|
-
}
|
|
74
|
-
});
|
|
54
|
+
const target = tag ? baseLogger.withTag(tag) : baseLogger;
|
|
55
|
+
return new Proxy(target, { get(targetLogger, prop, receiver) {
|
|
56
|
+
if (prop === "withTag" || prop === "withScope") return (nextTag) => createLogger(nextTag);
|
|
57
|
+
if (typeof prop !== "string") return Reflect.get(targetLogger, prop, receiver);
|
|
58
|
+
const value = Reflect.get(targetLogger, prop, receiver);
|
|
59
|
+
if (typeof value === "function" && prop in typeLevel) return (...args) => {
|
|
60
|
+
if (shouldLog(tag, prop)) return value.apply(targetLogger, args);
|
|
61
|
+
};
|
|
62
|
+
if (typeof value === "function") return value.bind(targetLogger);
|
|
63
|
+
return value;
|
|
64
|
+
} });
|
|
75
65
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
export {
|
|
79
|
-
colors,
|
|
80
|
-
configureLogger,
|
|
81
|
-
index_default as default
|
|
82
|
-
};
|
|
66
|
+
const logger = createLogger();
|
|
67
|
+
//#endregion
|
|
68
|
+
export { colors, configureLogger, logger as default };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-core/logger",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.1.
|
|
4
|
+
"version": "3.1.2",
|
|
5
5
|
"description": "@weapp-core/logger",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -43,10 +43,11 @@
|
|
|
43
43
|
"picocolors": "^1.1.1"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
|
-
"dev": "
|
|
47
|
-
"build": "
|
|
46
|
+
"dev": "tsdown -w --sourcemap",
|
|
47
|
+
"build": "tsdown",
|
|
48
48
|
"test": "vitest run",
|
|
49
49
|
"test:dev": "vitest",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
50
51
|
"release": "pnpm publish",
|
|
51
52
|
"lint": "eslint .",
|
|
52
53
|
"lint:fix": "eslint . --fix",
|