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);
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 {

View file

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