2024-09-27 12:13:39 +08:00
|
|
|
import store from "../store";
|
|
|
|
|
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;
|
|
|
|
|
this.unsubscribe = store.subscribe(() => {
|
|
|
|
|
const state = store.getState();
|
|
|
|
|
logLevel = state.settings.logLevel || "debug";
|
|
|
|
|
this.updateLogLevel(logLevel);
|
|
|
|
|
});
|
|
|
|
|
}
|
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();
|