@weapp-core/logger 3.0.3 → 3.1.1
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/README.md +4 -1
- package/dist/index.d.ts +23 -15
- package/dist/index.js +56 -67
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
- 全局日志级别控制
|
|
10
10
|
- 按 tag 覆盖日志级别
|
|
11
11
|
- API 兼容 consola 的常用用法
|
|
12
|
+
- 内置 `picocolors`,统一终端文本染色
|
|
12
13
|
|
|
13
14
|
## 安装
|
|
14
15
|
|
|
@@ -19,7 +20,7 @@ pnpm add @weapp-core/logger
|
|
|
19
20
|
## 使用
|
|
20
21
|
|
|
21
22
|
```ts
|
|
22
|
-
import logger, { configureLogger } from '@weapp-core/logger'
|
|
23
|
+
import logger, { colors, configureLogger } from '@weapp-core/logger'
|
|
23
24
|
|
|
24
25
|
configureLogger({
|
|
25
26
|
level: 'info',
|
|
@@ -30,6 +31,8 @@ configureLogger({
|
|
|
30
31
|
|
|
31
32
|
logger.info('Hello')
|
|
32
33
|
logger.withTag('build').warn('Build warning')
|
|
34
|
+
|
|
35
|
+
logger.success(colors.green('Build done'))
|
|
33
36
|
```
|
|
34
37
|
|
|
35
38
|
## 配置
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { ConsolaInstance } from
|
|
1
|
+
import { ConsolaInstance } from "consola";
|
|
2
|
+
import * as picocolors_types0 from "picocolors/types";
|
|
2
3
|
|
|
4
|
+
//#region src/index.d.ts
|
|
3
5
|
/**
|
|
4
6
|
* @description 日志类型
|
|
5
7
|
*/
|
|
@@ -8,24 +10,30 @@ type LogType = 'error' | 'warn' | 'info';
|
|
|
8
10
|
* @description 日志级别
|
|
9
11
|
*/
|
|
10
12
|
type LogLevel = LogType | 'silent';
|
|
13
|
+
/**
|
|
14
|
+
* @description 终端文本染色工具,基于 picocolors。
|
|
15
|
+
*/
|
|
16
|
+
declare const colors: picocolors_types0.Colors & {
|
|
17
|
+
createColors: (enabled?: boolean) => picocolors_types0.Colors;
|
|
18
|
+
};
|
|
11
19
|
interface LoggerConfig {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
/**
|
|
21
|
+
* @description 全局日志级别
|
|
22
|
+
* @example
|
|
23
|
+
* level: 'info'
|
|
24
|
+
*/
|
|
25
|
+
level?: LogLevel;
|
|
26
|
+
/**
|
|
27
|
+
* @description 按 tag 覆盖日志级别
|
|
28
|
+
* @example
|
|
29
|
+
* tags: { build: 'warn', npm: 'silent' }
|
|
30
|
+
*/
|
|
31
|
+
tags?: Record<string, LogLevel | undefined>;
|
|
24
32
|
}
|
|
25
33
|
/**
|
|
26
34
|
* @description 设置全局 logger 配置
|
|
27
35
|
*/
|
|
28
36
|
declare function configureLogger(config?: LoggerConfig): void;
|
|
29
37
|
declare const logger: ConsolaInstance;
|
|
30
|
-
|
|
31
|
-
export {
|
|
38
|
+
//#endregion
|
|
39
|
+
export { LogLevel, LogType, LoggerConfig, colors, configureLogger, logger as default };
|
package/dist/index.js
CHANGED
|
@@ -1,79 +1,68 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
1
|
import { createConsola } from "consola";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
import picocolors from "picocolors";
|
|
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
|
|
9
14
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
|
23
28
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
const defaultLoggerConfig = {
|
|
30
|
+
level: "info",
|
|
31
|
+
tags: {}
|
|
27
32
|
};
|
|
28
|
-
|
|
33
|
+
let loggerConfig = { ...defaultLoggerConfig };
|
|
34
|
+
/**
|
|
35
|
+
* @description 设置全局 logger 配置
|
|
36
|
+
*/
|
|
29
37
|
function configureLogger(config = {}) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
loggerConfig = {
|
|
39
|
+
level: config.level ?? defaultLoggerConfig.level,
|
|
40
|
+
tags: config.tags ?? defaultLoggerConfig.tags
|
|
41
|
+
};
|
|
34
42
|
}
|
|
35
43
|
function resolveLogLevel(tag) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
return loggerConfig.level;
|
|
44
|
+
if (tag && loggerConfig.tags[tag]) return loggerConfig.tags[tag];
|
|
45
|
+
return loggerConfig.level;
|
|
40
46
|
}
|
|
41
47
|
function shouldLog(tag, type) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const requiredLevel = typeLevel[type] ?? levelRank.info;
|
|
47
|
-
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;
|
|
48
52
|
}
|
|
49
53
|
function createLogger(tag) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return (...args) => {
|
|
62
|
-
if (shouldLog(tag, prop)) {
|
|
63
|
-
return value.apply(targetLogger, args);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
if (typeof value === "function") {
|
|
68
|
-
return value.bind(targetLogger);
|
|
69
|
-
}
|
|
70
|
-
return value;
|
|
71
|
-
}
|
|
72
|
-
});
|
|
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
|
+
} });
|
|
73
65
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
export {
|
|
77
|
-
configureLogger,
|
|
78
|
-
index_default as default
|
|
79
|
-
};
|
|
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.
|
|
4
|
+
"version": "3.1.1",
|
|
5
5
|
"description": "@weapp-core/logger",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -39,13 +39,15 @@
|
|
|
39
39
|
"node": "^20.19.0 || >=22.12.0"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"consola": "^3.4.2"
|
|
42
|
+
"consola": "^3.4.2",
|
|
43
|
+
"picocolors": "^1.1.1"
|
|
43
44
|
},
|
|
44
45
|
"scripts": {
|
|
45
|
-
"dev": "
|
|
46
|
-
"build": "
|
|
46
|
+
"dev": "tsdown -w --sourcemap",
|
|
47
|
+
"build": "tsdown",
|
|
47
48
|
"test": "vitest run",
|
|
48
49
|
"test:dev": "vitest",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
49
51
|
"release": "pnpm publish",
|
|
50
52
|
"lint": "eslint .",
|
|
51
53
|
"lint:fix": "eslint . --fix",
|