diff --git a/src-tauri/log4rs_sample.yml b/src-tauri/log4rs_sample.yml index fc5999643..10aafae6d 100644 --- a/src-tauri/log4rs_sample.yml +++ b/src-tauri/log4rs_sample.yml @@ -20,7 +20,22 @@ appenders: - kind: threshold level: warn - + # An appender named "web" that writes to a file with a custom pattern encoder + web: + kind: rolling_file + path: "{{log_dir}}/universe/universe-web.log" + policy: + kind: compound + trigger: + kind: size + limit: 10mb + roller: + kind: fixed_window + base: 1 + count: 5 + pattern: "{{log_dir}}/universe/universe-web.{}.log" + encoder: + pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} {l:5} {m}{n} " # An appender named "base_layer" that writes to a file with a custom pattern encoder default: @@ -68,4 +83,7 @@ loggers: level: debug appenders: - default - + tari::universe::web: + level: info + appenders: + - web diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 75902fd10..4a753dbfe 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -535,6 +535,14 @@ async fn status(state: tauri::State<'_, UniverseAppState>) -> Result) { + match level.as_str() { + "error" => error!(target: LOG_TARGET_WEB, "{}", message.join(" ")), + _ => info!(target: LOG_TARGET_WEB, "{}", message.join(" ")), + } +} + #[derive(Debug, Serialize)] pub struct AppStatus { cpu: CpuMinerStatus, @@ -605,6 +613,7 @@ struct Payload { } pub const LOG_TARGET: &str = "tari::universe::main"; +pub const LOG_TARGET_WEB: &str = "tari::universe::web"; fn main() { let default_hook = panic::take_hook(); @@ -724,6 +733,7 @@ fn main() { get_applications_versions, set_user_inactivity_timeout, update_applications, + log_web_message, set_telemetry_mode, set_airdrop_access_token ]) diff --git a/src/App.tsx b/src/App.tsx index 880b0e9b3..2c4bb637a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,5 @@ import './theme/theme.css'; -import { StrictMode } from 'react'; +import { StrictMode, useEffect } from 'react'; import CssBaseline from '@mui/material/CssBaseline'; import { ThemeProvider } from '@mui/material/styles'; import { lightTheme } from './theme/themes'; @@ -15,6 +15,7 @@ import { useEnvironment } from './hooks/useEnvironment.ts'; import { useAirdropTokensRefresh } from './hooks/airdrop/useAirdropTokensRefresh.ts'; import { SplashScreen } from './containers/SplashScreen'; import { useMiningEffects } from './hooks/mining/useMiningEffects.ts'; +import { setupLogger } from './utils/logger.ts'; function App() { useAirdropTokensRefresh(); @@ -26,6 +27,10 @@ function App() { const view = useUIStore((s) => s.view); const showSplash = useUIStore((s) => s.showSplash); + useEffect(() => { + setupLogger(); + }, []); + return ( diff --git a/src/types/invoke.ts b/src/types/invoke.ts index 637a9ee0e..6fb8552b8 100644 --- a/src/types/invoke.ts +++ b/src/types/invoke.ts @@ -13,4 +13,8 @@ declare module '@tauri-apps/api/tauri' { function invoke(param: 'set_mode', payload: { mode: modeType }): Promise; function invoke(param: 'get_seed_words'): Promise; function invoke(param: 'get_applications_versions'): Promise; + function invoke( + param: 'log_web_message', + payload: { level: 'log' | 'error' | 'warn' | 'info'; message: string } + ): Promise; } diff --git a/src/utils/logger.ts b/src/utils/logger.ts new file mode 100644 index 000000000..04e46cf72 --- /dev/null +++ b/src/utils/logger.ts @@ -0,0 +1,43 @@ +import { invoke } from '@tauri-apps/api'; + +// Override console functions +const originalConsoleLog = console.log; +const originalConsoleInfo = console.info; +const originalConsoleError = console.error; + +const parseArgument = (a: any) => { + try { + return JSON.stringify(a, null, 2); + } catch (e) { + return String(a); + } +}; + +export const setupLogger = () => { + // Override console.log + console.log = function (...args) { + invoke('log_web_message', { + level: 'log', + message: args.map(parseArgument), + }); + originalConsoleLog(...args); + }; + + // Override console.info + console.info = function (...args) { + invoke('log_web_message', { + level: 'info', + message: args.map(parseArgument), + }); + originalConsoleInfo(...args); + }; + + // Override console.error + console.error = function (...args) { + invoke('log_web_message', { + level: 'error', + message: args.map(parseArgument), + }); + originalConsoleError(...args); + }; +};