Skip to main content

warn

Macro warn 

Source
macro_rules! warn {
    ( ratelimit=$limit:literal / $unit:tt ; $($args:tt)+ ) => { ... };
    ( $($args:tt)+ ) => { ... };
}
Available on (crate features logging or metrics or tracing or memory-profiling or telemetry-server) and crate feature logging only.
Expand description

Log warning level record.

If duplicate fields are specified for the record then the last one takes precedence and overwrites the value of the previous one.

Certain added fields may not be present in the resulting logs if LoggingSettings::redact_keys is used.

ยงExamples

use foundations::telemetry::TelemetryContext;
use foundations::telemetry::log::{self, TestLogRecord};
use foundations::telemetry::settings::Level;

// Test context is used for demonstration purposes to show the resulting log records.
let ctx = TelemetryContext::test();
let _scope = ctx.scope();

// Simple log message
log::warn!("Hello world!");

// Macro also accepts format arguments
log::warn!("The values are: {}, {}", 42, true);

// Fields key-value pairs can be added to log record, by separating the format message
// and fields by `;`.
log::warn!("Answer: {}", 42; "foo" => "bar", "baz" => 1337);

// Log messages can be rate-limited with an extra keyword
for _ in 0..2 {
    log::warn!(ratelimit=1/h; "Answer: {}", 42; "foo" => "bar");
}

assert_eq!(*ctx.log_records(), &[
    TestLogRecord {
        level: Level::Warning,
        message: "Hello world!".into(),
        fields: vec![]
    },
    TestLogRecord {
        level: Level::Warning,
        message: "The values are: 42, true".into(),
        fields: vec![]
    },
    TestLogRecord {
        level: Level::Warning,
        message: "Answer: 42".into(),
        fields: vec![
            ("baz".into(), "1337".into()),
            ("foo".into(), "bar".into())
        ]
    },
    TestLogRecord {
        level: Level::Warning,
        message: "Answer: 42".into(),
        fields: vec![
            ("foo".into(), "bar".into())
        ]
    }
]);