diff --git a/benches/rotation.rs b/benches/rotation.rs index 4167331f..799174f2 100644 --- a/benches/rotation.rs +++ b/benches/rotation.rs @@ -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}", diff --git a/docs/Configuration.md b/docs/Configuration.md index 6c23c9f2..2d6fae91 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -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 @@ -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: @@ -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 diff --git a/src/append/rolling_file/mod.rs b/src/append/rolling_file/mod.rs index e14e4ad8..dc499a62 100644 --- a/src/append/rolling_file/mod.rs +++ b/src/append/rolling_file/mod.rs @@ -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 @@ -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() ); diff --git a/src/append/rolling_file/policy/compound/mod.rs b/src/append/rolling_file/policy/compound/mod.rs index 9fe7b233..f3b2af5e 100644 --- a/src/append/rolling_file/policy/compound/mod.rs +++ b/src/append/rolling_file/policy/compound/mod.rs @@ -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, roller: Roller, } @@ -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, + triggers: Vec>, roller: Box, } impl CompoundPolicy { /// Creates a new `CompoundPolicy`. - pub fn new(trigger: Box, roller: Box) -> CompoundPolicy { - CompoundPolicy { trigger, roller } + pub fn new(triggers: Vec>, roller: Box) -> 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())?; } @@ -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: @@ -150,8 +156,12 @@ impl Deserialize for CompoundPolicyDeserializer { config: CompoundPolicyConfig, deserializers: &Deserializers, ) -> anyhow::Result> { - 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))) } } diff --git a/src/append/rolling_file/policy/compound/trigger/mod.rs b/src/append/rolling_file/policy/compound/trigger/mod.rs index 76e67e74..3e63e789 100644 --- a/src/append/rolling_file/policy/compound/trigger/mod.rs +++ b/src/append/rolling_file/policy/compound/trigger/mod.rs @@ -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" } }