pub mod socket;
use anyhow::Result;
use tracing::{Level, Subscriber};
use tracing_appender::non_blocking::NonBlocking;
use tracing_subscriber::Layer;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::writer::MakeWriterExt;
use crate::cli::LogFormat;
use crate::cli::validator::parser::tracing::CustomFilter;
pub fn file<S>(
filter: CustomFilter,
file: NonBlocking,
format: LogFormat,
) -> Result<Box<dyn Layer<S> + Send + Sync>>
where
S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a> + Send + Sync,
{
let writer = file.with_max_level(Level::TRACE);
let layer = tracing_subscriber::fmt::layer();
match format {
LogFormat::Json => Ok(layer
.json()
.with_ansi(false)
.with_file(false)
.with_target(true)
.with_line_number(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_span_events(FmtSpan::NONE)
.with_writer(writer)
.with_filter(filter.env())
.with_filter(filter.span_filter::<S>())
.boxed()),
LogFormat::Text => Ok(layer
.compact()
.with_ansi(false)
.with_file(false)
.with_target(true)
.with_line_number(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_span_events(FmtSpan::NONE)
.with_writer(writer)
.with_filter(filter.env())
.with_filter(filter.span_filter::<S>())
.boxed()),
}
}
pub fn output<S>(
filter: CustomFilter,
stdout: NonBlocking,
stderr: NonBlocking,
format: LogFormat,
) -> Result<Box<dyn Layer<S> + Send + Sync>>
where
S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a> + Send + Sync,
{
let writer = stderr.with_max_level(Level::WARN).or_else(stdout);
#[cfg(not(debug_assertions))]
{
let layer = tracing_subscriber::fmt::layer();
match format {
LogFormat::Json => Ok(layer
.json()
.with_ansi(true)
.with_file(false)
.with_target(true)
.with_line_number(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_span_events(FmtSpan::NONE)
.with_writer(writer)
.with_filter(filter.env())
.with_filter(filter.span_filter::<S>())
.boxed()),
LogFormat::Text => Ok(layer
.compact()
.with_ansi(true)
.with_file(false)
.with_target(true)
.with_line_number(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_span_events(FmtSpan::NONE)
.with_writer(writer)
.with_filter(filter.env())
.with_filter(filter.span_filter::<S>())
.boxed()),
}
}
#[cfg(debug_assertions)]
{
let layer = tracing_subscriber::fmt::layer();
match format {
LogFormat::Json => Ok(layer
.json()
.with_ansi(true)
.with_file(true)
.with_target(true)
.with_line_number(true)
.with_thread_ids(false)
.with_thread_names(false)
.with_span_events(FmtSpan::NONE)
.with_writer(writer)
.with_filter(filter.env())
.with_filter(filter.span_filter::<S>())
.boxed()),
LogFormat::Text => Ok(layer
.compact()
.with_ansi(true)
.with_file(true)
.with_target(true)
.with_line_number(true)
.with_thread_ids(false)
.with_thread_names(false)
.with_span_events(FmtSpan::NONE)
.with_writer(writer)
.with_filter(filter.env())
.with_filter(filter.span_filter::<S>())
.boxed()),
}
}
}