Skip to content

Commit 77cb177

Browse files
committed
feat: logger compat
1 parent b7dde3b commit 77cb177

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

packages/force-copy/src/utils/logger.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
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 };
714

815
class Logger {
9-
constructor(private level: number) {}
16+
public constructor(private level: number) {}
1017

11-
setLevel(level: typeof LOG_LEVEL[keyof typeof LOG_LEVEL]) {
18+
public setLevel(level: typeof LOG_LEVEL[keyof typeof LOG_LEVEL]) {
1219
this.level = level;
1320
}
1421

15-
info(...args: unknown[]) {
22+
public info(...args: unknown[]) {
1623
if (this.level <= LOG_LEVEL.INFO) {
17-
console.log("FC Log:", ...args);
24+
log("FC Log:", ...args);
1825
}
1926
}
2027

21-
trace(...args: unknown[]) {
28+
public trace(...args: unknown[]) {
2229
if (this.level <= LOG_LEVEL.INFO) {
23-
console.trace("FC Trace:", ...args);
30+
trace("FC Trace:", ...args);
2431
}
2532
}
2633

27-
warning(...args: unknown[]) {
34+
public warning(...args: unknown[]) {
2835
if (this.level <= LOG_LEVEL.WARNING) {
29-
console.warn("FC Warning:", ...args);
36+
warn("FC Warning:", ...args);
3037
}
3138
}
3239

33-
error(...args: unknown[]) {
40+
public error(...args: unknown[]) {
3441
if (this.level <= LOG_LEVEL.ERROR) {
35-
console.error("FC Error:", ...args);
42+
error("FC Error:", ...args);
3643
}
3744
}
3845
}

0 commit comments

Comments
 (0)