Merge #397
397: Always log at debug level to file r=rishflab a=rishflab WILL SQUASH DOWN TO 3 COMMITS WHEN APPROVED! Log at debug level to file EnvFilter is applied globally. This means you cannot log at INFO level to the terminal and at DEBUG level to log files. To get a around this limitation I had to implement the layer trait on a new type and filter in the on_event() trait method. Each swap has its own log file denoted by its swap_id. The logger appends to the existing file when resuming a swap. Closes #278 I think the `DebugTerminalPritner` and `InfoTerminalPrinter` could be consolidated with some effort with some generics wizardry. It works for now and I think it can be done later. I wish in general there was a cleaner way to do this. Co-authored-by: rishflab <rishflab@hotmail.com>pull/412/head
commit
2f19222948
@ -1,5 +1,6 @@
|
||||
pub mod command;
|
||||
pub mod config;
|
||||
mod rate;
|
||||
pub mod tracing;
|
||||
|
||||
pub use rate::Rate;
|
||||
|
@ -1 +1,2 @@
|
||||
pub mod command;
|
||||
pub mod tracing;
|
||||
|
@ -0,0 +1,81 @@
|
||||
use anyhow::Result;
|
||||
use std::path::Path;
|
||||
use tracing::subscriber::set_global_default;
|
||||
use tracing::{Event, Level, Subscriber};
|
||||
use tracing_subscriber::fmt::format::{DefaultFields, Format};
|
||||
use tracing_subscriber::fmt::time::ChronoLocal;
|
||||
use tracing_subscriber::layer::{Context, SubscriberExt};
|
||||
use tracing_subscriber::{fmt, EnvFilter, Layer, Registry};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn init(debug: bool, dir: impl AsRef<Path>, swap_id: Uuid) -> Result<()> {
|
||||
let level_filter = EnvFilter::try_new("swap=debug")?;
|
||||
|
||||
let registry = Registry::default().with(level_filter);
|
||||
|
||||
let appender = tracing_appender::rolling::never(dir, format!("swap-{}.log", swap_id));
|
||||
let (appender, guard) = tracing_appender::non_blocking(appender);
|
||||
|
||||
std::mem::forget(guard);
|
||||
|
||||
let file_logger = fmt::layer()
|
||||
.with_ansi(false)
|
||||
.with_target(false)
|
||||
.with_writer(appender);
|
||||
|
||||
if debug {
|
||||
set_global_default(registry.with(file_logger).with(debug_terminal_printer()))?;
|
||||
} else {
|
||||
set_global_default(registry.with(file_logger).with(info_terminal_printer()))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub struct StdErrPrinter<L> {
|
||||
inner: L,
|
||||
level: Level,
|
||||
}
|
||||
|
||||
type StdErrLayer<S, T> = tracing_subscriber::fmt::Layer<
|
||||
S,
|
||||
DefaultFields,
|
||||
Format<tracing_subscriber::fmt::format::Full, T>,
|
||||
fn() -> std::io::Stderr,
|
||||
>;
|
||||
|
||||
fn debug_terminal_printer<S>() -> StdErrPrinter<StdErrLayer<S, ChronoLocal>> {
|
||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
||||
StdErrPrinter {
|
||||
inner: fmt::layer()
|
||||
.with_ansi(is_terminal)
|
||||
.with_target(false)
|
||||
.with_timer(ChronoLocal::with_format("%F %T".to_owned()))
|
||||
.with_writer(std::io::stderr),
|
||||
level: Level::DEBUG,
|
||||
}
|
||||
}
|
||||
|
||||
fn info_terminal_printer<S>() -> StdErrPrinter<StdErrLayer<S, ()>> {
|
||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
||||
StdErrPrinter {
|
||||
inner: fmt::layer()
|
||||
.with_ansi(is_terminal)
|
||||
.with_target(false)
|
||||
.with_level(false)
|
||||
.without_time()
|
||||
.with_writer(std::io::stderr),
|
||||
level: Level::INFO,
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, S> Layer<S> for StdErrPrinter<L>
|
||||
where
|
||||
L: 'static + Layer<S>,
|
||||
S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
|
||||
{
|
||||
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
|
||||
if self.level.ge(event.metadata().level()) {
|
||||
self.inner.on_event(event, ctx);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue