Skip to content

Commit

Permalink
Allow override of RUST_LOG (solana-labs#7705)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay authored Jan 8, 2020
1 parent 2f5f8e7 commit 07855e3
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bench-tps/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{collections::HashMap, fs::File, io::prelude::*, path::Path, process::e
pub const NUM_SIGNATURES_FOR_TXS: u64 = 100_000 * 60 * 60 * 24 * 7;

fn main() {
solana_logger::setup_with_filter("solana=info");
solana_logger::setup_with_default("solana=info");
solana_metrics::set_panic_hook("bench-tps");

let matches = cli::build_args(solana_clap_utils::version!()).get_matches();
Expand Down
2 changes: 1 addition & 1 deletion core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ impl RpcSol for RpcSolImpl {
}

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

Expand Down
2 changes: 1 addition & 1 deletion faucet/src/bin/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
};

fn main() -> Result<(), Box<dyn error::Error>> {
solana_logger::setup_with_filter("solana=info");
solana_logger::setup_with_default("solana=info");
solana_metrics::set_panic_hook("faucet");
let matches = App::new(crate_name!())
.about(crate_description!())
Expand Down
2 changes: 1 addition & 1 deletion gossip/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::process::exit;

fn main() -> Result<(), Box<dyn error::Error>> {
solana_logger::setup_with_filter("solana=info");
solana_logger::setup_with_default("solana=info");

let matches = App::new(crate_name!())
.about(crate_description!())
Expand Down
2 changes: 1 addition & 1 deletion ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ fn open_database(ledger_path: &Path) -> Database {
#[allow(clippy::cognitive_complexity)]
fn main() {
const DEFAULT_ROOT_COUNT: &str = "1";
solana_logger::setup_with_filter("solana=info");
solana_logger::setup_with_default("solana=info");

let starting_slot_arg = Arg::with_name("starting_slot")
.long("starting-slot")
Expand Down
30 changes: 22 additions & 8 deletions logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,34 @@ impl log::Log for LoggerShim {
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) {
let logger = env_logger::Builder::from_env(env_logger::Env::new().default_filter_or(filter))
.format_timestamp_nanos()
.build();
fn replace_logger(logger: env_logger::Logger) {
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")
// Configures logging with a specific filter overriding RUST_LOG. _RUST_LOG is used instead
// so if set it takes precedence.
// May be called at any time to re-configure the log filter
pub fn setup_with(filter: &str) {
let logger =
env_logger::Builder::from_env(env_logger::Env::new().filter_or("_RUST_LOG", filter))
.format_timestamp_nanos()
.build();
replace_logger(logger);
}

// Configures logging with a default filter if RUST_LOG is not set
pub fn setup_with_default(filter: &str) {
let logger = env_logger::Builder::from_env(env_logger::Env::new().default_filter_or(filter))
.format_timestamp_nanos()
.build();
replace_logger(logger);
}

// Configures logging with the default filter "error" if RUST_LOG is not set
pub fn setup() {
setup_with_filter("error");
setup_with_default("error");
}
2 changes: 1 addition & 1 deletion programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn process_instruction(
keyed_accounts: &mut [KeyedAccount],
ix_data: &[u8],
) -> Result<(), InstructionError> {
solana_logger::setup();
solana_logger::setup_with_default("solana=info");

if let Ok(instruction) = limited_deserialize(ix_data) {
match instruction {
Expand Down
2 changes: 1 addition & 1 deletion programs/vote/src/vote_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub fn process_instruction(
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
) -> Result<(), InstructionError> {
solana_logger::setup_with_filter("solana=info");
solana_logger::setup_with_default("solana=info");

trace!("process_instruction: {:?}", data);
trace!("keyed_accounts: {:?}", keyed_accounts);
Expand Down
2 changes: 1 addition & 1 deletion validator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ pub fn main() {
}
};

solana_logger::setup_with_filter(
solana_logger::setup_with_default(
&[
"solana=info", /* info logging for all solana modules */
"rpc=trace", /* json_rpc request/response logging */
Expand Down
2 changes: 1 addition & 1 deletion watchtower/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
let json_rpc_url = value_t_or_exit!(matches, "json_rpc_url", String);
let validator_identity = pubkey_of(&matches, "validator_identity").map(|i| i.to_string());

solana_logger::setup_with_filter("solana=info");
solana_logger::setup_with_default("solana=info");
solana_metrics::set_panic_hook("watchtower");

let rpc_client = RpcClient::new(json_rpc_url);
Expand Down

0 comments on commit 07855e3

Please sign in to comment.