From 615e70fdad61354e9148977f6650f599a017944c Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 4 May 2023 18:27:44 -0700 Subject: [PATCH] Log config provenance in debug. This helps identify configuration issues by printing the source of every configuration value used by Rocket. --- core/lib/src/config/config.rs | 62 ++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/core/lib/src/config/config.rs b/core/lib/src/config/config.rs index 3b721c1e..6fbd6e7e 100644 --- a/core/lib/src/config/config.rs +++ b/core/lib/src/config/config.rs @@ -6,6 +6,7 @@ use figment::value::{Map, Dict, magic::RelativePathBuf}; use serde::{Deserialize, Serialize}; use yansi::Paint; +use crate::log::PaintExt; use crate::config::{LogLevel, Shutdown, Ident}; use crate::request::{self, Request, FromRequest}; use crate::http::uncased::Uncased; @@ -376,13 +377,31 @@ impl Config { }) } - pub(crate) fn pretty_print(&self, figment: &Figment) { - use crate::log::PaintExt; + #[inline] + pub(crate) fn trace_print(&self, figment: &Figment) { + if self.log_level != LogLevel::Debug { + return; + } + trace!("-- configuration trace information --"); + for param in Self::PARAMETERS { + if let Some(meta) = figment.find_metadata(param) { + let (param, name) = (Paint::blue(param), Paint::white(&meta.name)); + if let Some(ref source) = meta.source { + trace_!("{:?} parameter source: {} ({})", param, name, source); + } else { + trace_!("{:?} parameter source: {}", param, name); + } + } + } + } + + pub(crate) fn pretty_print(&self, figment: &Figment) { fn bold(val: T) -> Paint { Paint::default(val).bold() } + self.trace_print(figment); launch_meta!("{}Configured for {}.", Paint::emoji("🔧 "), self.profile); launch_meta_!("address: {}", bold(&self.address)); launch_meta_!("port: {}", bold(&self.port)); @@ -410,15 +429,6 @@ impl Config { (false, _) => launch_meta_!("tls: {}", bold("disabled")), } - #[cfg(feature = "secrets")] { - launch_meta_!("secret key: {}", bold(&self.secret_key)); - if !self.secret_key.is_provided() { - warn!("secrets enabled without a stable `secret_key`"); - launch_meta_!("disable `secrets` feature or configure a `secret_key`"); - launch_meta_!("this becomes an {} in non-debug profiles", Paint::red("error")); - } - } - launch_meta_!("shutdown: {}", bold(&self.shutdown)); launch_meta_!("log level: {}", bold(self.log_level)); launch_meta_!("cli colors: {}", bold(&self.cli_colors)); @@ -451,6 +461,15 @@ impl Config { } } } + + #[cfg(feature = "secrets")] { + launch_meta_!("secret key: {}", bold(&self.secret_key)); + if !self.secret_key.is_provided() { + warn!("secrets enabled without a stable `secret_key`"); + launch_meta_!("disable `secrets` feature or configure a `secret_key`"); + launch_meta_!("this becomes an {} in non-debug profiles", Paint::red("error")); + } + } } } @@ -493,6 +512,12 @@ impl Config { /// The stringy parameter name for setting/extracting [`Config::keep_alive`]. pub const KEEP_ALIVE: &'static str = "keep_alive"; + /// The stringy parameter name for setting/extracting [`Config::ident`]. + pub const IDENT: &'static str = "ident"; + + /// The stringy parameter name for setting/extracting [`Config::ip_header`]. + pub const IP_HEADER: &'static str = "ip_header"; + /// The stringy parameter name for setting/extracting [`Config::limits`]. pub const LIMITS: &'static str = "limits"; @@ -513,11 +538,24 @@ impl Config { /// The stringy parameter name for setting/extracting [`Config::cli_colors`]. pub const CLI_COLORS: &'static str = "cli_colors"; + + /// An array of all of the stringy parameter names. + pub const PARAMETERS: &'static [&'static str] = &[ + Self::ADDRESS, Self::PORT, Self::WORKERS, Self::MAX_BLOCKING, + Self::KEEP_ALIVE, Self::IDENT, Self::IP_HEADER, Self::LIMITS, Self::TLS, + Self::SECRET_KEY, Self::TEMP_DIR, Self::LOG_LEVEL, Self::SHUTDOWN, + Self::CLI_COLORS, + ]; } impl Provider for Config { + #[track_caller] fn metadata(&self) -> Metadata { - Metadata::named("Rocket Config") + if self == &Config::default() { + Metadata::named("rocket::Config::default()") + } else { + Metadata::named("rocket::Config") + } } #[track_caller]