Skip to content

Asking for log_every_n macro #3457

@dentiny

Description

@dentiny

Feature Request

Motivation

Hi team, some other logging libraries (i.e., glog) supports log every N: https://github.com/google/glog/blob/53d58e4531c7c90f71ddab503d915e027432447a/src/glog/logging.h#L962-L963 I'm wondering if we could support that as well.
The main benefit of which is, on heavy logging our logging won't spam the output, and potentially improve the performance as well.

Proposal

macro_rules! log_every_n {
    ($n:expr, $($arg:tt)*) => {
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let count = COUNTER.fetch_add(1, Ordering::Relaxed);
        if count % $n == 0 {
            $($arg)*
        }
    };
}

// Usage
log_every_n!(100, warn!("Reject request due to memory limit exceeded"));

Alternatives

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions