feat: implement console logger with backend integration and file logging

This commit is contained in:
Flavio Fois
2026-02-05 12:39:11 +01:00
parent 654475d3ea
commit 3fb2f95e8a
6 changed files with 122 additions and 1 deletions

View File

@@ -0,0 +1,56 @@
import { FrontendLog } from '$lib/wailsjs/go/main/App';
function safeStringify(obj: any): string {
try {
if (typeof obj === 'object' && obj !== null) {
return JSON.stringify(obj);
}
return String(obj);
} catch (e) {
return '[Circular/Error]';
}
}
export function setupConsoleLogger() {
if ((window as any).__logger_initialized__) return;
(window as any).__logger_initialized__ = true;
const originalLog = console.log;
const originalWarn = console.warn;
const originalError = console.error;
const originalInfo = console.info;
function logToBackend(level: string, args: any[]) {
try {
// Avoid logging if wails runtime is not ready or function is missing
if (typeof FrontendLog !== 'function') return;
const message = args.map(arg => safeStringify(arg)).join(' ');
FrontendLog(level, message).catch(() => {});
} catch (e) {
// ignore
}
}
console.log = (...args) => {
originalLog(...args);
logToBackend("INFO", args);
};
console.warn = (...args) => {
originalWarn(...args);
logToBackend("WARN", args);
};
console.error = (...args) => {
originalError(...args);
logToBackend("ERROR", args);
};
console.info = (...args) => {
originalInfo(...args);
logToBackend("INFO", args);
};
originalLog("Console logger hooked to Wails backend");
}