Skip to content

Commit

Permalink
segregate the log level changing logic into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj authored and FlorentinDUBOIS committed Jul 13, 2022
1 parent fdcbacc commit adf2d3a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
6 changes: 6 additions & 0 deletions bin/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub enum Success {
ListFrontends(CommandResponseData), // the list of frontends
ListWorkers(CommandResponseData),
LoadState(String, usize, usize), // state path, oks, errors
Logging(String), // new logging level
MasterStop,
// this should contain CommandResponseData but the logic does not return anything
// is this logic gone into sozu_command_lib::proxy::Query::Metrics(_) ?
Expand Down Expand Up @@ -148,6 +149,11 @@ impl std::fmt::Display for Success {
"Successfully loaded state from path {}, {} ok messages, {} errors",
path, ok, error
),
Self::Logging(logging_filter) => write!(
f,
"Successfully set the logging level to {}",
logging_filter
),
Self::MasterStop => write!(f, "stopping main process"),
// Self::Metrics => write!(f, "Successfully fetched the metrics"),
Self::NotifiedClient(id) => {
Expand Down
29 changes: 18 additions & 11 deletions bin/src/command/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ impl CommandServer {
CommandRequestData::Proxy(proxy_request) => match proxy_request {
ProxyRequestData::Metrics(config) => self.metrics(request_identifier, config).await,
ProxyRequestData::Query(query) => self.query(request_identifier, query).await,
ProxyRequestData::Logging(logging_filter) => self.set_logging_level(logging_filter),
// we should have something like
// ProxyRequestData::SoftStop => self.do_something(),
// ProxyRequestData::HardStop => self.do_nothing_and_return_early(),
// but it goes in there instead:
order => {
self.worker_order(request_identifier, order, request.worker_id)
.await
Expand Down Expand Up @@ -1032,6 +1037,19 @@ impl CommandServer {
Ok(None)
}

pub fn set_logging_level(&mut self, logging_filter: String) -> anyhow::Result<Option<Success>> {
debug!("Changing main process log level to {}", logging_filter);
logging::LOGGER.with(|l| {
let directives = logging::parse_logging_spec(&logging_filter);
l.borrow_mut().set_directives(directives);
});
// also change / set the content of RUST_LOG so future workers / main thread
// will have the new logging filter value
::std::env::set_var("RUST_LOG", logging_filter.to_owned());
debug!("Logging level now: {}", ::std::env::var("RUST_LOG")?);
Ok(Some(Success::Logging(logging_filter)))
}

pub async fn worker_order(
&mut self,
request_identifier: RequestIdentifier,
Expand All @@ -1044,17 +1062,6 @@ impl CommandServer {
debug!("workerconfig client order {:?}", order);
}

if let &ProxyRequestData::Logging(ref logging_filter) = &order {
debug!("Changing main process log level to {}", logging_filter);
logging::LOGGER.with(|l| {
let directives = logging::parse_logging_spec(&logging_filter);
l.borrow_mut().set_directives(directives);
});
// also change / set the content of RUST_LOG so future workers / main thread
// will have the new logging filter value
::std::env::set_var("RUST_LOG", logging_filter);
}

if !self.state.handle_order(&order) {
// Check if the backend or frontend exist before deleting it
if worker_id.is_none() {
Expand Down

0 comments on commit adf2d3a

Please sign in to comment.