#logging-framework #logging #log #logger #log4

log4rs

A highly configurable multi-output logging implementation for the log facade

40 releases (5 stable)

1.3.0 Feb 10, 2024
1.2.0 Oct 11, 2022
1.1.1 Apr 22, 2022
1.0.0 Dec 16, 2020
0.3.1 Mar 26, 2015

#4 in Debugging

Download history 77961/week @ 2024-07-30 76632/week @ 2024-08-06 78813/week @ 2024-08-13 74783/week @ 2024-08-20 65889/week @ 2024-08-27 74635/week @ 2024-09-03 68848/week @ 2024-09-10 64890/week @ 2024-09-17 69995/week @ 2024-09-24 63330/week @ 2024-10-01 67561/week @ 2024-10-08 64689/week @ 2024-10-15 65328/week @ 2024-10-22 60283/week @ 2024-10-29 71997/week @ 2024-11-05 56962/week @ 2024-11-12

266,123 downloads per month
Used in 616 crates (343 directly)

MIT/Apache

230KB
5.5K SLoC

log4rs

docs crates.io License: MIT OR Apache-2.0 CI Minimum rustc version

log4rs is a highly configurable logging framework modeled after Java's Logback and log4j libraries.

Warning

If you are using the file rotation in your configuration there is a known substantial performance issue so listen up! By default the gzip feature is enabled and when rolling files it will zip log archives automatically. This is a problem when the log archives are large as the zip happens in the main thread and will halt the process while the zip is completed. Be advised that the gzip feature will be removed from default features as of 1.0.

The methods to mitigate this are as follows.

  1. Use the background_rotation feature which spawns an os thread to do the compression.
  2. Disable the gzip feature with --no-default-features.
  3. Ensure the archives are small enough that the compression time is acceptable.

For more information see the PR that added background_rotation.

Quick Start

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:

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

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

    info!("booting up");

    // ...
}

Rust Version Requirements

1.69

Building for Dev

  • Run the tests: cargo test --all-features
  • Run the tests for windows with cross: cross test --target x86_64-pc-windows-gnu
  • Run the tests for all individual features: ./test.sh
  • Run the tests for all individual features for windows with cross: ./test.sh win

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.6–7MB
~49K SLoC