Use 'debug_struct' to improve 'Config' 'Debug' impl.

Using the method `debug_struct()` of `fmt::Formatter` takes care of
the exact formatting for us. Additionally, it also handles the
"alternate" form of printing enabled with '#'. In the struct case it
prints the struct on multiple lines instead of just one.

This commit also changes the output slightly. Before, the field
`log_level` was printed with `log: {}`. This commit replaces "log"
with "log_level". Additionally, the value of `environment` is now
printed as a struct field instead of being combined with the struct
name.
This commit is contained in:
Lukas Kalbertodt 2017-09-04 00:57:52 +02:00 committed by Sergio Benitez
parent 303e23f720
commit a6c5a7e31c
1 changed files with 8 additions and 5 deletions

View File

@ -791,15 +791,18 @@ impl Config {
impl fmt::Debug for Config {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Config[{}] {{ address: {}, port: {}, workers: {}, log: {:?}",
self.environment, self.address, self.port, self.workers,
self.log_level)?;
let mut s = f.debug_struct("Config");
s.field("environment", &self.environment);
s.field("address", &self.address);
s.field("port", &self.port);
s.field("workers", &self.workers);
s.field("log_level", &self.log_level);
for (key, value) in self.extras() {
write!(f, ", {}: {}", key, value)?;
s.field(key, &value);
}
write!(f, " }}")
s.finish()
}
}