Skip to content

Commit

Permalink
feat(swap): Log tracing in rolling log files (#155)
Browse files Browse the repository at this point in the history
We now log verbose messages to hourly rotating `tracing*.log` which are kept for 24 hours. General logs are written to `swap-all.log`.
  • Loading branch information
binarybaron authored Nov 16, 2024
1 parent 56102fa commit 3085eee
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- ASB: We now log verbose messages to hourly rotating `tracing*.log` which are kept for 24 hours. General logs are written to `swap-all.log`.

## [1.0.0-rc.2] - 2024-11-16

- GUI: ASBs discovered via rendezvous are now prioritized if they are running the latest version
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

12 changes: 6 additions & 6 deletions swap/src/asb/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ where
"Communication error: {:#}", error);
}
SwarmEvent::ConnectionEstablished { peer_id: peer, endpoint, .. } => {
tracing::debug!(%peer, address = %endpoint.get_remote_address(), "New connection established");
tracing::trace!(%peer, address = %endpoint.get_remote_address(), "New connection established");

// If we have buffered transfer proofs for this peer, we can now send them
if let Some(transfer_proofs) = self.buffered_transfer_proofs.remove(&peer) {
Expand All @@ -429,13 +429,13 @@ where
}
}
SwarmEvent::IncomingConnectionError { send_back_addr: address, error, .. } => {
tracing::warn!(%address, "Failed to set up connection with peer: {:#}", error);
tracing::trace!(%address, "Failed to set up connection with peer: {:#}", error);
}
SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: Some(error), connection_id } => {
tracing::debug!(%peer, address = %endpoint.get_remote_address(), %connection_id, "Lost connection to peer: {:#}", error);
tracing::trace!(%peer, address = %endpoint.get_remote_address(), %connection_id, "Lost connection to peer: {:#}", error);
}
SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: None, connection_id } => {
tracing::info!(%peer, address = %endpoint.get_remote_address(), %connection_id, "Successfully closed connection");
tracing::trace!(%peer, address = %endpoint.get_remote_address(), %connection_id, "Successfully closed connection");
}
SwarmEvent::NewListenAddr{address, ..} => {
tracing::info!(%address, "New listen address reported");
Expand Down Expand Up @@ -493,7 +493,7 @@ where
tracing::debug!(%ask_price, %xmr_balance, %max_bitcoin_for_monero, "Computed quote");

if min_buy > max_bitcoin_for_monero {
tracing::warn!(
tracing::trace!(
"Your Monero balance is too low to initiate a swap, as your minimum swap amount is {}. You could at most swap {}",
min_buy, max_bitcoin_for_monero
);
Expand All @@ -506,7 +506,7 @@ where
}

if max_buy > max_bitcoin_for_monero {
tracing::warn!(
tracing::trace!(
"Your Monero balance is too low to initiate a swap with the maximum swap amount {} that you have specified in your config. You can at most swap {}",
max_buy, max_bitcoin_for_monero
);
Expand Down
53 changes: 35 additions & 18 deletions swap/src/common/tracing_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::Path;
use std::str::FromStr;

use anyhow::Result;
use tracing_appender::rolling::{RollingFileAppender, Rotation};
use tracing_subscriber::filter::{Directive, LevelFilter};
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::fmt::MakeWriter;
Expand Down Expand Up @@ -31,8 +32,17 @@ pub fn init(
tauri_handle: Option<TauriHandle>,
) -> Result<()> {
// file logger will always write in JSON format and with timestamps
let file_appender = tracing_appender::rolling::never(&dir, "swap-all.log");
let file_appender: RollingFileAppender = tracing_appender::rolling::never(&dir, "swap-all.log");

let tracing_file_appender: RollingFileAppender = RollingFileAppender::builder()
.rotation(Rotation::HOURLY)
.filename_prefix("tracing")
.filename_suffix("log")
.max_log_files(24)
.build(&dir)
.expect("initializing rolling file appender failed");

// Log to file
let file_layer = fmt::layer()
.with_writer(file_appender)
.with_ansi(false)
Expand All @@ -41,15 +51,23 @@ pub fn init(
.json()
.with_filter(env_filter(level_filter)?);

// terminal loger
let tracing_file_layer = fmt::layer()
.with_writer(tracing_file_appender)
.with_ansi(false)
.with_timer(UtcTime::rfc_3339())
.with_target(false)
.json()
.with_filter(env_filter(LevelFilter::TRACE)?);

// Log to stdout
let is_terminal = atty::is(atty::Stream::Stderr);
let terminal_layer = fmt::layer()
.with_writer(std::io::stdout)
.with_ansi(is_terminal)
.with_timer(UtcTime::rfc_3339())
.with_target(false);

// tauri layer (forwards logs to the tauri guest when connected)
// Forwards logs to the tauri guest
let tauri_layer = fmt::layer()
.with_writer(TauriWriter::new(tauri_handle))
.with_ansi(false)
Expand All @@ -58,23 +76,22 @@ pub fn init(
.json()
.with_filter(env_filter(level_filter)?);

// combine the layers and start logging, format with json if specified
if let Format::Json = format {
tracing_subscriber::registry()
.with(file_layer)
.with(tauri_layer)
.with(terminal_layer.json().with_filter(env_filter(level_filter)?))
.try_init()?;
} else {
tracing_subscriber::registry()
.with(file_layer)
.with(tauri_layer)
.with(terminal_layer.with_filter(env_filter(level_filter)?))
.try_init()?;
}
let env_filtered = env_filter(level_filter)?;

let final_terminal_layer = match format {
Format::Json => terminal_layer.json().with_filter(env_filtered).boxed(),
Format::Raw => terminal_layer.with_filter(env_filtered).boxed(),
};

tracing_subscriber::registry()
.with(file_layer)
.with(tracing_file_layer)
.with(final_terminal_layer)
.with(tauri_layer)
.try_init()?;

// Now we can use the tracing macros to log messages
tracing::info!(%level_filter, logs_dir=%dir.as_ref().display(), "Initialized tracing");
tracing::info!(%level_filter, logs_dir=%dir.as_ref().display(), "Initialized tracing. General logs will be written to swap-all.log, and verbose logs to tracing*.log");

Ok(())
}
Expand Down
2 changes: 0 additions & 2 deletions swap/src/kraken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ mod connection {
return Ok(None);
}
Ok(wire::Event::Heartbeat) => {
tracing::trace!("Received heartbeat message");

return Ok(None);
}
// if the message is not an event, it is a ticker update or an unknown event
Expand Down

0 comments on commit 3085eee

Please sign in to comment.