[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
2
3
4
5
# Cargo.toml

[dependencies]
log = "0.4.8"
log4rs = "1.0.0"

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# log4rs.yaml

refresh_rate: 30 seconds
appenders:
stdout:
kind: console
requests:
kind: file
path: "log/requests.log"
encoder:
pattern: "{d} - {m}{n}"
root:
level: warn
appenders:
- stdout
loggers:
app::backend::db:
level: info
app::requests:
level: info
appenders:
- requests
additive: false

lib.rs:

1
2
3
4
5
6
7
8
9
10
11
12
// src/lib.rs

use log::{error, info, warn};
use log4rs;

fn main() {
log4rs::init_file("config/log4rs.yaml", Default::default()).unwrap();

info!("booting up");

// ...
}

Programmatically constructing a configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// src/lib.rs

use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
use log4rs::encode::pattern::PatternEncoder;
use log4rs::config::{Appender, Config, Logger, Root};

fn main() {
let stdout = ConsoleAppender::builder().build();

let requests = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new("{d} - {m}{n}")))
.build("log/requests.log")
.unwrap();

let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.appender(Appender::builder().build("requests", Box::new(requests)))
.logger(Logger::builder().build("app::backend::db", LevelFilter::Info))
.logger(Logger::builder()
.appender("requests")
.additive(false)
.build("app::requests", LevelFilter::Info))
.build(Root::builder().appender("stdout").build(LevelFilter::Warn))
.unwrap();

let handle = log4rs::init_config(config).unwrap();

// use handle to change logger configuration at runtime
}

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

[3] log4rs - Rust - https://docs.rs/log4rs/1.0.0/log4rs/

[4] log - Rust - https://docs.rs/log/