Skip to content

Commit

Permalink
Validator log filter may now be reconfigured at runtime (solana-labs#…
Browse files Browse the repository at this point in the history
…5473)

* Log filter may now be reconfigured at runtime

* Add RPC API and bash script to reconfigure the log filter
  • Loading branch information
mvines authored Aug 11, 2019
1 parent 799d3b1 commit 54f4d13
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ pub trait RpcSol {

#[rpc(meta, name = "getVersion")]
fn get_version(&self, _: Self::Metadata) -> Result<RpcVersionInfo>;

#[rpc(meta, name = "setLogFilter")]
fn set_log_filter(&self, _: Self::Metadata, _: String) -> Result<()>;
}

pub struct RpcSolImpl;
Expand Down Expand Up @@ -619,6 +622,11 @@ impl RpcSol for RpcSolImpl {
solana_core: VERSION.to_string(),
})
}

fn set_log_filter(&self, _: Self::Metadata, filter: String) -> Result<()> {
solana_logger::setup_with_filter(&filter);
Ok(())
}
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ edition = "2018"

[dependencies]
env_logger = "0.6.2"
lazy_static = "1.3.0"
log = "0.4.8"

[lib]
name = "solana_logger"
42 changes: 32 additions & 10 deletions logger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
//! The `logger` module provides a setup function for `env_logger`. Its only function,
//! `setup()` may be called multiple times.
//! The `logger` module configures `env_logger`
use env_logger;
use std::sync::Once;
use lazy_static::lazy_static;
use std::sync::{Arc, RwLock};

static INIT: Once = Once::new();
lazy_static! {
static ref LOGGER: Arc<RwLock<env_logger::Logger>> =
{ Arc::new(RwLock::new(env_logger::Logger::from_default_env())) };
}

struct LoggerShim {}

impl log::Log for LoggerShim {
fn enabled(&self, metadata: &log::Metadata) -> bool {
LOGGER.read().unwrap().enabled(metadata)
}

fn log(&self, record: &log::Record) {
LOGGER.read().unwrap().log(record);
}

fn flush(&self) {}
}

// Configures logging with a specific filter.
// May be called at any time to re-configure the log filter
pub fn setup_with_filter(filter: &str) {
INIT.call_once(|| {
env_logger::Builder::from_env(env_logger::Env::new().default_filter_or(filter))
.default_format_timestamp_nanos(true)
.init();
});
let logger = env_logger::Builder::from_env(env_logger::Env::new().default_filter_or(filter))
.default_format_timestamp_nanos(true)
.build();
let max_level = logger.filter();
log::set_max_level(max_level);
let mut rw = LOGGER.write().unwrap();
std::mem::replace(&mut *rw, logger);
let _ = log::set_boxed_logger(Box::new(LoggerShim {}));
}

// Configures logging with the default filter ("error")
pub fn setup() {
setup_with_filter("error");
}
27 changes: 27 additions & 0 deletions scripts/set-log-filter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
#
# Reconfigures the log filter on a validator using the current RUST_LOG value
#

if [[ -n $1 ]]; then
url=$1
else
# Default to the local node
url=http://127.0.0.1:8899
fi

if [[ -z $RUST_LOG ]]; then
echo "RUST_LOG not defined"
exit 1
fi

set -x
exec curl $url -X POST -H "Content-Type: application/json" \
-d "
{
\"jsonrpc\": \"2.0\",
\"id\": 1,
\"method\": \"setLogFilter\",
\"params\": [\"$RUST_LOG\"]
}
"

0 comments on commit 54f4d13

Please sign in to comment.