feat(notary): Make logging format configurable (#719)

* Make logging format configurable

* Document logging format

* Fix formatting

* Init server config with default value
s in notary interation tests
This commit is contained in:
Leonid Logvinov
2025-03-19 18:57:00 +01:00
committed by GitHub
parent 61ce838f8c
commit c1b3d64d5d
5 changed files with 35 additions and 14 deletions

View File

@@ -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 }

View File

@@ -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

View File

@@ -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<String>,
/// 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,
}

View File

@@ -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<S>(format: LogFormat) -> Box<dyn Layer<S> + 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(())

View File

@@ -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,