Skip to content

Commit

Permalink
feat(is_post_process): add config item
Browse files Browse the repository at this point in the history
add `is_post_process` and make  it configurable
  • Loading branch information
Dirreke committed Dec 4, 2023
1 parent 7e5a1d5 commit c768858
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Cargo.lock
.idea/
*.iml
.vscode/
log/
11 changes: 8 additions & 3 deletions examples/compile_time_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{thread::sleep, time::Duration};

use log::{error, info, trace};
use log4rs;
use serde_yaml;
Expand All @@ -7,7 +9,10 @@ fn main() {
let config = serde_yaml::from_str(config_str).unwrap();
log4rs::init_raw_config(config).unwrap();

info!("Goes to console");
error!("Goes to console");
trace!("Doesn't go to console as it is filtered out");
for _ in 1..40 {
info!("Goes to console, file and rolling file");
error!("Goes to console, file and rolling file");
trace!("Doesn't go to console as it is filtered out");
sleep(Duration::from_secs(2));
}
}
7 changes: 4 additions & 3 deletions examples/sample_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ appenders:
level: info
file:
kind: file
path: "log/log.log"
path: "log/file.log"
encoder:
pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
rollingfile:
kind: rolling_file
path: "log/log2.log"
path: "log/rolling_file.log"
encoder:
pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
policy:
Expand All @@ -22,9 +22,10 @@ appenders:
limit: 1 minute
roller:
kind: fixed_window
pattern: "log/old-log-{}.log"
pattern: "log/old-rolling_file-{}.log"
base: 0
count: 2
is_post_process: true
root:
level: info
appenders:
Expand Down
45 changes: 31 additions & 14 deletions src/append/rolling_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,39 @@ impl Append for RollingFileAppender {
// TODO(eas): Perhaps this is better as a concurrent queue?
let mut writer = self.writer.lock();

let is_post_process = self.policy.is_post_process();
let log_writer = self.get_writer(&mut writer)?;
let len = log_writer.len;

let mut file = LogFile {
writer: &mut writer,
path: &self.path,
len,
};
if is_post_process {
let len = log_writer.len;

let mut file = LogFile {
writer: &mut writer,
path: &self.path,
len,
};

// TODO(eas): Idea: make this optionally return a future, and if so, we initialize a queue for
// data that comes in while we are processing the file rotation.

// TODO(eas): Idea: make this optionally return a future, and if so, we initialize a queue for
// data that comes in while we are processing the file rotation.
self.policy.process(&mut file)?;

//first, rotate
self.policy.process(&mut file)?;
let log_writer_new = self.get_writer(&mut writer)?;
self.encoder.encode(log_writer_new, record)?;
log_writer_new.flush()?;
} else {
self.encoder.encode(log_writer, record)?;
log_writer.flush()?;
let len = log_writer.len;

//second, write
let writer_file = self.get_writer(&mut writer)?;
self.encoder.encode(writer_file, record)?;
writer_file.flush()?;
let mut file = LogFile {
writer: &mut writer,
path: &self.path,
len,
};

self.policy.process(&mut file)?;
}

Ok(())
}
Expand Down Expand Up @@ -410,6 +424,9 @@ appenders:
fn process(&self, _: &mut LogFile) -> anyhow::Result<()> {
Ok(())
}
fn is_post_process(&self) -> bool {
false
}
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions src/append/rolling_file/policy/compound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ impl Policy for CompoundPolicy {
}
Ok(())
}

fn is_post_process(&self) -> bool {
self.roller.is_post_process()
}
}

/// A deserializer for the `CompoundPolicyDeserializer`.
Expand Down
11 changes: 8 additions & 3 deletions src/append/rolling_file/policy/compound/roll/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ use crate::config::{Deserialize, Deserializers};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DeleteRollerConfig {
#[serde(skip_deserializing)]
_p: (),
is_post_process: Option<bool>,
}

/// A roller which deletes the log file.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
pub struct DeleteRoller(());
pub struct DeleteRoller {
is_post_process: bool,
}

impl Roll for DeleteRoller {
fn roll(&self, file: &Path) -> anyhow::Result<()> {
fs::remove_file(file).map_err(Into::into)
}

fn is_post_process(&self) -> bool {
self.is_post_process
}
}

impl DeleteRoller {
Expand Down
14 changes: 13 additions & 1 deletion src/append/rolling_file/policy/compound/roll/fixed_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct FixedWindowRollerConfig {
pattern: String,
base: Option<u32>,
count: u32,
is_post_process: Option<bool>,
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
Expand Down Expand Up @@ -88,14 +89,15 @@ pub struct FixedWindowRoller {
compression: Compression,
base: u32,
count: u32,
is_post_process: bool,
#[cfg(feature = "background_rotation")]
cond_pair: Arc<(Mutex<bool>, Condvar)>,
}

impl FixedWindowRoller {
/// Returns a new builder for the `FixedWindowRoller`.
pub fn builder() -> FixedWindowRollerBuilder {
FixedWindowRollerBuilder { base: 0 }
FixedWindowRollerBuilder::default()
}
}

Expand Down Expand Up @@ -156,6 +158,10 @@ impl Roll for FixedWindowRoller {

Ok(())
}

fn is_post_process(&self) -> bool {
self.is_post_process
}
}

fn move_file<P, Q>(src: P, dst: Q) -> io::Result<()>
Expand Down Expand Up @@ -240,6 +246,7 @@ fn rotate(
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
pub struct FixedWindowRollerBuilder {
base: u32,
is_post_process: bool,
}

impl FixedWindowRollerBuilder {
Expand All @@ -251,6 +258,10 @@ impl FixedWindowRollerBuilder {
self
}

pub fn is_post_process(mut self, b: bool) -> FixedWindowRollerBuilder {
self.is_post_process = b;
self
}
/// Constructs a new `FixedWindowRoller`.
///
/// `pattern` is either an absolute path or lacking a leading `/`, relative
Expand Down Expand Up @@ -284,6 +295,7 @@ impl FixedWindowRollerBuilder {
compression,
base: self.base,
count,
is_post_process: self.is_post_process,
#[cfg(feature = "background_rotation")]
cond_pair: Arc::new((Mutex::new(true), Condvar::new())),
})
Expand Down
2 changes: 2 additions & 0 deletions src/append/rolling_file/policy/compound/roll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub trait Roll: fmt::Debug + Send + Sync + 'static {
/// If this method returns successfully, there *must* no longer be a file
/// at the specified location.
fn roll(&self, file: &Path) -> anyhow::Result<()>;

fn is_post_process(&self) -> bool;
}

#[cfg(feature = "config_parsing")]
Expand Down
2 changes: 2 additions & 0 deletions src/append/rolling_file/policy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub trait Policy: Sync + Send + 'static + fmt::Debug {
/// This method is called after each log event. It is provided a reference
/// to the current log file.
fn process(&self, log: &mut LogFile) -> anyhow::Result<()>;

fn is_post_process(&self) -> bool;
}

#[cfg(feature = "config_parsing")]
Expand Down

0 comments on commit c768858

Please sign in to comment.