mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-30 12:47:05 +00:00
Add log-format option to CLI (#744)
* Add log-format CLI option * Cargo fmt * Add log format logic for file logging. Add doc * Review comment * Fix compilation errors * Remove Mutex from logger
This commit is contained in:
@@ -15,7 +15,6 @@ use std::cell::RefCell;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::{rename as FsRename, OpenOptions};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tokio::runtime::{Builder as RuntimeBuilder, Runtime, TaskExecutor};
|
||||
use types::{EthSpec, InteropEthSpec, MainnetEthSpec, MinimalEthSpec};
|
||||
@@ -99,12 +98,27 @@ impl<E: EthSpec> EnvironmentBuilder<E> {
|
||||
/// The logger is "async" because it has a dedicated thread that accepts logs and then
|
||||
/// asynchronously flushes them to stdout/files/etc. This means the thread that raised the log
|
||||
/// does not have to wait for the logs to be flushed.
|
||||
pub fn async_logger(mut self, debug_level: &str) -> Result<Self, String> {
|
||||
// Build the initial logger.
|
||||
let decorator = slog_term::TermDecorator::new().build();
|
||||
let decorator = logging::AlignedTermDecorator::new(decorator, logging::MAX_MESSAGE_WIDTH);
|
||||
let drain = slog_term::FullFormat::new(decorator).build().fuse();
|
||||
let drain = slog_async::Async::new(drain).build();
|
||||
pub fn async_logger(
|
||||
mut self,
|
||||
debug_level: &str,
|
||||
log_format: Option<&str>,
|
||||
) -> Result<Self, String> {
|
||||
// Setting up the initial logger format and building it.
|
||||
let drain = if let Some(format) = log_format {
|
||||
match format.to_uppercase().as_str() {
|
||||
"JSON" => {
|
||||
let drain = slog_json::Json::default(std::io::stdout()).fuse();
|
||||
slog_async::Async::new(drain).build()
|
||||
}
|
||||
_ => return Err("Logging format provided is not supported".to_string()),
|
||||
}
|
||||
} else {
|
||||
let decorator = slog_term::TermDecorator::new().build();
|
||||
let decorator =
|
||||
logging::AlignedTermDecorator::new(decorator, logging::MAX_MESSAGE_WIDTH);
|
||||
let drain = slog_term::FullFormat::new(decorator).build().fuse();
|
||||
slog_async::Async::new(drain).build()
|
||||
};
|
||||
|
||||
let drain = match debug_level {
|
||||
"info" => drain.filter_level(Level::Info),
|
||||
@@ -230,7 +244,12 @@ 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, debug_level: &str) -> Result<(), String> {
|
||||
pub fn log_to_json_file(
|
||||
&mut self,
|
||||
path: PathBuf,
|
||||
debug_level: &str,
|
||||
log_format: Option<&str>,
|
||||
) -> Result<(), String> {
|
||||
// Creating a backup if the logfile already exists.
|
||||
if path.exists() {
|
||||
let start = SystemTime::now();
|
||||
@@ -256,8 +275,14 @@ impl<E: EthSpec> Environment<E> {
|
||||
.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();
|
||||
let log_format = log_format.unwrap_or("JSON");
|
||||
let drain = match log_format.to_uppercase().as_str() {
|
||||
"JSON" => {
|
||||
let drain = slog_json::Json::default(file).fuse();
|
||||
slog_async::Async::new(drain).build()
|
||||
}
|
||||
_ => return Err("Logging format provided is not supported".to_string()),
|
||||
};
|
||||
|
||||
let drain = match debug_level {
|
||||
"info" => drain.filter_level(Level::Info),
|
||||
|
||||
Reference in New Issue
Block a user