Skip to content

Commit e49122b

Browse files
author
Bryan Conn
authored
Add documentation covering all current aspects of the configuration file (#308)
* Add documentation covering all current aspects of the configuration file * ci: add lint workflow * chore: review markdown * docs: Add the configuration doc to docs.rs * chore: review comments. Bump min rust version * chore(docs): bump rust version in readme * fix(docs): markdown lint fixes for new doc and README * fix: missing newline * fix(ci): Resolve clippy issues * chore: move config docs, dedub section, add examples link
1 parent 5544688 commit e49122b

File tree

14 files changed

+276
-29
lines changed

14 files changed

+276
-29
lines changed

.github/workflows/main.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
- devel
1212

1313
jobs:
14-
lint:
14+
rust-lint:
1515
name: Lint
1616
runs-on: ubuntu-latest
1717
steps:
@@ -37,7 +37,7 @@ jobs:
3737
runs-on: ${{ matrix.os }}
3838
strategy:
3939
matrix:
40-
rust_versions: ["stable", "1.56"]
40+
rust_versions: ["stable", "1.57"]
4141
os: [ubuntu-latest, windows-latest]
4242
steps:
4343
- name: Checkout the source code
@@ -81,7 +81,7 @@ jobs:
8181
runs-on: ubuntu-latest
8282
strategy:
8383
matrix:
84-
rust_versions: ["stable", "1.56"]
84+
rust_versions: ["stable", "1.57"]
8585
steps:
8686
- name: Checkout the source code
8787
uses: actions/checkout@master
@@ -93,3 +93,13 @@ jobs:
9393
9494
- name: Rotation with backgrounded rotation
9595
run: cargo bench --features gzip,background_rotation
96+
97+
docs-lint:
98+
name: Lint
99+
runs-on: ubuntu-latest
100+
steps:
101+
- name: Markdown Linting Action
102+
uses: avto-dev/markdown-lint@v1.5.0
103+
with:
104+
config: .markdownlint.yml
105+
args: docs/Configuration.md README.md

.markdownlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
line-length: false

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repository = "https://github.com/estk/log4rs"
88
readme = "README.md"
99
keywords = ["log", "logger", "logging", "log4"]
1010
edition = "2018"
11-
rust = "1.56"
11+
rust = "1.57"
1212

1313
[features]
1414
default = ["all_components", "config_parsing", "yaml_format"]
@@ -58,7 +58,7 @@ chrono = { version = "0.4", optional = true }
5858
flate2 = { version = "1.0", optional = true }
5959
fnv = "1.0"
6060
humantime = { version = "2.0", optional = true }
61-
log = { version = "0.4.0", features = ["std"] }
61+
log = { version = "=0.4.17", features = ["std"] }
6262
log-mdc = { version = "0.1", optional = true }
6363
serde = { version = "1.0", optional = true, features = ["derive"] }
6464
serde-value = { version = "0.7", optional = true }

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
log4rs is a highly configurable logging framework modeled after Java's Logback
1010
and log4j libraries.
1111

12-
### Warning
12+
## Warning
1313

1414
If you are using the file rotation in your configuration there is a known substantial performance issue so listen up!
1515
By default the `gzip` feature is enabled and when rolling files it will zip log archives automatically. This is a problem
@@ -27,6 +27,7 @@ For more information see the PR that added [`background_rotation`](https://githu
2727
## Quick Start
2828

2929
log4rs.yaml:
30+
3031
```yaml
3132
refresh_rate: 30 seconds
3233
appenders:
@@ -52,6 +53,7 @@ loggers:
5253
```
5354
5455
lib.rs:
56+
5557
```rust
5658
use log::{error, info, warn};
5759
use log4rs;
@@ -67,7 +69,7 @@ fn main() {
6769

6870
## Rust Version Requirements
6971

70-
1.46
72+
1.57
7173

7274
## Building for Dev
7375

@@ -79,8 +81,9 @@ fn main() {
7981
## License
8082

8183
Licensed under either of
82-
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
83-
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
84+
85+
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
86+
* MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
8487

8588
at your option.
8689

docs/Configuration.md

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# Configuration
2+
3+
log4rs can be configured programmatically by using the builders in the `config`
4+
module to construct a log4rs `Config` object, which can be passed to the
5+
`init_config` function.
6+
7+
The more common configuration method, however, is via a separate config file.
8+
The `init_file` function takes the path to a config file as well as a
9+
`Deserializers` object which is responsible for instantiating the various
10+
objects specified by the config file. The following section covers the exact
11+
configuration syntax. Examples of both the programatic and configuration files
12+
can be found in the
13+
[examples directory](https://github.com/estk/log4rs/tree/master/examples).
14+
15+
## Common Fields
16+
17+
### LevelFilter's
18+
19+
- Off
20+
- Error
21+
- Warn
22+
- Info
23+
- Debug
24+
- Trace
25+
26+
### Filters
27+
28+
The only accepted `filter` is of kind threshold with a level. The level must
29+
be a [LevelFilter](#levelfilters). One to many filters are allowed.
30+
31+
i.e.
32+
33+
```yml
34+
filters:
35+
- kind: threshold
36+
level: info
37+
```
38+
39+
### Encoder
40+
41+
An `encoder` consists of a kind: the default which is pattern, or json. If
42+
pattern is defined, the default pattern `{d} {l} {t} - {m}{n}` is used unless
43+
overridden. Refer to
44+
[this documentation](https://docs.rs/log4rs/latest/log4rs/encode/pattern/index.html#formatters)
45+
for details regarding valid patterns.
46+
47+
> Note that the json encoder does not have any additional controls such as the
48+
> pattern field.
49+
50+
i.e.
51+
52+
```yml
53+
encoder:
54+
kind: pattern
55+
pattern: "{h({d(%+)(utc)} [{f}:{L}] {l:<6} {M}:{m})}{n}"
56+
```
57+
58+
## Loggers
59+
60+
A map of logger configurations.
61+
62+
### Logger Configuration
63+
64+
The _name_ of the logger is the yml tag.
65+
66+
The _level_ of the logger is optional and defaults to the parents log level.
67+
The level must be a [LevelFilter](#levelfilters).
68+
69+
The _appenders_ field is an optional list of [appenders](#appenders) attached
70+
to the logger.
71+
72+
The _additive_ field is an optional boolean determining if the loggers parent
73+
will also be attached to this logger. The default is true.
74+
75+
i.e.
76+
77+
```yml
78+
loggers:
79+
my_logger:
80+
level: info
81+
appenders:
82+
- my_appender
83+
additive: true
84+
```
85+
86+
## The Root Logger
87+
88+
Root is the required logger. It is the parent to all children loggers. To
89+
configure the Root, refer to [the logger section](#logger-configuration).
90+
91+
> Note: The root logger has no parent and therefore cannot the _additive_
92+
field does not apply.
93+
94+
```yml
95+
root:
96+
level: info
97+
appenders:
98+
- my_appender
99+
```
100+
101+
## Appenders
102+
103+
All appenders require a unique identifying string for each
104+
[appender configuration](#appender-config).
105+
106+
### Appender Config
107+
108+
Each Appender Kind has it's own configuration. However, all accept
109+
[filters](#filters). The `kind` field is required in an appender configuration.
110+
111+
#### The Console Appender
112+
113+
The _target_ field is optional and accepts `stdout` or `stderr`. It's default
114+
value is stdout.
115+
116+
The _tty_only_ field is an optional boolean and dictates that the appender must
117+
only write when the target is a TTY. It's default value is false.
118+
119+
The _encoder_ field is optional and can consist of multiple fields. Refer to
120+
the [encoder](#encoder) documention.
121+
122+
```yml
123+
my_console_appender:
124+
kind: console
125+
target: stdout
126+
tty_only: false
127+
```
128+
129+
#### The File Appender
130+
131+
The _path_ field is required and accepts environment variables of the form
132+
`$ENV{name_here}`. The path can be relative or absolute.
133+
134+
The _encoder_ field is optional and can consist of multiple fields. Refer to
135+
the [encoder](#encoder) documention.
136+
137+
The _append_ field is an optional boolean and defaults to `true`. True will
138+
append to the log file if it exists, false will truncate the existing file.
139+
140+
```yml
141+
my_file_appender:
142+
kind: file
143+
path: $ENV{PWD}/log/test.log
144+
append: true
145+
```
146+
147+
#### The Rolling File Appender
148+
149+
The rolling file configuration is by far the most complex. Like the
150+
[file appender](#the-file-appender), the path to the log file is required
151+
with the _append_ and the _encoders_ optional fields.
152+
153+
i.e.
154+
155+
```yml
156+
my_rolling_appender:
157+
kind: rolling_file
158+
path: "logs/test.log"
159+
policy:
160+
kind: compound
161+
trigger:
162+
kind: size
163+
limit: 1mb
164+
roller:
165+
kind: fixed_window
166+
base: 1
167+
count: 5
168+
pattern: "logs/test.{}.log"
169+
```
170+
171+
The new component is the _policy_ field. A policy must have `kind` like most
172+
other components, the default (and only supported) policy is `kind: compound`.
173+
174+
The _trigger_ field is used to dictate when the log file should be rolled. The
175+
only supported trigger is `kind: size`. There is a required field `limit`
176+
which defines the maximum file size prior to a rolling of the file. The limit
177+
field requires one of the following units in bytes, case does not matter:
178+
179+
- b
180+
- kb/kib
181+
- mb/mib
182+
- gb/gib
183+
- tb/tib
184+
185+
i.e.
186+
187+
```yml
188+
trigger:
189+
kind: size
190+
limit: 10 mb
191+
```
192+
193+
The _roller_ field supports two types: delete, and fixed_window. The delete
194+
roller does not take any other configuration fields. The fixed_window roller
195+
supports three fields: pattern, base, and count. The most current log file will
196+
always have the _base_ index.
197+
198+
The _pattern_ field is used to rename files. The pattern must contain the
199+
double curly brace `{}`. For example `archive/foo.{}.log`. Each instance of
200+
`{}` will be replaced with the index number of the configuration file. Note
201+
that if the file extension of the pattern is `.gz` and the `gzip` Cargo
202+
feature is enabled, the archive files will be gzip-compressed.
203+
204+
> Note: This pattern field is only used for archived files. The `path` field
205+
of the higher level `rolling_file` will be used for the active log file.
206+
207+
The _base_ field is the starting index used to name rolling files.
208+
209+
The _count_ field is the exclusive maximum index used to name rolling files.
210+
However, be warned that the roller renames every file when a log rolls over.
211+
Having a large count value can negatively impact performance.
212+
213+
i.e.
214+
215+
```yml
216+
roller:
217+
kind: fixed_window
218+
base: 1
219+
count: 5
220+
pattern: "archive/journey-service.{}.log"
221+
```
222+
223+
or
224+
225+
```yml
226+
roller:
227+
kind: delete
228+
```
229+
230+
## Refresh Rate
231+
232+
The _refresh_rate_ accepts a u64 value in seconds. The field is used to
233+
determine how often log4rs will scan the configuration file for changes. If a
234+
change is discovered, the logger will reconfigure automatically.
235+
236+
i.e.
237+
238+
```yml
239+
refresh_rate: 30 seconds
240+
```

src/append/console.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl ConsoleAppenderBuilder {
199199
writer,
200200
encoder: self
201201
.encoder
202-
.unwrap_or_else(|| Box::new(PatternEncoder::default())),
202+
.unwrap_or_else(|| Box::<PatternEncoder>::default()),
203203
do_write,
204204
}
205205
}

src/append/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl FileAppenderBuilder {
106106
file: Mutex::new(SimpleWriter(BufWriter::with_capacity(1024, file))),
107107
encoder: self
108108
.encoder
109-
.unwrap_or_else(|| Box::new(PatternEncoder::default())),
109+
.unwrap_or_else(|| Box::<PatternEncoder>::default()),
110110
})
111111
}
112112
}

src/append/rolling_file/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl RollingFileAppenderBuilder {
264264
append: self.append,
265265
encoder: self
266266
.encoder
267-
.unwrap_or_else(|| Box::new(PatternEncoder::default())),
267+
.unwrap_or_else(|| Box::<PatternEncoder>::default()),
268268
policy,
269269
};
270270

src/append/rolling_file/policy/compound/roll/delete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ impl Deserialize for DeleteRollerDeserializer {
5656
_: DeleteRollerConfig,
5757
_: &Deserializers,
5858
) -> anyhow::Result<Box<dyn Roll>> {
59-
Ok(Box::new(DeleteRoller::default()))
59+
Ok(Box::<DeleteRoller>::default())
6060
}
6161
}

0 commit comments

Comments
 (0)