Skip to content

Commit

Permalink
feat: Use Option<PathBuf> && write log to stdout by default
Browse files Browse the repository at this point in the history
Signed-off-by: GFX9 <[email protected]>
  • Loading branch information
GFX9 committed May 1, 2024
1 parent 4aa940d commit 602ea08
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
8 changes: 4 additions & 4 deletions crates/utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,9 @@ impl LogConfig {
/// Generate a new `LogConfig` object
#[must_use]
#[inline]
pub fn new(path: PathBuf, rotation: RotationConfig, level: LevelConfig) -> Self {
pub fn new(path: Option<PathBuf>, rotation: RotationConfig, level: LevelConfig) -> Self {
Self {
path: Some(path),
path: path,
rotation,
level,
}
Expand Down Expand Up @@ -1326,7 +1326,7 @@ mod tests {
assert_eq!(
config.log,
LogConfig::new(
PathBuf::from("/var/log/xline"),
Some(PathBuf::from("/var/log/xline")),
RotationConfig::Daily,
LevelConfig::INFO
)
Expand Down Expand Up @@ -1452,7 +1452,7 @@ mod tests {
assert_eq!(
config.log,
LogConfig::new(
PathBuf::from("/var/log/xline"),
Some(PathBuf::from("/var/log/xline")),
RotationConfig::Daily,
LevelConfig::INFO
)
Expand Down
21 changes: 20 additions & 1 deletion crates/utils/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, time::Duration};
use std::{collections::HashMap, path::PathBuf, time::Duration};

use clippy_utilities::OverflowArithmetic;
use thiserror::Error;
Expand Down Expand Up @@ -162,6 +162,25 @@ pub fn parse_state(s: &str) -> Result<InitialClusterState, ConfigParseError> {
}
}

/// Parse `LOG_PATH` from string
/// # Errors
/// Return error when parsing the given string to `PathBuf` failed
#[inline]
pub fn parse_log_file(s: &str) -> Result<Option<PathBuf>, ConfigParseError> {
if s.is_empty() {
return Ok(None);
}
let path = PathBuf::from(s);
if path.exists() {
Ok(Some(path))
} else {
Err(ConfigParseError::InvalidValue(format!(
"Log file path is illegal: {}",
s
)))
}
}

/// Parse `LevelConfig` from string
/// # Errors
/// Return error when parsing the given string to `LevelConfig` failed
Expand Down
8 changes: 3 additions & 5 deletions crates/xline/src/utils/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use utils::{
EngineConfig, InitialClusterState, LevelConfig, LogConfig, MetricsConfig,
MetricsPushProtocol, RotationConfig, ServerTimeout, StorageConfig, TlsConfig, TraceConfig,
XlineServerConfig,
},
parse_batch_bytes, parse_duration, parse_log_level, parse_members, parse_metrics_push_protocol,
parse_rotation, parse_state, ConfigFileError,
}, parse_batch_bytes, parse_duration, parse_log_file, parse_log_level, parse_members, parse_metrics_push_protocol, parse_rotation, parse_state, ConfigFileError
};

/// Xline server config path env name
Expand Down Expand Up @@ -92,8 +90,8 @@ pub struct ServerArgs {
#[clap(long, value_parser = parse_metrics_push_protocol, default_value_t = default_metrics_push_protocol())]
metrics_push_protocol: MetricsPushProtocol,
/// Log file path
#[clap(long, default_value = "/stdout")]
log_file: PathBuf,
#[clap(long, value_parser = parse_log_file, default_value = None)]
log_file: Option<PathBuf>,
/// Log rotate strategy, eg: never, hourly, daily
#[clap(long, value_parser = parse_rotation, default_value_t = default_rotation())]
log_rotate: RotationConfig,
Expand Down
5 changes: 2 additions & 3 deletions crates/xline/src/utils/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use utils::config::{default_rotation, file_appender, LogConfig, TraceConfig};
/// Return a Box trait from the config
fn generate_writer(name: &str, log_config: &LogConfig) -> Box<dyn std::io::Write + Send> {
match *log_config.path() {
Some(ref file_path) if file_path.to_string_lossy() == "/stdout" => {
Some(ref file_path) => Box::new(file_appender(*log_config.rotation(), file_path, name)),
None => {
if *log_config.rotation() != default_rotation() {
warn!("The log is output to the terminal, so the rotation parameter is ignored.");
}
Box::new(std::io::stdout())
}
Some(ref file_path) => Box::new(file_appender(*log_config.rotation(), file_path, name)),
None => unreachable!("the path of log cannot be empty in config"),
}
}

Expand Down
3 changes: 1 addition & 2 deletions scripts/quick_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ run_xline() {
log::info "run_xline node${1}: docker run -d -it --rm --name=node${1} -d --net=xline_net \
--ip=${SERVERS[$1]} --cap-add=NET_ADMIN --cpu-shares=1024 \
-m=512M ${mount_point} ${image} \
-e RUST_LOG=debug
${cmd}"
log::info xline${1} started
}

# run etcdctl
# args:
# $1: size of cluster
run_etcdctl() {
log::info etcdctl starting

Expand Down

0 comments on commit 602ea08

Please sign in to comment.