Append all cli logs to single log file

After careful consideration, I've concluded that it's not practical/possible to ensure that the previous behaviour (one log file per swap) is preserved due to limitations of the tracing-subscriber crate and a big in the built in JSON formatter
This commit is contained in:
binarybaron 2023-08-12 17:16:23 +02:00
parent f804254f94
commit 433bf824f9
2 changed files with 29 additions and 48 deletions

View file

@ -93,7 +93,7 @@ impl Context {
let tor_socks5_port = tor.map(|tor| tor.tor_socks5_port); let tor_socks5_port = tor.map(|tor| tor.tor_socks5_port);
START.call_once(|| { START.call_once(|| {
let _ = cli::tracing::init(debug, json, data_dir.join("logs"), None); let _ = cli::tracing::init(debug, json, data_dir.join("logs"));
}); });
let context = Context { let context = Context {

View file

@ -7,55 +7,36 @@ use tracing::{Event, Level, Subscriber};
use tracing_subscriber::fmt::format::{DefaultFields, Format, JsonFields}; use tracing_subscriber::fmt::format::{DefaultFields, Format, JsonFields};
use tracing_subscriber::fmt::time::UtcTime; use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::layer::{Context, SubscriberExt}; use tracing_subscriber::layer::{Context, SubscriberExt};
use tracing_subscriber::{fmt, EnvFilter, FmtSubscriber, Layer, Registry}; use tracing_subscriber::{fmt, EnvFilter, Layer, Registry};
use uuid::Uuid;
pub fn init(debug: bool, json: bool, dir: impl AsRef<Path>, swap_id: Option<Uuid>) -> Result<()> { pub fn init(debug: bool, json: bool, dir: impl AsRef<Path>) -> Result<()> {
if let Some(swap_id) = swap_id { let level_filter = EnvFilter::try_new("swap=debug")?;
let level_filter = EnvFilter::try_new("swap=debug")?; let registry = Registry::default().with(level_filter);
let registry = Registry::default().with(level_filter); let appender =
tracing_appender::rolling::never(dir.as_ref(), "swap-all.log");
let (appender, guard) = tracing_appender::non_blocking(appender);
let appender = std::mem::forget(guard);
tracing_appender::rolling::never(dir.as_ref(), format!("swap-{}.log", swap_id));
let (appender, guard) = tracing_appender::non_blocking(appender);
std::mem::forget(guard); let file_logger = registry.with(
fmt::layer()
.with_ansi(false)
.with_target(false)
.with_span_events(fmt::format::FmtSpan::FULL)
.json()
.with_writer(appender),
);
let file_logger = registry.with( if json && debug {
fmt::layer() set_global_default(file_logger.with(debug_json_terminal_printer()))?;
.with_ansi(false) } else if json && !debug {
.with_target(false) set_global_default(file_logger.with(info_json_terminal_printer()))?;
.json() } else if !json && debug {
.with_writer(appender), set_global_default(file_logger.with(debug_terminal_printer()))?;
);
if json && debug {
set_global_default(file_logger.with(debug_json_terminal_printer()))?;
} else if json && !debug {
set_global_default(file_logger.with(info_json_terminal_printer()))?;
} else if !json && debug {
set_global_default(file_logger.with(debug_terminal_printer()))?;
} else {
set_global_default(file_logger.with(info_terminal_printer()))?;
}
} else { } else {
let level = if debug { Level::DEBUG } else { Level::INFO }; set_global_default(file_logger.with(info_terminal_printer()))?;
let is_terminal = atty::is(atty::Stream::Stderr); }
let builder = FmtSubscriber::builder()
.with_env_filter(format!("swap={}", level))
.with_writer(std::io::stderr)
.with_ansi(is_terminal)
.with_timer(UtcTime::rfc_3339())
.with_target(false);
if json {
builder.json().init();
} else {
builder.init();
}
};
tracing::info!("Logging initialized to {}", dir.as_ref().display()); tracing::info!("Logging initialized to {}", dir.as_ref().display());
Ok(()) Ok(())
@ -66,17 +47,17 @@ pub struct StdErrPrinter<L> {
level: Level, level: Level,
} }
type StdErrLayer<S, T> = tracing_subscriber::fmt::Layer< type StdErrLayer<S, T> = fmt::Layer<
S, S,
DefaultFields, DefaultFields,
Format<tracing_subscriber::fmt::format::Full, T>, Format<fmt::format::Full, T>,
fn() -> std::io::Stderr, fn() -> std::io::Stderr,
>; >;
type StdErrJsonLayer<S, T> = tracing_subscriber::fmt::Layer< type StdErrJsonLayer<S, T> = fmt::Layer<
S, S,
JsonFields, JsonFields,
Format<tracing_subscriber::fmt::format::Json, T>, Format<fmt::format::Json, T>,
fn() -> std::io::Stderr, fn() -> std::io::Stderr,
>; >;