@anjianshi/utils 1.2.0 → 1.2.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/logging/adapt.d.ts +2 -1
- package/logging/adapt.js +3 -2
- package/logging/index.d.ts +4 -2
- package/logging/index.js +21 -2
- package/package.json +1 -1
- package/src/logging/adapt.ts +3 -3
- package/src/logging/index.ts +23 -4
package/logging/adapt.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Debug } from 'debug';
|
|
2
|
+
import { type Logger } from './index.js';
|
|
2
3
|
/**
|
|
3
4
|
* 适配 debug package
|
|
4
5
|
*/
|
|
5
|
-
export declare function adaptDebugLib(debugLib: Debug, enable?: string): void;
|
|
6
|
+
export declare function adaptDebugLib(debugLib: Debug, enable?: string, logger?: Logger): void;
|
package/logging/adapt.js
CHANGED
|
@@ -2,7 +2,7 @@ import { getLogger } from './index.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* 适配 debug package
|
|
4
4
|
*/
|
|
5
|
-
export function adaptDebugLib(debugLib, enable = '') {
|
|
5
|
+
export function adaptDebugLib(debugLib, enable = '', logger) {
|
|
6
6
|
// 不在 localStorage 里记录 debugLib enable 状态,
|
|
7
7
|
// 以解决 web worker 里读不到 localStorage 而无法启用 debugLib 日志的问题
|
|
8
8
|
const emulate = {
|
|
@@ -30,7 +30,8 @@ export function adaptDebugLib(debugLib, enable = '') {
|
|
|
30
30
|
};
|
|
31
31
|
Object.assign(debugLib, emulate);
|
|
32
32
|
// 将 debugLib 日志转发给 logger
|
|
33
|
-
|
|
33
|
+
if (!logger)
|
|
34
|
+
logger = getLogger('3rd-library');
|
|
34
35
|
debugLib.log = logger.debug.bind(logger);
|
|
35
36
|
if (enable) {
|
|
36
37
|
debugLib.enable(enable);
|
package/logging/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare enum LogLevel {
|
|
|
7
7
|
Warning = 3,
|
|
8
8
|
Error = 4
|
|
9
9
|
}
|
|
10
|
+
export declare const logLevelMap: Record<string, LogLevel>;
|
|
10
11
|
export interface LogInfo {
|
|
11
12
|
logger: string;
|
|
12
13
|
level: LogLevel;
|
|
@@ -22,13 +23,14 @@ export declare class Logger {
|
|
|
22
23
|
level: LogLevel;
|
|
23
24
|
handlers: Set<LogHandler>;
|
|
24
25
|
constructor(name?: string, base?: Logger | null);
|
|
25
|
-
|
|
26
|
+
static getRealLevel(raw: LogLevel | string): LogLevel;
|
|
27
|
+
setLevel(level: LogLevel | string): void;
|
|
26
28
|
addHandler(handler: LogHandler): void;
|
|
27
29
|
/**
|
|
28
30
|
* 创建一个以当前 logger 为 base 的 child logger
|
|
29
31
|
*/
|
|
30
32
|
getChild(name: string): this;
|
|
31
|
-
log(level: LogLevel, args: unknown[]): void;
|
|
33
|
+
log(level: LogLevel | string, args: unknown[]): void;
|
|
32
34
|
protected logByInfo(info: LogInfo): void;
|
|
33
35
|
debug(...args: any[]): void;
|
|
34
36
|
info(...args: any[]): void;
|
package/logging/index.js
CHANGED
|
@@ -11,6 +11,14 @@ export var LogLevel;
|
|
|
11
11
|
LogLevel[LogLevel["Warning"] = 3] = "Warning";
|
|
12
12
|
LogLevel[LogLevel["Error"] = 4] = "Error";
|
|
13
13
|
})(LogLevel || (LogLevel = {}));
|
|
14
|
+
export const logLevelMap = {
|
|
15
|
+
debug: LogLevel.Debug,
|
|
16
|
+
info: LogLevel.Info,
|
|
17
|
+
warn: LogLevel.Warning,
|
|
18
|
+
warning: LogLevel.Warning,
|
|
19
|
+
err: LogLevel.Error,
|
|
20
|
+
error: LogLevel.Error,
|
|
21
|
+
};
|
|
14
22
|
export class LogHandler {
|
|
15
23
|
log(info) { } // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
16
24
|
}
|
|
@@ -19,12 +27,22 @@ export class Logger {
|
|
|
19
27
|
base;
|
|
20
28
|
level = LogLevel.Info;
|
|
21
29
|
handlers = new Set();
|
|
22
|
-
constructor(name = '', base = null
|
|
30
|
+
constructor(name = '', base = null // 指定上级 logger,当前 logger 记录的日志也会传递给上级
|
|
31
|
+
) {
|
|
23
32
|
this.name = name;
|
|
24
33
|
this.base = base;
|
|
25
34
|
}
|
|
35
|
+
static getRealLevel(raw) {
|
|
36
|
+
if (typeof raw === 'string') {
|
|
37
|
+
raw = raw.toLocaleLowerCase();
|
|
38
|
+
if (logLevelMap[raw] === undefined)
|
|
39
|
+
throw new Error('Not supported log level: ' + raw);
|
|
40
|
+
return logLevelMap[raw];
|
|
41
|
+
}
|
|
42
|
+
return raw;
|
|
43
|
+
}
|
|
26
44
|
setLevel(level) {
|
|
27
|
-
this.level = level;
|
|
45
|
+
this.level = Logger.getRealLevel(level);
|
|
28
46
|
}
|
|
29
47
|
addHandler(handler) {
|
|
30
48
|
this.handlers.add(handler);
|
|
@@ -38,6 +56,7 @@ export class Logger {
|
|
|
38
56
|
return new this.constructor(fullname, this);
|
|
39
57
|
}
|
|
40
58
|
log(level, args) {
|
|
59
|
+
level = Logger.getRealLevel(level);
|
|
41
60
|
this.logByInfo({ logger: this.name, level, time: dayjs(), args });
|
|
42
61
|
}
|
|
43
62
|
logByInfo(info) {
|
package/package.json
CHANGED
package/src/logging/adapt.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Debug } from 'debug'
|
|
2
|
-
import { getLogger } from './index.js'
|
|
2
|
+
import { getLogger, type Logger } from './index.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 适配 debug package
|
|
6
6
|
*/
|
|
7
|
-
export function adaptDebugLib(debugLib: Debug, enable = '') {
|
|
7
|
+
export function adaptDebugLib(debugLib: Debug, enable = '', logger?: Logger) {
|
|
8
8
|
// 不在 localStorage 里记录 debugLib enable 状态,
|
|
9
9
|
// 以解决 web worker 里读不到 localStorage 而无法启用 debugLib 日志的问题
|
|
10
10
|
const emulate = {
|
|
@@ -31,7 +31,7 @@ export function adaptDebugLib(debugLib: Debug, enable = '') {
|
|
|
31
31
|
Object.assign(debugLib, emulate)
|
|
32
32
|
|
|
33
33
|
// 将 debugLib 日志转发给 logger
|
|
34
|
-
|
|
34
|
+
if (!logger) logger = getLogger('3rd-library')
|
|
35
35
|
debugLib.log = logger.debug.bind(logger)
|
|
36
36
|
|
|
37
37
|
if (enable) {
|
package/src/logging/index.ts
CHANGED
|
@@ -13,6 +13,15 @@ export enum LogLevel {
|
|
|
13
13
|
Error = 4,
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export const logLevelMap: Record<string, LogLevel> = {
|
|
17
|
+
debug: LogLevel.Debug,
|
|
18
|
+
info: LogLevel.Info,
|
|
19
|
+
warn: LogLevel.Warning,
|
|
20
|
+
warning: LogLevel.Warning,
|
|
21
|
+
err: LogLevel.Error,
|
|
22
|
+
error: LogLevel.Error,
|
|
23
|
+
}
|
|
24
|
+
|
|
16
25
|
export interface LogInfo {
|
|
17
26
|
logger: string // logger name;有多级 logger 的情况下,这是最初的 logger 名称
|
|
18
27
|
level: LogLevel
|
|
@@ -30,11 +39,20 @@ export class Logger {
|
|
|
30
39
|
|
|
31
40
|
constructor(
|
|
32
41
|
public name: string = '',
|
|
33
|
-
public base: Logger | null = null
|
|
42
|
+
public base: Logger | null = null // 指定上级 logger,当前 logger 记录的日志也会传递给上级
|
|
34
43
|
) {}
|
|
35
44
|
|
|
36
|
-
|
|
37
|
-
|
|
45
|
+
static getRealLevel(raw: LogLevel | string) {
|
|
46
|
+
if (typeof raw === 'string') {
|
|
47
|
+
raw = raw.toLocaleLowerCase()
|
|
48
|
+
if (logLevelMap[raw] === undefined) throw new Error('Not supported log level: ' + raw)
|
|
49
|
+
return logLevelMap[raw]!
|
|
50
|
+
}
|
|
51
|
+
return raw
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
setLevel(level: LogLevel | string) {
|
|
55
|
+
this.level = Logger.getRealLevel(level)
|
|
38
56
|
}
|
|
39
57
|
|
|
40
58
|
addHandler(handler: LogHandler) {
|
|
@@ -51,7 +69,8 @@ export class Logger {
|
|
|
51
69
|
return new (this.constructor as Constructor)(fullname, this) as this
|
|
52
70
|
}
|
|
53
71
|
|
|
54
|
-
log(level: LogLevel, args: unknown[]) {
|
|
72
|
+
log(level: LogLevel | string, args: unknown[]) {
|
|
73
|
+
level = Logger.getRealLevel(level)
|
|
55
74
|
this.logByInfo({ logger: this.name, level, time: dayjs(), args })
|
|
56
75
|
}
|
|
57
76
|
protected logByInfo(info: LogInfo) {
|