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

feat: add logging #135

Merged
merged 6 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ axum = "0.7.5"
axum-jrpc = "0.3.2"
diesel = { version = "2.1.6", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] }
diesel_migrations = { version = "2.1.0", features = ["sqlite"] }
log4rs = { version = "1.3", features = [
"rolling_file_appender",
"compound_policy",
"size_trigger",
"fixed_window_roller",
"console_appender",
] }
log4rs = { version = "1.3", features = ["rolling_file_appender", "compound_policy", "size_trigger", "fixed_window_roller", "console_appender"] }
tower-http = { version = "0.5.2", features = ["fs", "trace"] }
tari_crypto = "0.20.0"
flate2 = "1.0.28"
Expand All @@ -56,3 +50,5 @@ sha2 = "0.10.8"
sha1 = "0.10.6"
thiserror = "1.0.60"
semver = "1.0.23"
serde_yaml = "0.9.34"
log = "0.4.22"
195 changes: 195 additions & 0 deletions src-tauri/log4rs/universe_sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# A sample log configuration file for running in release mode. By default, this configuration splits up log messages to
# the following destinations:
# * Console: For log messages with level INFO and higher
# * logs/universe: INFO-level logs related to the Tari Universe. This file will be quite busy, so logs are splited:
# logs/universe/universe.log
# logs/universe/universe-web.log
# logs/universe/other.log
# * logs/wallet_daemon: INFO-level logs related to the wallet daemon. This file will be quite busy, so logs are splited:
# logs/wallet_daemon/wallet_daemon.log
# logs/wallet_daemon/dan-layer.log
# logs/wallet_daemon/jason-rpc.log
# * logs/tapplet: INFO-level logs related to the active tapplet. At the moment only single file is created:
# logs/tapplet/tapplet.log
#
#
# See https://docs.rs/log4rs/0.8.3/log4rs/encode/pattern/index.html for deciphering the log pattern. The log format
# used in this sample configuration prints messages as:
# timestamp [target] LEVEL message
refresh_rate: 30 seconds
appenders:
# An appender named "stdout" that writes to stdout
stdout:
kind: console
encoder:
pattern: "{d(%H:%M)} {h({l}):5} {m}{n}"
filters:
- kind: threshold
level: info

# An appender named "default" that writes to a file with a custom pattern encoder
default:
kind: rolling_file
path: "{{log_dir}}/universe/log/universe.log"
policy:
kind: compound
trigger:
kind: size
limit: 2mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/universe/log/universe.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} {l:5} {m} // {f}:{L}{n}"

# An appender named "web" that writes to a file with a custom pattern encoder
web:
kind: rolling_file
path: "{{log_dir}}/universe/log/universe-web.log"
policy:
kind: compound
trigger:
kind: size
limit: 2mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/universe/log/universe-web.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} {l:5} {m}{n} "

# An appender named "other" that writes to a file with a custom pattern encoder
other:
kind: rolling_file
path: "{{log_dir}}/universe/log/other.log"
policy:
kind: compound
trigger:
kind: size
limit: 2mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/universe/log/other.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} {l:5} {m}{n} // {f}:{L} "

# An appender named "dan_layer" that writes to a file with a custom pattern encoder
dan_layer:
kind: rolling_file
path: "{{log_dir}}/wallet_daemon/log/dan_layer.log"
policy:
kind: compound
trigger:
kind: size
limit: 2mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/wallet_daemon/log/dan_layer.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] [{X(node-public-key)},{X(node-id)}] {l:5} {m} // {f}:{L}{n}"

# An appender named "dan_layer" that writes to a file with a custom pattern encoder
wallet_daemon:
kind: rolling_file
path: "{{log_dir}}/wallet_daemon/log/wallet_daemon.log"
policy:
kind: compound
trigger:
kind: size
limit: 2mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/wallet_daemon/log/wallet_daemon.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] [{X(node-public-key)},{X(node-id)}] {l:5} {m} // {f}:{L}{n}"

# An appender named "json_rpc" that writes to a file with a custom pattern encoder
json_rpc:
kind: rolling_file
path: "{{log_dir}}/wallet_daemon/log/json_rpc.log"
policy:
kind: compound
trigger:
kind: size
limit: 2mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/wallet_daemon/log/json_rpc.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] [{X(node-public-key)},{X(node-id)}] {l:5} {m} // {f}:{L}{n}"

# An appender named "tapplet" that writes to a file with a custom pattern encoder
tapplet:
kind: rolling_file
path: "{{log_dir}}/tapplet/log/tapplet.log"
policy:
kind: compound
trigger:
kind: size
limit: 2mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/tapplet/log/tapplet.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} {l:5} {m} // {f}:{L}{n}"

# Set the default logging level to "info"
root:
level: info
appenders:
- other
- stdout

loggers:
# Route log events common to every application to all appenders
tari::universe:
level: debug
appenders:
- stdout
- default
additive: false

tari::universe::web:
level: info
appenders:
- web
additive: false

tari::dan:
level: info
appenders:
- dan_layer
- stdout
additive: false

tari::dan::wallet_daemon:
level: info
appenders:
- wallet_daemon
- stdout
additive: false

tari::dan::wallet_daemon::json_rpc:
level: debug
appenders:
- json_rpc
additive: false

tari::tapplet:
level: info
appenders:
- stdout
additive: false
81 changes: 0 additions & 81 deletions src-tauri/log4rs_sample.yml

This file was deleted.

23 changes: 20 additions & 3 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use constants::TAPPLETS_ASSETS_DIR;
use diesel::SqliteConnection;
use fs::{ get_config_file, get_data_dir, get_log_dir };
use tapplet_server::start;
use log4rs::config::RawConfig;
use log::info;
use tapplet_server::{ setup_log, start };
use utils::logging_utils::setup_logging;
use std::{ collections::HashMap, sync::{ Arc, Mutex }, thread::sleep, time::Duration };
use tauri::{ self, Manager };
use tokio_util::sync::CancellationToken;
Expand All @@ -17,6 +20,7 @@ mod interface;
mod error;
mod constants;
mod fs;
mod utils;

use commands::{
call_wallet,
Expand Down Expand Up @@ -45,6 +49,7 @@ use commands::{

use crate::{ rpc::permission_token, wallet_daemon::start_wallet_daemon };

const LOG_TARGET: &str = "tari::universe::main";
pub struct Tokens {
auth: Mutex<String>,
permission: Mutex<String>,
Expand Down Expand Up @@ -74,11 +79,21 @@ async fn try_get_tokens() -> (String, String) {
fn setup_tari_universe(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
let data_dir_path = get_data_dir(app)?;
let log_path = get_log_dir(app)?;
let log_tapp_dir = log_path.clone();
let wallet_daemon_config_file = get_config_file(app, "wallet_daemon.config.toml")?;
let log_config_file = get_config_file(app, "wallet_daemon.log.yml")?;
// let log_config_file = get_config_file(app, "../log4rs/wallet_daemon.log.yml")?;

// setup universe logging
let log_config_file = &log_path.join("universe").join("configs").join("log4rs_config_universe.yml");
let contents = setup_logging(&log_config_file, &log_path, include_str!("../log4rs/universe_sample.yml"))?;
let config: RawConfig = serde_yaml
::from_str(&contents)
.expect("Could not parse the contents of the log file as yaml");
// global logger init
log4rs::init_raw_config(config).expect("Could not initialize logging");

tauri::async_runtime::spawn(async move {
start_wallet_daemon(log_path, data_dir_path, wallet_daemon_config_file, log_config_file).await.unwrap();
start_wallet_daemon(log_path, data_dir_path, wallet_daemon_config_file).await.unwrap();
});
let db_path = app.path().app_data_dir()?.to_path_buf().join(constants::DB_FILE_NAME);
app.manage(DatabaseConnection(Arc::new(Mutex::new(database::establish_connection(db_path.to_str().unwrap())))));
Expand All @@ -97,9 +112,11 @@ fn setup_tari_universe(app: &mut tauri::App) -> Result<(), Box<dyn std::error::E

let app_path = app.path().app_data_dir().unwrap().to_path_buf();
let tapplet_assets_path = app_path.join(TAPPLETS_ASSETS_DIR);
let _handle_setup_log = tauri::async_runtime::spawn(async move { setup_log(log_tapp_dir).await });
let handle_start = tauri::async_runtime::spawn(async move { start(tapplet_assets_path).await });
let (addr, cancel_token) = tauri::async_runtime::block_on(handle_start)?.unwrap();
app.manage(AssetServer { addr, cancel_token });
info!(target: LOG_TARGET, "Tari Universe setup completed successfully");

Ok(())
}
Expand Down
Loading