[Awesome Rust] log4rs : A highly configurable logging framework modeled after Java's Logback and log4j libraries
log4rs
log4rs is a highly configurable logging framework modeled after Java’s Logback and log4j libraries.
Dependencies
By default, log4rs can be depended on with:
1 | # Cargo.toml |
Quick Start
log4rs can be configured programmatically by using the builders in the config module to construct a log4rs Config object, which can be passed to the init_config function.
Configuration via a YAML file
log4rs.yaml:
1 | # log4rs.yaml |
1 | // src/lib.rs |
Programmatically constructing a configuration
1 | // src/lib.rs |
For more examples see the (examples)https://github.com/estk/log4rs/tree/master/examples in the source.
Formatters
A formatter inserts a dynamic portion of text into the pattern. It may be text derived from a log event or from some other context like the current time. Formatters may be passed arguments consisting of parenthesized format strings.
The following formatters are currently supported. Unless otherwise stated, a formatter does not accept any argument.
-
d, date - The current time. By default, the ISO 8601 format is used. A custom format may be provided in the syntax accepted by chrono. The timezone defaults to local, but can be specified explicitly by passing a second argument of utc for UTC or local for local time.
-
{d} - 2016-03-20T14:22:20.644420340-08:00
-
{d(%Y-%m-%d %H:%M:%S)} - 2016-03-20 14:22:20
-
{d(%Y-%m-%d %H:%M:%S %Z)(utc)} - 2016-03-20 22:22:20 UTC
-
-
f, file - The source file that the log message came from, or ??? if not provided.
-
h, highlight - Styles its argument according to the log level. The style is intense red for errors, red for warnings, blue for info, and the default style for all other levels.
{h(the level is {l})} - the level is ERROR -
l``, level - The log level.
-
L, line - The line that the log message came from, or ??? if not provided.
-
m, message - The log message.
-
M, module - The module that the log message came from, or ??? if not provided.
-
P, pid - The current process id.
-
n - A platform-specific newline.
-
t, target - The target of the log message.
-
T, thread - The name of the current thread.
-
I, thread_id - The ID of the current thread.
-
X, mdc - A value from the MDC. The first argument specifies the key, and the second argument specifies the default value if the key is not present in the MDC. The second argument is optional, and defaults to the empty string.
-
{X(user_id)} - 123e4567-e89b-12d3-a456-426655440000
-
{X(nonexistent_key)(no mapping)} - no mapping
-
-
An “unnamed” formatter simply formats its argument, applying the format specification.
- {({l} {m})} - INFO hello
See log4rs::encode::pattern - Rust - https://docs.rs/log4rs/0.10.0/log4rs/encode/pattern/index.html to learn more.
References
[1] estk/log4rs: A highly configurable logging framework for Rust - https://github.com/estk/log4rs
[2] log4rs - crates.io: Rust Package Registry - https://crates.io/crates/log4rs