Skip to content

Commit

Permalink
Allowing cargo test to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Ultraxime committed Jul 31, 2024
1 parent efe054d commit fffab9c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

**log_tester** is a crate that takes care of capturing log messages produced by the [`log`](https://docs.rs/log) crate during test, and then perform checks on them.

## Warnings

This crate is made to capture all logs, including in multithreaded case.
Hence, it does not work well with `cargo test`, the logs from all test will be captured.

It is better to use `cargo nextest` instead.

Using `cargo test` will not fail but additional logs will be captured. In that way the test may not be right.

## Usage

This crate is intend to be used in conjunction with the [`log`](https://docs.rs/log)
Expand All @@ -19,7 +28,7 @@ log = "0.4"
log_tester = "0.1"
```

```rust, ignore
```rust
use log_tester::LogTester;
use log::Level;

Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use log::{Level, Log, Record};
/// The list of captured logs.
static LOGS: RwLock<Vec<CapturedLog>> = RwLock::new(Vec::new());

static INIT: std::sync::Once = std::sync::Once::new();

/// The logger
pub struct LogTester;

Expand All @@ -49,7 +51,7 @@ impl LogTester {
/// LogTester::start();
/// ```
pub fn start() {
log::set_logger(&LogTester).expect("Failed to start the logger");
INIT.call_once(|| log::set_logger(&LogTester).expect("Failed to start the logger"));
log::set_max_level(log::LevelFilter::Trace);
}

Expand Down

0 comments on commit fffab9c

Please sign in to comment.