#error #razor #info

log-once

Collection of helper macros for logging some events only once

8 releases

0.4.1 Jan 4, 2024
0.4.0 Sep 10, 2022
0.3.1 Oct 13, 2019
0.3.0 Dec 11, 2018
0.1.0 Nov 25, 2016

#70 in Debugging

Download history 15330/week @ 2024-12-14 4989/week @ 2024-12-21 5185/week @ 2024-12-28 13067/week @ 2025-01-04 13832/week @ 2025-01-11 10675/week @ 2025-01-18 10684/week @ 2025-01-25 12400/week @ 2025-02-01 10762/week @ 2025-02-08 11037/week @ 2025-02-15 11666/week @ 2025-02-22 16361/week @ 2025-03-01 19687/week @ 2025-03-08 18017/week @ 2025-03-15 17762/week @ 2025-03-22 13681/week @ 2025-03-29

72,339 downloads per month
Used in 111 crates (4 directly)

MIT/Apache

13KB
125 lines

log-once

Tests Documentation Crates.io version

Collection of helper macros for logging some events only once.

This crate provide macro in the log_once family (warn_once!, trace_once!, ...); that only send a logging event once for every message. It rely and uses the logging infrastructure in the log crate; and is fully compatible with any logger implementation.

These macro will store the already seen messages in a BTreeSet, and check if a message is in the set before sending the log event.

Usage

  1. Add it to your Cargo.toml file:
[dependencies]
log-once = "0.4"
  1. import the macros:
use log_once::{warn_once, log_once};
  1. Enjoy!

Examples

use log::info;
use log_once::{info_once, warn_once};

pub fn shave_the_yak(yaks: &[Yak]) {
    for yak in yaks {
        info!(target: "yak_events", "Commencing yak shaving for {yak:?}");

        loop {
            match find_a_razor() {
                Ok(razor) => {
                    // This will only appear once in the logger output for each razor
                    info_once!("Razor located: {razor}");
                    yak.shave(razor);
                    break;
                }
                Err(err) => {
                    // This will only appear once in the logger output for each error
                    warn_once!("Unable to locate a razor: {err}, retrying");
                }
            }
        }
    }
}

License

log-once is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.

Dependencies

~90KB