mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Initial testing for appending tracing logs to appropiate log files depending on swap_id span
This commit is contained in:
parent
63c1edbdd3
commit
c32bb6f624
@ -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 {
|
||||
|
@ -1,145 +1,99 @@
|
||||
use anyhow::Result;
|
||||
use std::fmt::Debug;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::option::Option::Some;
|
||||
use std::path::Path;
|
||||
use time::format_description::well_known::Rfc3339;
|
||||
use tracing::subscriber::set_global_default;
|
||||
use tracing::{Event, Level, Subscriber};
|
||||
use tracing_subscriber::fmt::format::{DefaultFields, Format, JsonFields};
|
||||
use std::path::{Path, PathBuf};
|
||||
use tracing::field::Field;
|
||||
use tracing::span::Attributes;
|
||||
use tracing::{Id, Level, Subscriber};
|
||||
use tracing_subscriber::fmt::format::{Format, Json, 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::util::SubscriberInitExt;
|
||||
use tracing_subscriber::{EnvFilter, FmtSubscriber, Layer, Registry};
|
||||
use tracing_subscriber::fmt::{format, FormatEvent, MakeWriter};
|
||||
|
||||
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")?;
|
||||
struct SwapIdVisitor {
|
||||
swap_id: Option<String>,
|
||||
}
|
||||
|
||||
let registry = Registry::default().with(level_filter);
|
||||
pub struct FileLayer {
|
||||
dir: PathBuf,
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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()))?;
|
||||
impl FileLayer {
|
||||
pub fn new(dir: impl AsRef<Path>) -> Self {
|
||||
Self {
|
||||
dir: dir.as_ref().to_path_buf(),
|
||||
}
|
||||
} 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);
|
||||
fn get_log_path(&self, swap_id: &str) -> PathBuf {
|
||||
self.dir.join(format!("swap-{}.log", swap_id))
|
||||
}
|
||||
|
||||
if json {
|
||||
builder.json().init();
|
||||
} else {
|
||||
builder.init();
|
||||
fn append_to_file(&self, swap_id: &str, message: &str) -> std::io::Result<()> {
|
||||
let path = self.get_log_path(swap_id);
|
||||
let mut file = OpenOptions::new().append(true).create(true).open(path)?;
|
||||
file.write_all(message.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<S> Layer<S> for FileLayer
|
||||
where
|
||||
S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
|
||||
{
|
||||
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
|
||||
let mut visitor = SwapIdVisitor { swap_id: None };
|
||||
attrs.record(&mut visitor);
|
||||
|
||||
if let Some(swap_id) = visitor.swap_id {
|
||||
if let Some(span) = ctx.span(id) {
|
||||
span.extensions_mut().insert(swap_id);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn on_event(&self, event: &tracing::Event<'_>, ctx: tracing_subscriber::layer::Context<'_, S>) {
|
||||
if let Some(current_span_id) = ctx.current_span().id() {
|
||||
if let Some(span) = ctx.span(current_span_id) {
|
||||
if let Some(swap_id) = span.extensions().get::<String>() {
|
||||
println!("swap_id: {}", swap_id);
|
||||
|
||||
// Here I need to figure out how to format the event in JSON just like the internal JSON formatter does
|
||||
self.append_to_file(swap_id, &format!("{}\n", event.metadata().fields())).expect("Failed to write to file");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl tracing::field::Visit for SwapIdVisitor {
|
||||
fn record_debug(&mut self, field: &Field, value: &dyn Debug) {
|
||||
if field.name() == "swap_id" {
|
||||
self.swap_id = Some(format!("{:?}", value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(debug: bool, json: bool, dir: impl AsRef<Path>) -> Result<()> {
|
||||
let level = if debug { Level::DEBUG } else { Level::INFO };
|
||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
||||
|
||||
let file_layer = FileLayer::new(dir.as_ref());
|
||||
|
||||
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)
|
||||
.finish()
|
||||
.with(file_layer)
|
||||
.init();
|
||||
|
||||
tracing::info!("Logging initialized to {}", dir.as_ref().display());
|
||||
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,
|
||||
>;
|
||||
|
||||
type StdErrJsonLayer<S, T> = tracing_subscriber::fmt::Layer<
|
||||
S,
|
||||
JsonFields,
|
||||
Format<tracing_subscriber::fmt::format::Json, T>,
|
||||
fn() -> std::io::Stderr,
|
||||
>;
|
||||
|
||||
fn debug_terminal_printer<S>() -> StdErrPrinter<StdErrLayer<S, UtcTime<Rfc3339>>> {
|
||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
||||
StdErrPrinter {
|
||||
inner: fmt::layer()
|
||||
.with_ansi(is_terminal)
|
||||
.with_target(false)
|
||||
.with_timer(UtcTime::rfc_3339())
|
||||
.with_writer(std::io::stderr),
|
||||
level: Level::DEBUG,
|
||||
}
|
||||
}
|
||||
|
||||
fn debug_json_terminal_printer<S>() -> StdErrPrinter<StdErrJsonLayer<S, UtcTime<Rfc3339>>> {
|
||||
let is_terminal = atty::is(atty::Stream::Stderr);
|
||||
StdErrPrinter {
|
||||
inner: fmt::layer()
|
||||
.with_ansi(is_terminal)
|
||||
.with_target(false)
|
||||
.with_timer(UtcTime::rfc_3339())
|
||||
.json()
|
||||
.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,
|
||||
}
|
||||
}
|
||||
|
||||
fn info_json_terminal_printer<S>() -> StdErrPrinter<StdErrJsonLayer<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()
|
||||
.json()
|
||||
.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
Block a user