Skip to content

Commit

Permalink
Unify logfile handling in environment crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Nov 15, 2019
1 parent c10fea5 commit 075704f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 89 deletions.
1 change: 0 additions & 1 deletion beacon_node/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ error-chain = "0.12.1"
serde_yaml = "0.8.11"
slog = { version = "2.5.2", features = ["max_level_trace"] }
slog-async = "2.3.0"
slog-json = "2.3.0"
tokio = "0.1.22"
clap = "2.33.0"
dirs = "2.0.2"
Expand Down
47 changes: 2 additions & 45 deletions beacon_node/client/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use clap::ArgMatches;
use network::NetworkConfig;
use serde_derive::{Deserialize, Serialize};
use slog::{info, o, Drain};
use std::fs::{self, OpenOptions};
use std::fs;
use std::path::PathBuf;
use std::sync::Mutex;

/// The number initial validators when starting the `Minimal`.
const TESTNET_SPEC_CONSTANTS: &str = "minimal";
Expand Down Expand Up @@ -95,47 +93,11 @@ impl Config {
Some(path)
}

// Update the logger to output in JSON to specified file
fn update_logger(&mut self, log: &mut slog::Logger) -> Result<(), &'static str> {
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&self.log_file);

if file.is_err() {
return Err("Cannot open log file");
}
let file = file.unwrap();

if let Some(file) = self.log_file.to_str() {
info!(
*log,
"Log file specified, output will now be written to {} in json.", file
);
} else {
info!(
*log,
"Log file specified output will now be written in json"
);
}

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

Ok(())
}

/// Apply the following arguments to `self`, replacing values if they are specified in `args`.
///
/// Returns an error if arguments are obviously invalid. May succeed even if some values are
/// invalid.
pub fn apply_cli_args(
&mut self,
args: &ArgMatches,
log: &mut slog::Logger,
) -> Result<(), String> {
pub fn apply_cli_args(&mut self, args: &ArgMatches, _log: &slog::Logger) -> Result<(), String> {
if let Some(dir) = args.value_of("datadir") {
self.data_dir = PathBuf::from(dir);
};
Expand All @@ -149,11 +111,6 @@ impl Config {
self.rest_api.apply_cli_args(args)?;
self.websocket_server.apply_cli_args(args)?;

if let Some(log_file) = args.value_of("logfile") {
self.log_file = PathBuf::from(log_file);
self.update_logger(log)?;
};

Ok(())
}
}
Expand Down
1 change: 1 addition & 0 deletions lighthouse/environment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ slog-async = "^2.3.0"
ctrlc = { version = "3.1.1", features = ["termination"] }
futures = "0.1.25"
parking_lot = "0.7"
slog-json = "2.3.0"
27 changes: 26 additions & 1 deletion lighthouse/environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

use eth2_config::Eth2Config;
use futures::{sync::oneshot, Future};
use slog::{o, Drain, Level, Logger};
use slog::{info, o, Drain, Level, Logger};
use sloggers::{null::NullLoggerBuilder, Build};
use std::cell::RefCell;
use std::fs::OpenOptions;
use std::path::PathBuf;
use std::sync::Mutex;
use tokio::runtime::{Builder as RuntimeBuilder, Runtime, TaskExecutor};
use types::{EthSpec, InteropEthSpec, MainnetEthSpec, MinimalEthSpec};

Expand Down Expand Up @@ -224,6 +227,28 @@ impl<E: EthSpec> Environment<E> {
.map_err(|e| format!("Tokio runtime shutdown returned an error: {:?}", 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> {
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&path)
.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!());

info!(
self.log,
"Logging to JSON file";
"path" => format!("{:?}", path)
);

Ok(())
}

pub fn eth_spec_instance(&self) -> &E {
&self.eth_spec_instance
}
Expand Down
8 changes: 8 additions & 0 deletions lighthouse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::{App, Arg, ArgMatches};
use env_logger::{Builder, Env};
use environment::EnvironmentBuilder;
use slog::{crit, info, warn};
use std::path::PathBuf;
use std::process::exit;
use types::EthSpec;
use validator_client::ProductionValidatorClient;
Expand Down Expand Up @@ -94,6 +95,13 @@ fn run<E: EthSpec>(

let log = environment.core_context().log;

if let Some(log_path) = matches.value_of("logfile") {
let path = log_path
.parse::<PathBuf>()
.map_err(|e| format!("Failed to parse log path: {:?}", e))?;
environment.log_to_json_file(path)?;
}

if std::mem::size_of::<usize>() != 8 {
crit!(
log,
Expand Down
1 change: 0 additions & 1 deletion validator_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ serde_derive = "1.0.102"
serde_json = "1.0.41"
slog = { version = "2.5.2", features = ["max_level_trace", "release_max_level_trace"] }
slog-async = "2.3.0"
slog-json = "2.3.0"
slog-term = "2.4.2"
tokio = "0.1.22"
tokio-timer = "0.2.11"
Expand Down
44 changes: 3 additions & 41 deletions validator_client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use bincode;
use bls::Keypair;
use clap::ArgMatches;
use serde_derive::{Deserialize, Serialize};
use slog::{error, info, o, warn, Drain};
use std::fs::{self, File, OpenOptions};
use slog::{error, warn};
use std::fs::{self, File};
use std::io::{Error, ErrorKind};
use std::ops::Range;
use std::path::PathBuf;
use std::sync::Mutex;
use types::{
test_utils::{generate_deterministic_keypair, load_keypairs_from_yaml},
EthSpec, MainnetEthSpec,
Expand Down Expand Up @@ -94,56 +93,19 @@ impl Config {
pub fn apply_cli_args(
&mut self,
args: &ArgMatches,
log: &mut slog::Logger,
_log: &slog::Logger,
) -> Result<(), &'static str> {
if let Some(datadir) = args.value_of("datadir") {
self.data_dir = PathBuf::from(datadir);
};

if let Some(log_file) = args.value_of("logfile") {
self.log_file = PathBuf::from(log_file);
self.update_logger(log)?;
};

if let Some(srv) = args.value_of("server") {
self.server = srv.to_string();
};

Ok(())
}

// Update the logger to output in JSON to specified file
fn update_logger(&mut self, log: &mut slog::Logger) -> Result<(), &'static str> {
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&self.log_file);

if file.is_err() {
return Err("Cannot open log file");
}
let file = file.unwrap();

if let Some(file) = self.log_file.to_str() {
info!(
*log,
"Log file specified, output will now be written to {} in json.", file
);
} else {
info!(
*log,
"Log file specified output will now be written in json"
);
}

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

Ok(())
}

/// Reads a single keypair from the given `path`.
///
/// `path` should be the path to a directory containing a private key. The file name of `path`
Expand Down

0 comments on commit 075704f

Please sign in to comment.