Skip to content

Commit

Permalink
Attempt to expand env vars in the path for File and RollingFileAppenders
Browse files Browse the repository at this point in the history
  • Loading branch information
gadunga committed Apr 4, 2020
1 parent 1031ba6 commit e238fb3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ json_format = ["serde_json"]
toml_format = ["toml"]

console_appender = ["console_writer", "simple_writer", "pattern_encoder"]
file_appender = ["parking_lot", "simple_writer", "pattern_encoder"]
rolling_file_appender = ["parking_lot", "simple_writer", "pattern_encoder"]
file_appender = ["parking_lot", "simple_writer", "pattern_encoder", "regex"]
rolling_file_appender = ["parking_lot", "simple_writer", "pattern_encoder", "regex"]
compound_policy = []
delete_roller = []
fixed_window_roller = []
Expand Down Expand Up @@ -68,6 +68,7 @@ serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.8.4", optional = true }
toml = { version = "0.5", optional = true }
parking_lot = { version = "0.10.0", optional = true }
regex = { version = "1", optional = true }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", optional = true, features = ["handleapi", "minwindef", "processenv", "winbase", "wincon"] }
Expand Down
10 changes: 9 additions & 1 deletion src/append/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ impl FileAppenderBuilder {
}

/// Consumes the `FileAppenderBuilder`, producing a `FileAppender`.
/// The path argument can contain environment variables of the form $ENV{name_here},
/// where 'name_here' will be the name of the environment variable that
/// will be resolved. Note that if the variable fails to resolve,
/// $ENV{name_here} will NOT be replaced in the path.
pub fn build<P: AsRef<Path>>(self, path: P) -> io::Result<FileAppender> {
let path = path.as_ref().to_owned();
let path = super::env_util::expand_env_vars(path.as_ref().to_path_buf());
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
Expand Down Expand Up @@ -122,6 +126,10 @@ impl FileAppenderBuilder {
/// kind: file
///
/// # The path of the log file. Required.
/// # The path can contain environment variables of the form $ENV{name_here},
/// # where 'name_here' will be the name of the environment variable that
/// # will be resolved. Note that if the variable fails to resolve,
/// # $ENV{name_here} will NOT be replaced in the path.
/// path: log/foo.log
///
/// # Specifies if the appender should append to or truncate the log file if it
Expand Down
14 changes: 14 additions & 0 deletions src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ pub mod file;
#[cfg(feature = "rolling_file_appender")]
pub mod rolling_file;

#[cfg(any(feature = "file_appender", feature = "rolling_file_appender"))]
mod env_util {
pub fn expand_env_vars(path: std::path::PathBuf) -> std::path::PathBuf {
let mut path: String = path.to_string_lossy().into();
let matcher = regex::Regex::new(r#"\$ENV\{([\w][\w|\d|\.|_]*)\}"#).unwrap();
matcher.captures_iter(&path.clone()).for_each(|c| {
if let Ok(s) = std::env::var(&c[1]) {
path = path.replace(&c[0], &s);
}
});
path.into()
}
}

/// A trait implemented by log4rs appenders.
///
/// Appenders take a log record and processes them, for example, by writing it
Expand Down
11 changes: 10 additions & 1 deletion src/append/rolling_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ impl RollingFileAppenderBuilder {
}

/// Constructs a `RollingFileAppender`.
/// The path argument can contain environment variables of the form $ENV{name_here},
/// where 'name_here' will be the name of the environment variable that
/// will be resolved. Note that if the variable fails to resolve,
/// $ENV{name_here} will NOT be replaced in the path.
pub fn build<P>(
self,
path: P,
Expand All @@ -258,9 +262,10 @@ impl RollingFileAppenderBuilder {
where
P: AsRef<Path>,
{
let path = super::env_util::expand_env_vars(path.as_ref().to_path_buf());
let appender = RollingFileAppender {
writer: Mutex::new(None),
path: path.as_ref().to_owned(),
path,
append: self.append,
encoder: self
.encoder
Expand All @@ -287,6 +292,10 @@ impl RollingFileAppenderBuilder {
/// kind: rolling_file
///
/// # The path of the log file. Required.
/// # The path can contain environment variables of the form $ENV{name_here},
/// # where 'name_here' will be the name of the environment variable that
/// # will be resolved. Note that if the variable fails to resolve,
/// # $ENV{name_here} will NOT be replaced in the path.
/// path: log/foo.log
///
/// # Specifies if the appender should append to or truncate the log file if it
Expand Down

0 comments on commit e238fb3

Please sign in to comment.