Skip to content

Commit 673047d

Browse files
authored
Merge branch 'main' into integration_tests
2 parents 1260ca0 + f688e38 commit 673047d

File tree

6 files changed

+144
-1
lines changed

6 files changed

+144
-1
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ delete_roller = []
2626
fixed_window_roller = []
2727
size_trigger = []
2828
time_trigger = ["rand"]
29+
onstartup_trigger = []
2930
json_encoder = ["serde", "serde_json", "chrono", "log-mdc", "log/serde", "thread-id"]
3031
pattern_encoder = ["chrono", "log-mdc", "thread-id"]
3132
ansi_writer = []
@@ -43,6 +44,7 @@ all_components = [
4344
"fixed_window_roller",
4445
"size_trigger",
4546
"time_trigger",
47+
"onstartup_trigger",
4648
"json_encoder",
4749
"pattern_encoder",
4850
"threshold_filter"

docs/Configuration.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ The new component is the _policy_ field. A policy must have the _kind_ field lik
172172
other components, the default (and only supported) policy is `kind: compound`.
173173

174174
The _trigger_ field is used to dictate when the log file should be rolled. It
175-
supports two types: `size`, and `time`.
175+
supports three types: `size`, `time` and `onstartup`.
176176

177177
For `size`, it require a _limit_ field. The _limit_ field is a string which defines the maximum file size
178178
prior to a rolling of the file. The limit field requires one of the following
@@ -233,6 +233,16 @@ trigger:
233233
max_random_delay: 0
234234
```
235235

236+
For `onstartup`, it has an optional field, _min_size_. It indicates the minimum size the file must have to roll over. A size of zero will cause a roll over no matter what the file size is. The default value is 1, which will prevent rolling over an empty file.
237+
238+
i.e.
239+
240+
```yml
241+
trigger:
242+
kind: onstartup
243+
min_size: 1
244+
```
245+
236246
The _roller_ field supports two types: delete, and fixed_window. The delete
237247
roller does not take any other configuration fields. The fixed_window roller
238248
supports three fields: pattern, base, and count. The most current log file will

src/append/rolling_file/policy/compound/trigger/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub mod size;
1212
#[cfg(feature = "time_trigger")]
1313
pub mod time;
1414

15+
#[cfg(feature = "onstartup_trigger")]
16+
pub mod onstartup;
17+
1518
/// A trait which identifies if the active log file should be rolled over.
1619
pub trait Trigger: fmt::Debug + Send + Sync + 'static {
1720
/// Determines if the active log file should be rolled over.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//! The OnStartUp trigger.
2+
//!
3+
//! Requires the `onstartup_trigger` feature.
4+
5+
use std::sync::Once;
6+
7+
use crate::append::rolling_file::{policy::compound::trigger::Trigger, LogFile};
8+
9+
#[cfg(feature = "config_parsing")]
10+
use crate::config::{Deserialize, Deserializers};
11+
12+
/// Configuration for the onstartup trigger.
13+
#[cfg(feature = "config_parsing")]
14+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, serde::Deserialize)]
15+
#[serde(deny_unknown_fields)]
16+
pub struct OnStartUpTriggerConfig {
17+
#[serde(default = "default_min_size")]
18+
min_size: u64,
19+
}
20+
21+
#[cfg(feature = "config_parsing")]
22+
fn default_min_size() -> u64 {
23+
1
24+
}
25+
26+
/// A trigger which rolls the log on startup.
27+
#[derive(Debug)]
28+
pub struct OnStartUpTrigger {
29+
min_size: u64,
30+
initial: Once,
31+
}
32+
33+
impl OnStartUpTrigger {
34+
/// Returns a new trigger which rolls the log on startup.
35+
pub fn new(min_size: u64) -> OnStartUpTrigger {
36+
OnStartUpTrigger {
37+
min_size,
38+
initial: Once::new(),
39+
}
40+
}
41+
}
42+
43+
impl Trigger for OnStartUpTrigger {
44+
fn trigger(&self, file: &LogFile) -> anyhow::Result<bool> {
45+
let mut result = false;
46+
self.initial.call_once(|| {
47+
if file.len_estimate() >= self.min_size {
48+
result = true;
49+
}
50+
});
51+
Ok(result)
52+
}
53+
54+
fn is_pre_process(&self) -> bool {
55+
true
56+
}
57+
}
58+
59+
/// A deserializer for the `OnStartUpTrigger`.
60+
///
61+
/// # Configuration
62+
///
63+
/// ```yaml
64+
/// kind: onstartup
65+
///
66+
/// ```
67+
#[cfg(feature = "config_parsing")]
68+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
69+
pub struct OnStartUpTriggerDeserializer;
70+
71+
#[cfg(feature = "config_parsing")]
72+
impl Deserialize for OnStartUpTriggerDeserializer {
73+
type Trait = dyn Trigger;
74+
75+
type Config = OnStartUpTriggerConfig;
76+
77+
fn deserialize(
78+
&self,
79+
config: OnStartUpTriggerConfig,
80+
_: &Deserializers,
81+
) -> anyhow::Result<Box<dyn Trigger>> {
82+
Ok(Box::new(OnStartUpTrigger::new(config.min_size)))
83+
}
84+
}
85+
86+
#[cfg(test)]
87+
mod test {
88+
use super::*;
89+
90+
#[test]
91+
fn pre_process() {
92+
let trigger = OnStartUpTrigger::new(0);
93+
assert!(trigger.is_pre_process());
94+
}
95+
96+
fn trigger_with_file_size(file_size: u64) -> (bool, bool) {
97+
let file = tempfile::tempdir().unwrap();
98+
let logfile = LogFile {
99+
writer: &mut None,
100+
path: file.path(),
101+
len: file_size,
102+
};
103+
104+
let trigger = OnStartUpTrigger::new(1); // default
105+
let result1 = trigger.trigger(&logfile).unwrap();
106+
let result2 = trigger.trigger(&logfile).unwrap();
107+
(result1, result2)
108+
}
109+
110+
#[test]
111+
fn trigger() {
112+
// When the file size < min_size, the trigger should return false.
113+
assert_eq!(trigger_with_file_size(0), (false, false));
114+
// When the file size == min_size, the trigger should return true for the first time.
115+
assert_eq!(trigger_with_file_size(1), (true, false));
116+
// When the file size >= min_size, the trigger should return true for the first time.
117+
assert_eq!(trigger_with_file_size(2), (true, false));
118+
}
119+
}

src/config/raw.rs

+8
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ impl Default for Deserializers {
219219
append::rolling_file::policy::compound::trigger::time::TimeTriggerDeserializer,
220220
);
221221

222+
#[cfg(feature = "onstartup_trigger")]
223+
d.insert(
224+
"onstartup",
225+
append::rolling_file::policy::compound::trigger::onstartup::OnStartUpTriggerDeserializer,
226+
);
227+
222228
#[cfg(feature = "json_encoder")]
223229
d.insert("json", encode::json::JsonEncoderDeserializer);
224230

@@ -265,6 +271,8 @@ impl Deserializers {
265271
/// * Requires the `size_trigger` feature.
266272
/// * "time" -> `TimeTriggerDeserializer`
267273
/// * Requires the `time_trigger` feature.
274+
/// * "onstartup" -> `OnStartUpTriggerDeserializer`
275+
/// * Requires the `onstartup_trigger` feature.
268276
pub fn new() -> Deserializers {
269277
Deserializers::default()
270278
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//! - Triggers
2323
//! - [size](append/rolling_file/policy/compound/trigger/size/struct.SizeTriggerDeserializer.html#configuration): requires the `size_trigger` feature
2424
//! - [time](append/rolling_file/policy/compound/trigger/tine/struct.TimeTriggerDeserializer.html#configuration): requires the `time_trigger` feature
25+
//! - [onstartup](append/rolling_file/policy/compound/trigger/tine/struct.OnStartUpTriggerDeserializer.html#configuration): requires the `onstartup_trigger` feature
2526
//!
2627
//! ## Encoders
2728
//!

0 commit comments

Comments
 (0)