#log #once #logging #macro #events

log-once

Collection of helper macros for logging some events only once

7 unstable releases

0.4.0 Sep 10, 2022
0.3.1 Oct 13, 2019
0.3.0 Dec 11, 2018
0.2.0 Apr 6, 2018
0.1.0 Nov 25, 2016

#170 in Data structures

Download history 607/week @ 2022-07-31 342/week @ 2022-08-07 886/week @ 2022-08-14 1162/week @ 2022-08-21 265/week @ 2022-08-28 438/week @ 2022-09-04 1027/week @ 2022-09-11 960/week @ 2022-09-18 1011/week @ 2022-09-25 1455/week @ 2022-10-02 1513/week @ 2022-10-09 1329/week @ 2022-10-16 1435/week @ 2022-10-23 1390/week @ 2022-10-30 1640/week @ 2022-11-06 1067/week @ 2022-11-13

5,550 downloads per month
Used in 2 crates

MIT/Apache

12KB
126 lines

log-once

Build Status 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.3"
  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

~87KB