const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null;
const levels = {
DEBUG: 'debug',
INFO: 'info',
WARN: 'warn',
ERROR: 'error'
};
const prefixes = {
DEBUG: '[DEBUG]',
INFO: '[INFO]',
WARN: '[WARN]',
ERROR: '[ERROR]'
};
class Logger {
constructor(level = levels.INFO) {
this.level = level;
}
setLevel(level) {
this.level = level;
}
log(level, ...args) {
if (this.shouldLog(level)) {
const timestamp = new Date().toLocaleString();
const prefix = prefixes[level.toUpperCase()] || '';
const logMessage = `[${timestamp}] ${prefix} ${args.join(' ')}`;
console.log(logMessage);
this.sendToRealtimeLog(level, ...args);
}
}
debug(...args) {
this.log(levels.DEBUG, ...args);
}
info(...args) {
this.log(levels.INFO, ...args);
}
warn(...args) {
this.log(levels.WARN, ...args);
}
error(...args) {
this.log(levels.ERROR, ...args);
}
shouldLog(level) {
const levelOrder = [levels.DEBUG, levels.INFO, levels.WARN, levels.ERROR];
return levelOrder.indexOf(level) >= levelOrder.indexOf(this.level);
}
sendToRealtimeLog(level, ...args) {
if (!log) {
return;
}
switch (level) {
case levels.INFO:
log.info.apply(log, args);
break;
case levels.WARN:
log.warn.apply(log, args);
break;
case levels.ERROR:
log.error.apply(log, args);
break;
default:
log.debug ? log.debug.apply(log, args) : log.info.apply(log, args);
break;
}
}
setFilterMsg(msg) {
if (!log || !log.setFilterMsg) {
return;
}
if (typeof msg !== "string") {
return;
}
log.setFilterMsg(msg);
}
addFilterMsg(msg) {
if (!log || !log.addFilterMsg) {
return;
}
if (typeof msg !== "string") {
return;
}
log.addFilterMsg(msg);
}
}
const logger = new Logger(levels.DEBUG);
export default logger;
export { levels };