2024-09-27 12:13:39 +08:00
|
|
|
const LOG_LEVEL = import.meta.env.VITE_APP_LOG_LEVEL || "debug";
|
2024-10-09 14:42:27 +08:00
|
|
|
const NO_OP = () => {};
|
|
|
|
|
|
2024-08-19 14:20:55 -07:00
|
|
|
class Logger {
|
2024-10-17 12:05:06 +08:00
|
|
|
constructor() {
|
|
|
|
|
let logLevel = LOG_LEVEL;
|
2026-01-24 16:19:43 +00:00
|
|
|
this.updateLogLevel(logLevel);
|
|
|
|
|
// Defer store subscription to avoid circular dependency during HMR
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
try {
|
|
|
|
|
import("../store").then(({ default: store }) => {
|
|
|
|
|
if (store) {
|
|
|
|
|
this.unsubscribe = store.subscribe(() => {
|
|
|
|
|
logLevel = "debug";
|
|
|
|
|
this.updateLogLevel(logLevel);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Store not ready yet, ignore
|
|
|
|
|
}
|
|
|
|
|
}, 0);
|
2024-10-17 12:05:06 +08:00
|
|
|
}
|
2024-09-27 12:13:39 +08:00
|
|
|
|
2024-10-17 12:05:06 +08:00
|
|
|
updateLogLevel(logLevel) {
|
|
|
|
|
if (logLevel === "none") {
|
|
|
|
|
this.info = NO_OP;
|
|
|
|
|
this.error = NO_OP;
|
|
|
|
|
this.warn = NO_OP;
|
|
|
|
|
this.log = NO_OP;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-19 14:28:18 -07:00
|
|
|
|
2024-10-17 12:05:06 +08:00
|
|
|
if (logLevel === "error") {
|
|
|
|
|
this.error = console.error.bind(console);
|
|
|
|
|
this.info = NO_OP;
|
|
|
|
|
this.warn = NO_OP;
|
|
|
|
|
this.log = NO_OP;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-19 14:20:55 -07:00
|
|
|
|
2024-10-17 12:05:06 +08:00
|
|
|
if (logLevel === "warn") {
|
|
|
|
|
this.error = console.error.bind(console);
|
|
|
|
|
this.warn = console.warn.bind(console);
|
|
|
|
|
this.info = NO_OP;
|
|
|
|
|
this.log = NO_OP;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-09 12:06:01 +08:00
|
|
|
|
2024-10-17 12:05:06 +08:00
|
|
|
if (logLevel === "info") {
|
|
|
|
|
this.error = console.error.bind(console);
|
|
|
|
|
this.warn = console.warn.bind(console);
|
|
|
|
|
this.info = console.info.bind(console);
|
|
|
|
|
this.log = NO_OP;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-25 10:06:40 +08:00
|
|
|
|
|
|
|
|
if (logLevel === "debug") {
|
|
|
|
|
this.error = console.error.bind(console);
|
|
|
|
|
this.warn = console.warn.bind(console);
|
|
|
|
|
this.info = console.info.bind(console);
|
|
|
|
|
this.log = console.log.bind(console);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-17 12:05:06 +08:00
|
|
|
}
|
2024-09-27 12:13:39 +08:00
|
|
|
|
2024-10-17 12:05:06 +08:00
|
|
|
cleanup() {
|
|
|
|
|
if (this.unsubscribe) {
|
|
|
|
|
this.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-19 14:20:55 -07:00
|
|
|
}
|
|
|
|
|
|
2024-09-27 12:13:39 +08:00
|
|
|
export const logger = new Logger();
|