1
0
Fork 0
forked from mirror/grapevine

config options for log format and color usage

We want to be able to disable colors for complement logs (since they
are likely to be opened in a text editor). There's no pressing need for
alternative log formats, but I'm interested in whether the 'pretty'
format will be easier for debugging.

I chose to add 'log_*' options rather than making a separate 'log'
section for now. There's been some discussion about trying to separate
the tracing/logging stuff into more structured sections, but that can
happen later.
This commit is contained in:
Benjamin Lee 2024-06-13 19:01:37 -07:00
parent 4f041f9153
commit a909e2079b
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4
4 changed files with 46 additions and 4 deletions

13
Cargo.lock generated
View file

@ -3214,6 +3214,16 @@ dependencies = [
"web-time",
]
[[package]]
name = "tracing-serde"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1"
dependencies = [
"serde",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.18"
@ -3224,12 +3234,15 @@ dependencies = [
"nu-ansi-term",
"once_cell",
"regex",
"serde",
"serde_json",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
"tracing-serde",
]
[[package]]

View file

@ -139,7 +139,7 @@ tower-http = { version = "0.5.2", features = ["add-extension", "cors", "sensitiv
tracing = { version = "0.1.40", features = [] }
tracing-flame = "0.2.0"
tracing-opentelemetry = "0.24.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
trust-dns-resolver = "0.23.2"
xdg = "2.5.2"

View file

@ -77,6 +77,10 @@ pub(crate) struct Config {
pub(crate) trusted_servers: Vec<OwnedServerName>,
#[serde(default = "default_log")]
pub(crate) log: EnvFilterClone,
#[serde(default = "true_fn")]
pub(crate) log_colors: bool,
#[serde(default)]
pub(crate) log_format: LogFormat,
#[serde(default)]
pub(crate) turn_username: String,
#[serde(default)]
@ -110,6 +114,20 @@ pub(crate) enum ListenConfig {
},
}
#[derive(Copy, Clone, Default, Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum LogFormat {
/// Use the [`tracing_subscriber::fmt::format::Pretty`] formatter
Pretty,
/// Use the [`tracing_subscriber::fmt::format::Full`] formatter
#[default]
Full,
/// Use the [`tracing_subscriber::fmt::format::Compact`] formatter
Compact,
/// Use the [`tracing_subscriber::fmt::format::Json`] formatter
Json,
}
impl Display for ListenConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {

View file

@ -23,7 +23,11 @@ use tokio::time::Instant;
use tracing_flame::{FlameLayer, FlushGuard};
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Layer, Registry};
use crate::{config::Config, error, utils::error::Result};
use crate::{
config::{Config, LogFormat},
error,
utils::error::Result,
};
/// Globally accessible metrics state
pub(crate) static METRICS: Lazy<Metrics> = Lazy::new(Metrics::new);
@ -119,8 +123,15 @@ pub(crate) fn init(config: &Config) -> Result<Guard, error::Observability> {
.transpose()?
.unzip();
let fmt_layer = tracing_subscriber::fmt::Layer::new()
.with_filter(EnvFilter::from(&config.log));
let fmt_layer =
tracing_subscriber::fmt::Layer::new().with_ansi(config.log_colors);
let fmt_layer = match config.log_format {
LogFormat::Pretty => fmt_layer.pretty().boxed(),
LogFormat::Full => fmt_layer.boxed(),
LogFormat::Compact => fmt_layer.compact().boxed(),
LogFormat::Json => fmt_layer.json().boxed(),
};
let fmt_layer = fmt_layer.with_filter(EnvFilter::from(&config.log));
let subscriber = Registry::default()
.with(jaeger_layer)