diff --git a/crates/notary/server/Cargo.toml b/crates/notary/server/Cargo.toml index 08e81e8ea..542b5d607 100644 --- a/crates/notary/server/Cargo.toml +++ b/crates/notary/server/Cargo.toml @@ -54,7 +54,7 @@ tower-http = { version = "0.5", features = ["cors"] } tower-service = { version = "0.3" } tower-util = { version = "0.3.1" } tracing = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter"] } +tracing-subscriber = { workspace = true, features = ["env-filter", "json"] } uuid = { workspace = true, features = ["v4", "fast-rng"] } ws_stream_tungstenite = { workspace = true, features = ["tokio_io"] } zeroize = { workspace = true } diff --git a/crates/notary/server/README.md b/crates/notary/server/README.md index 3ede09eb4..7afa1aa41 100644 --- a/crates/notary/server/README.md +++ b/crates/notary/server/README.md @@ -119,7 +119,9 @@ The default logging strategy of this server is set to `DEBUG` verbosity level fo In the config [file](./config/config.yaml), one can toggle the verbosity level for these crates using the `level` field under `logging`. Alternatively, use the CLI argument `--log-level` (see [this](#configuration)). -One can also provide a custom filtering logic by adding a `filter` field under `logging` in the config file above, and use a value that follows the tracing crate's [filter directive syntax](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#example-syntax). +One can also provide a custom filtering logic by adding a `filter` field under `logging` in the config file above, and use a value that follows the tracing crate's [filter directive syntax](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#example-syntax). + +Logs can be printed in two formats. Compact and JSON. Compact is human-readable and is best suited for console. JSON is machine-readable and is used to send logs to log collection services. One can change log format by switching the `format` field under `logging`. Accepted values are `compact` and `json`. If the config key is not set - `compact` is used by default. --- ## Architecture diff --git a/crates/notary/server/src/config.rs b/crates/notary/server/src/config.rs index 176a6852f..e1413c13d 100644 --- a/crates/notary/server/src/config.rs +++ b/crates/notary/server/src/config.rs @@ -71,4 +71,16 @@ pub struct LoggingProperties { /// Custom filtering logic, refer to the syntax here https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#example-syntax /// This will override the default filtering logic above pub filter: Option, + /// Log format. Available options are "compact" and "json". Default is + /// "compact" + #[serde(default)] + pub format: LogFormat, +} + +#[derive(Clone, Copy, Debug, Deserialize, Default)] +#[serde(rename_all = "kebab-case")] +pub enum LogFormat { + #[default] + Compact, + Json, } diff --git a/crates/notary/server/src/server_tracing.rs b/crates/notary/server/src/server_tracing.rs index eb09132cd..f2b30b91d 100644 --- a/crates/notary/server/src/server_tracing.rs +++ b/crates/notary/server/src/server_tracing.rs @@ -1,9 +1,23 @@ use eyre::Result; use std::str::FromStr; -use tracing::Level; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; +use tracing::{Level, Subscriber}; +use tracing_subscriber::{ + fmt, layer::SubscriberExt, registry::LookupSpan, util::SubscriberInitExt, EnvFilter, Layer, + Registry, +}; -use crate::config::NotaryServerProperties; +use crate::config::{LogFormat, NotaryServerProperties}; + +fn format_layer(format: LogFormat) -> Box + Send + Sync> +where + S: Subscriber + for<'a> LookupSpan<'a>, +{ + let f = fmt::layer().with_thread_ids(true).with_thread_names(true); + match format { + LogFormat::Compact => f.compact().boxed(), + LogFormat::Json => f.json().boxed(), + } +} pub fn init_tracing(config: &NotaryServerProperties) -> Result<()> { // Retrieve log filtering logic from config @@ -18,16 +32,9 @@ pub fn init_tracing(config: &NotaryServerProperties) -> Result<()> { }; let filter_layer = EnvFilter::builder().parse(directives)?; - // Format the log - let format_layer = tracing_subscriber::fmt::layer() - // Use a more compact, abbreviated log format - .compact() - .with_thread_ids(true) - .with_thread_names(true); - Registry::default() .with(filter_layer) - .with(format_layer) + .with(format_layer(config.logging.format)) .try_init()?; Ok(()) diff --git a/crates/notary/tests-integration/tests/notary.rs b/crates/notary/tests-integration/tests/notary.rs index 199ecb19e..813c71682 100644 --- a/crates/notary/tests-integration/tests/notary.rs +++ b/crates/notary/tests-integration/tests/notary.rs @@ -61,7 +61,7 @@ fn get_server_config(port: u16, tls_enabled: bool, auth_enabled: bool) -> Notary }, logging: LoggingProperties { level: "DEBUG".to_string(), - filter: None, + ..Default::default() }, authorization: AuthorizationProperties { enabled: auth_enabled,