Skip to content

Commit

Permalink
Add logging level when using logfile (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott authored and paulhauner committed Dec 13, 2019
1 parent 5e7803f commit b1d4284
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 14 additions & 3 deletions lighthouse/environment/src/lib.rs
Expand Up @@ -228,7 +228,7 @@ impl<E: EthSpec> Environment<E> {
}

/// Sets the logger (and all child loggers) to log to a file.
pub fn log_to_json_file(&mut self, path: PathBuf) -> Result<(), String> {
pub fn log_to_json_file(&mut self, path: PathBuf, debug_level: &str) -> Result<(), String> {
let file = OpenOptions::new()
.create(true)
.write(true)
Expand All @@ -237,8 +237,19 @@ impl<E: EthSpec> Environment<E> {
.map_err(|e| format!("Unable to open logfile: {:?}", e))?;

let drain = Mutex::new(slog_json::Json::default(file)).fuse();
let drain = slog_async::Async::new(drain).build().fuse();
self.log = slog::Logger::root(drain, o!());
let drain = slog_async::Async::new(drain).build();

let drain = match debug_level {
"info" => drain.filter_level(Level::Info),
"debug" => drain.filter_level(Level::Debug),
"trace" => drain.filter_level(Level::Trace),
"warn" => drain.filter_level(Level::Warning),
"error" => drain.filter_level(Level::Error),
"crit" => drain.filter_level(Level::Critical),
unknown => return Err(format!("Unknown debug-level: {}", unknown)),
};

self.log = Logger::root(drain.fuse(), o!());

info!(
self.log,
Expand Down
12 changes: 6 additions & 6 deletions lighthouse/src/main.rs
Expand Up @@ -95,12 +95,12 @@ fn run<E: EthSpec>(
environment_builder: EnvironmentBuilder<E>,
matches: &ArgMatches,
) -> Result<(), String> {
let debug_level = matches
.value_of("debug-level")
.ok_or_else(|| "Expected --debug-level flag".to_string())?;

let mut environment = environment_builder
.async_logger(
matches
.value_of("debug-level")
.ok_or_else(|| "Expected --debug-level flag".to_string())?,
)?
.async_logger(debug_level)?
.multi_threaded_tokio_runtime()?
.build()?;

Expand All @@ -110,7 +110,7 @@ fn run<E: EthSpec>(
let path = log_path
.parse::<PathBuf>()
.map_err(|e| format!("Failed to parse log path: {:?}", e))?;
environment.log_to_json_file(path)?;
environment.log_to_json_file(path, debug_level)?;
}

if std::mem::size_of::<usize>() != 8 {
Expand Down

0 comments on commit b1d4284

Please sign in to comment.