|
1 | | -export const LOG_LEVEL = { |
2 | | - INFO: 0, |
3 | | - WARNING: 1, |
4 | | - ERROR: 2, |
5 | | - DISABLE: 3, |
6 | | -}; |
| 1 | +const _global: typeof globalThis = (() => { |
| 2 | + if (typeof globalThis !== "undefined") return globalThis; // ES2020 |
| 3 | + if (typeof window !== "undefined") return window; // Browser |
| 4 | + if (typeof global !== "undefined") return global; // Node |
| 5 | + if (typeof self !== "undefined") return self; // Worker |
| 6 | + return Function("return this")(); // Fallback |
| 7 | +})(); |
| 8 | + |
| 9 | +export const log = _global.console.log; |
| 10 | +export const warn = _global.console.warn; |
| 11 | +export const error = _global.console.error; |
| 12 | +export const trace = _global.console.trace; |
| 13 | +export const LOG_LEVEL = { INFO: 0, WARNING: 1, ERROR: 2, DISABLE: 3 }; |
7 | 14 |
|
8 | 15 | class Logger { |
9 | | - constructor(private level: number) {} |
| 16 | + public constructor(private level: number) {} |
10 | 17 |
|
11 | | - setLevel(level: typeof LOG_LEVEL[keyof typeof LOG_LEVEL]) { |
| 18 | + public setLevel(level: typeof LOG_LEVEL[keyof typeof LOG_LEVEL]) { |
12 | 19 | this.level = level; |
13 | 20 | } |
14 | 21 |
|
15 | | - info(...args: unknown[]) { |
| 22 | + public info(...args: unknown[]) { |
16 | 23 | if (this.level <= LOG_LEVEL.INFO) { |
17 | | - console.log("FC Log:", ...args); |
| 24 | + log("FC Log:", ...args); |
18 | 25 | } |
19 | 26 | } |
20 | 27 |
|
21 | | - trace(...args: unknown[]) { |
| 28 | + public trace(...args: unknown[]) { |
22 | 29 | if (this.level <= LOG_LEVEL.INFO) { |
23 | | - console.trace("FC Trace:", ...args); |
| 30 | + trace("FC Trace:", ...args); |
24 | 31 | } |
25 | 32 | } |
26 | 33 |
|
27 | | - warning(...args: unknown[]) { |
| 34 | + public warning(...args: unknown[]) { |
28 | 35 | if (this.level <= LOG_LEVEL.WARNING) { |
29 | | - console.warn("FC Warning:", ...args); |
| 36 | + warn("FC Warning:", ...args); |
30 | 37 | } |
31 | 38 | } |
32 | 39 |
|
33 | | - error(...args: unknown[]) { |
| 40 | + public error(...args: unknown[]) { |
34 | 41 | if (this.level <= LOG_LEVEL.ERROR) { |
35 | | - console.error("FC Error:", ...args); |
| 42 | + error("FC Error:", ...args); |
36 | 43 | } |
37 | 44 | } |
38 | 45 | } |
|
0 commit comments