Skip to content

Commit

Permalink
Log config provenance in debug.
Browse files Browse the repository at this point in the history
This helps identify configuration issues by printing the source of every
configuration value used by Rocket.
  • Loading branch information
SergioBenitez committed May 5, 2023
1 parent db53581 commit 615e70f
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions core/lib/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T: std::fmt::Display>(val: T) -> Paint<T> {
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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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"));
}
}
}
}

Expand Down Expand Up @@ -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";

Expand All @@ -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]
Expand Down

0 comments on commit 615e70f

Please sign in to comment.