Skip to content

Commit

Permalink
feat!(trigger): add support for CompositeTrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirreke committed Jan 30, 2024
1 parent b0ad8ba commit 348d011
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 47 deletions.
2 changes: 1 addition & 1 deletion benches/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn mk_config(file_size: u64, file_count: u32) -> log4rs::config::Config {
let roller = policy::compound::roll::fixed_window::FixedWindowRoller::builder()
.build(&roll_pattern, file_count)
.unwrap();
let policy = policy::compound::CompoundPolicy::new(Box::new(trigger), Box::new(roller));
let policy = policy::compound::CompoundPolicy::new(vec![Box::new(trigger)], Box::new(roller));
let file = RollingFileAppender::builder()
.encoder(Box::new(PatternEncoder::new(
"{d(%Y-%m-%d %H:%M:%S.%3f %Z)} {l} [{t} - {T}] {m}{n}",
Expand Down
14 changes: 7 additions & 7 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ my_rolling_appender:
path: "logs/test.log"
policy:
kind: compound
trigger:
kind: size
limit: 1mb
triggers:
- kind: size
limit: 1mb
roller:
kind: fixed_window
base: 1
Expand All @@ -171,7 +171,7 @@ my_rolling_appender:
The new component is the _policy_ field. A policy must have `kind` like most
other components, the default (and only supported) policy is `kind: compound`.

The _trigger_ field is used to dictate when the log file should be rolled. The
The _triggers_ field is used to dictate when the log file should be rolled. The
only supported trigger is `kind: size`. There is a required field `limit`
which defines the maximum file size prior to a rolling of the file. The limit
field requires one of the following units in bytes, case does not matter:
Expand All @@ -185,9 +185,9 @@ field requires one of the following units in bytes, case does not matter:
i.e.

```yml
trigger:
kind: size
limit: 10 mb
triggers:
- kind: size
limit: 10 mb
```

The _roller_ field supports two types: delete, and fixed_window. The delete
Expand Down
50 changes: 25 additions & 25 deletions src/append/rolling_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ impl RollingFileAppenderBuilder {
///
/// # The remainder of the configuration is passed along to the policy's
/// # deserializer, and will vary based on the kind of policy.
/// trigger:
/// kind: size
/// limit: 10 mb
/// triggers:
/// - kind: size
/// limit: 10 mb
///
/// roller:
/// kind: delete
Expand Down Expand Up @@ -366,28 +366,28 @@ mod test {
let config = format!(
"
appenders:
foo:
kind: rolling_file
path: {0}/foo.log
policy:
trigger:
kind: size
limit: 1024
roller:
kind: delete
bar:
kind: rolling_file
path: {0}/foo.log
policy:
kind: compound
trigger:
kind: size
limit: 5 mb
roller:
kind: fixed_window
pattern: '{0}/foo.log.{{}}'
base: 1
count: 5
foo:
kind: rolling_file
path: {0}/foo.log
policy:
triggers:
- kind: size
limit: 1024
roller:
kind: delete
bar:
kind: rolling_file
path: {0}/foo.log
policy:
kind: compound
triggers:
- kind: size
limit: 5 mb
roller:
kind: fixed_window
pattern: '{0}/foo.log.{{}}'
base: 1
count: 5
",
dir.path().display()
);
Expand Down
36 changes: 23 additions & 13 deletions src/append/rolling_file/policy/compound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod trigger;
#[derive(Clone, Eq, PartialEq, Hash, Debug, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CompoundPolicyConfig {
trigger: Trigger,
triggers: Vec<Trigger>,
roller: Roller,
}

Expand Down Expand Up @@ -81,27 +81,33 @@ impl<'de> serde::Deserialize<'de> for Roller {
}
}

/// A rolling policy which delegates to a "trigger" and "roller".
/// A rolling policy which delegates to a "triggers" and "roller".
///
/// The trigger determines if the log file should roll, for example, by checking
/// the size of the file. The roller processes the old log file, for example,
/// by compressing it and moving it to a different location.
#[derive(Debug)]
pub struct CompoundPolicy {
trigger: Box<dyn trigger::Trigger>,
triggers: Vec<Box<dyn trigger::Trigger>>,
roller: Box<dyn Roll>,
}

impl CompoundPolicy {
/// Creates a new `CompoundPolicy`.
pub fn new(trigger: Box<dyn trigger::Trigger>, roller: Box<dyn Roll>) -> CompoundPolicy {
CompoundPolicy { trigger, roller }
pub fn new(triggers: Vec<Box<dyn trigger::Trigger>>, roller: Box<dyn Roll>) -> CompoundPolicy {
CompoundPolicy { triggers, roller }
}
}

impl Policy for CompoundPolicy {
fn process(&self, log: &mut LogFile) -> anyhow::Result<()> {
if self.trigger.trigger(log)? {
let mut is_trigger = false;
for trigger in &self.triggers {
if trigger.trigger(log)? {
is_trigger = true;
}
}
if is_trigger {
log.roll();
self.roller.roll(log.path())?;
}
Expand All @@ -117,14 +123,14 @@ impl Policy for CompoundPolicy {
/// kind: compound
///
/// # The trigger, which determines when the log will roll over. Required.
/// trigger:
/// triggers:
///
/// # Identifies which trigger is to be used. Required.
/// kind: size
/// - kind: size
///
/// # The remainder of the configuration is passed to the trigger's
/// # deserializer, and will vary based on the kind of trigger.
/// limit: 10 mb
/// # The remainder of the configuration is passed to the trigger's
/// # deserializer, and will vary based on the kind of trigger.
/// limit: 10 mb
///
/// # The roller, which processes the old log file. Required.
/// roller:
Expand All @@ -150,8 +156,12 @@ impl Deserialize for CompoundPolicyDeserializer {
config: CompoundPolicyConfig,
deserializers: &Deserializers,
) -> anyhow::Result<Box<dyn Policy>> {
let trigger = deserializers.deserialize(&config.trigger.kind, config.trigger.config)?;
let mut triggers = Vec::new();
for config_trigger in &config.triggers {
let trigger = deserializers.deserialize(&config_trigger.kind, config_trigger.config.clone())?;
triggers.push(trigger);
}
let roller = deserializers.deserialize(&config.roller.kind, config.roller.config)?;
Ok(Box::new(CompoundPolicy::new(trigger, roller)))
Ok(Box::new(CompoundPolicy::new(triggers, roller)))
}
}
2 changes: 1 addition & 1 deletion src/append/rolling_file/policy/compound/trigger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ pub trait Trigger: fmt::Debug + Send + Sync + 'static {
#[cfg(feature = "config_parsing")]
impl Deserializable for dyn Trigger {
fn name() -> &'static str {
"trigger"
"triggers"
}
}

0 comments on commit 348d011

Please sign in to comment.