Skip to content

Commit

Permalink
feat(triger): Add "time" triger like log4j
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirreke committed Jan 24, 2024
1 parent 1753fe8 commit f252498
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 261 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Cargo.lock
*.iml
.vscode/
info.log
log/
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ compound_policy = []
delete_roller = []
fixed_window_roller = []
size_trigger = []
time_trigger = []
time_trigger = ["rand"]
json_encoder = ["serde", "serde_json", "chrono", "log-mdc", "log/serde", "thread-id"]
pattern_encoder = ["chrono", "log-mdc", "thread-id"]
ansi_writer = []
Expand Down Expand Up @@ -70,6 +70,7 @@ serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.9", optional = true }
toml = { version = "0.8", optional = true }
parking_lot = { version = "0.12.0", optional = true }
rand = { version = "0.8", optional = true}
thiserror = "1.0.15"
anyhow = "1.0.28"
derivative = "2.2"
Expand Down
32 changes: 21 additions & 11 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ my_rolling_appender:
pattern: "logs/test.{}.log"
```

The new component is the _policy_ field. A policy must have `kind` like most
The new component is the _policy_ field. A policy must have _kind_ field 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. It
supports two types: `size`, and `time`. They both require a `limit` field.
supports two types: `size`, and `time`.

For `size`, the `limit` field is a string which defines the maximum file size
For `size`, it require a _limit_ field. The _limit_ field is a string 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 @@ -192,8 +192,10 @@ trigger:
limit: 10 mb
```

For `time`, the `limit` field is a string which defines the time to roll the
file. The limit field supports the following units(second will be used if the
For `time`, it has three field, _interval_, _modulate_ and _max_random_delay_.

The _interval_ field is a string which defines the time to roll the
file. The interval field supports the following units(second will be used if the
unit is not specified), case does not matter:

- second[s]
Expand All @@ -204,18 +206,26 @@ unit is not specified), case does not matter:
- month[s]
- year[s]

> Note: The log file will be rolled at the integer time. For example, if the
> `limit` is set to `2 day`, the log file will be rolled at 0:00 every other a
> day, regardless of the time `log4rs` was started or the log file was created.
> This means that the initial log file will be likely rolled before the limit
> is reached.
The _modulate_ field is an optional boolean.It indicates whether the interval should
be adjusted to cause the next rollover to occur on the interval boundary. For example,
if the item is hours, the current hour is 3 am and the interval is 4 then the first
rollover will occur at 4 am and then next ones will occur at 8 am, noon, 4pm, etc.
The default value is false.

The _max_random_delay_ field is an optional integar.Indicates the maximum number
of seconds to randomly delay a rollover. By default, this is 0 which indicates no
delay. This setting is useful on servers where multiple applications are configured
to rollover log files at the same time and can spread the load of doing so across
time.

i.e.

```yml
trigger:
kind: time
limit: 7 day
interval: 1 day
modulate: false
max_random_delay: 0
```

The _roller_ field supports two types: delete, and fixed_window. The delete
Expand Down
2 changes: 1 addition & 1 deletion examples/sample_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ appenders:
policy:
trigger:
kind: time
limit: 1 minute
interval: 1 minute
roller:
kind: fixed_window
pattern: "log/old-rolling_file-{}.log"
Expand Down
2 changes: 1 addition & 1 deletion src/append/rolling_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ appenders:
policy:
trigger:
kind: time
limit: 2 minutes
interval: 2 minutes
roller:
kind: delete
bar:
Expand Down

0 comments on commit f252498

Please sign in to comment.