Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(log): pipe web logs to the file #217

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src-tauri/log4rs_sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -68,4 +83,7 @@ loggers:
level: debug
appenders:
- default

tari::universe::web:
level: info
appenders:
- web
10 changes: 10 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,14 @@ async fn status(state: tauri::State<'_, UniverseAppState>) -> Result<AppStatus,
})
}

#[tauri::command]
fn log_web_message(level: String, message: Vec<String>) {
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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
])
Expand Down
7 changes: 6 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
Expand All @@ -26,6 +27,10 @@ function App() {
const view = useUIStore((s) => s.view);
const showSplash = useUIStore((s) => s.showSplash);

useEffect(() => {
setupLogger();
}, []);

return (
<StrictMode>
<ThemeProvider theme={lightTheme}>
Expand Down
4 changes: 4 additions & 0 deletions src/types/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ declare module '@tauri-apps/api/tauri' {
function invoke(param: 'set_mode', payload: { mode: modeType }): Promise<void>;
function invoke(param: 'get_seed_words'): Promise<string[]>;
function invoke(param: 'get_applications_versions'): Promise<ApplicationsVersions>;
function invoke(
param: 'log_web_message',
payload: { level: 'log' | 'error' | 'warn' | 'info'; message: string }
): Promise<ApplicationsVersions>;
}
43 changes: 43 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { invoke } from '@tauri-apps/api';

// Override console functions
const originalConsoleLog = console.log;

Check warning on line 4 in src/utils/logger.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unexpected console statement
const originalConsoleInfo = console.info;
const originalConsoleError = console.error;

const parseArgument = (a: any) => {

Check warning on line 8 in src/utils/logger.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unexpected any. Specify a different type
try {
return JSON.stringify(a, null, 2);
} catch (e) {

Check warning on line 11 in src/utils/logger.ts

View workflow job for this annotation

GitHub Actions / Run linters

'e' is defined but never used
return String(a);
}
};

export const setupLogger = () => {
// Override console.log
console.log = function (...args) {

Check warning on line 18 in src/utils/logger.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unexpected console statement
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);
};
};