@coffer-org/sdk 1.3.1 → 1.4.0
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/logger.d.ts +18 -0
- package/dist/logger.js +57 -0
- package/package.json +1 -1
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface Logger {
|
|
2
|
+
debug(msg: string, extra?: Record<string, unknown>): void;
|
|
3
|
+
info(msg: string, extra?: Record<string, unknown>): void;
|
|
4
|
+
warn(msg: string, extra?: unknown): void;
|
|
5
|
+
error(msg: string, extra?: unknown): void;
|
|
6
|
+
}
|
|
7
|
+
export interface RootLogger {
|
|
8
|
+
child(bindings: Record<string, unknown>): RootLogger;
|
|
9
|
+
debug(obj: unknown, msg?: string): void;
|
|
10
|
+
info(obj: unknown, msg?: string): void;
|
|
11
|
+
warn(obj: unknown, msg?: string): void;
|
|
12
|
+
error(obj: unknown, msg?: string): void;
|
|
13
|
+
}
|
|
14
|
+
export declare const LOG_LEVEL: string;
|
|
15
|
+
export declare function parseTags(raw: string | undefined): Set<string>;
|
|
16
|
+
export declare function tagEnabled(tag: string, tags: Set<string>): boolean;
|
|
17
|
+
export declare function setRootLogger(l: RootLogger | null): void;
|
|
18
|
+
export declare function getLogger(tag: string): Logger;
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const LEVEL_NUM = { trace: 10, debug: 20, info: 30, warn: 40, error: 50 };
|
|
2
|
+
export const LOG_LEVEL = process.env.LOG_LEVEL ?? 'info';
|
|
3
|
+
export function parseTags(raw) {
|
|
4
|
+
return new Set((raw ?? '')
|
|
5
|
+
.split(',')
|
|
6
|
+
.map((t) => t.trim())
|
|
7
|
+
.filter(Boolean));
|
|
8
|
+
}
|
|
9
|
+
export function tagEnabled(tag, tags) {
|
|
10
|
+
return tags.size === 0 || tags.has(tag);
|
|
11
|
+
}
|
|
12
|
+
const TAGS = parseTags(process.env.LOG_TAGS);
|
|
13
|
+
const CONSOLE_THRESHOLD = LEVEL_NUM[LOG_LEVEL] ?? LEVEL_NUM.info;
|
|
14
|
+
let root = null;
|
|
15
|
+
const children = new Map();
|
|
16
|
+
export function setRootLogger(l) {
|
|
17
|
+
root = l;
|
|
18
|
+
children.clear();
|
|
19
|
+
}
|
|
20
|
+
export function getLogger(tag) {
|
|
21
|
+
const emit = (level, msg, extra) => {
|
|
22
|
+
if (!tagEnabled(tag, TAGS))
|
|
23
|
+
return;
|
|
24
|
+
if (root) {
|
|
25
|
+
let child = children.get(tag);
|
|
26
|
+
if (!child) {
|
|
27
|
+
child = root.child({ tag });
|
|
28
|
+
children.set(tag, child);
|
|
29
|
+
}
|
|
30
|
+
if (extra === undefined)
|
|
31
|
+
child[level](msg);
|
|
32
|
+
else if (extra instanceof Error)
|
|
33
|
+
child[level]({ err: extra }, msg);
|
|
34
|
+
else if (typeof extra === 'object')
|
|
35
|
+
child[level](extra, msg);
|
|
36
|
+
else
|
|
37
|
+
child[level]({ err: extra }, msg);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (LEVEL_NUM[level] < CONSOLE_THRESHOLD)
|
|
41
|
+
return;
|
|
42
|
+
const line = `[${tag}] ${msg}`;
|
|
43
|
+
const rest = extra === undefined ? [] : [extra];
|
|
44
|
+
if (level === 'error')
|
|
45
|
+
console.error(line, ...rest);
|
|
46
|
+
else if (level === 'warn')
|
|
47
|
+
console.warn(line, ...rest);
|
|
48
|
+
else
|
|
49
|
+
console.log(line, ...rest);
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
debug: (msg, extra) => emit('debug', msg, extra),
|
|
53
|
+
info: (msg, extra) => emit('info', msg, extra),
|
|
54
|
+
warn: (msg, extra) => emit('warn', msg, extra),
|
|
55
|
+
error: (msg, extra) => emit('error', msg, extra),
|
|
56
|
+
};
|
|
57
|
+
}
|