|
| 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 | +``` |
0 commit comments