macro_rules! log {
(target: $target:expr, $lvl:expr, $($rest:tt)+) => { ... };
($lvl:expr, $($rest:tt)+) => { ... };
}Expand description
The standard logging macro.
This macro will generically log with the specified Level and format!
based argument list.
§Examples
use micromegas_tracing::prelude::*;
let data = (42, "Forty-two");
let private_data = "private";
log!(Level::Error, "Received errors: {}, {}", data.0, data.1);
log!(target: "app_events", Level::Warn, "App warning: {}, {}, {}",
data.0, data.1, private_data);Properties go before the format string, as key = value pairs. A key is an identifier or a
string literal. A literal value is interned once per callsite, unless a ..set is spread
without a static value, in which case the literals are copied into the block (the spread
set’s own values are not). Any other value (a request or correlation id, for example) is
copied into the current block, so nothing leaks. A bare value must implement
PropertyValue (strings do); use
%value (Display) or ?value (Debug) for the rest, at the cost of one String
allocation. Dynamic values are only evaluated when the entry is enabled.
use micromegas_tracing::prelude::*;
let correlation_id = String::from("c0ffee");
let rows = 3;
info!(correlation_id = correlation_id, region = "eu", rows = %rows, "fetched {} rows", rows);
info!(target: "app_events", "http.method" = "GET", "request done");A precomputed &'static PropertySet is spread with ..set. It must come last in the
property list, and key = value pairs override its entries:
use micromegas_tracing::prelude::*;
use micromegas_tracing::property_set::{Property, PropertySet};
let set = PropertySet::find_or_create(vec![Property::new("region", "eu")]);
info!(..set, "from a set");
info!(correlation_id = "c0ffee", ..set, "from a set and a pair");key = static expr takes a &'static str chosen at runtime, from a small closed set (a
label or an enum variant name, for example). Each distinct tuple of static values is interned
once per callsite and then looked up, a few nanoseconds more than a ..set spread and far
cheaper than a dynamic value. Every distinct value is kept forever, so the cardinality
contract of PropertySet applies: never use it for ids. It mixes freely with literals,
dynamic values and a trailing ..set. Among literals and static values the last one wins
(case-insensitive), a dynamic value beats both, and any pair overrides a ..set entry. With
..set and a static value in the same call, literals and static values are interned over the
set, so dynamic values beat them whatever the order. A static expression is only evaluated
when the entry is enabled.
use micromegas_tracing::prelude::*;
let label: &'static str = "alpha";
info!(prefix = static label, request = %"id", "fetched");
info!(prefix = static label, kind = "a", ..PropertySet::empty(), "with a set");The value must be 'static; a borrowed string is a compile error:
use micromegas_tracing::prelude::*;
let s = String::from("alpha");
info!(prefix = static s.as_str(), "fetched");static doesn’t combine with % or ?:
use micromegas_tracing::prelude::*;
info!(prefix = static %"x", "fetched");level and target are reserved keys, because analytics treats them as column overrides.
They can still be set through a ..set spread:
use micromegas_tracing::prelude::*;
info!(level = "debug", "message");A literal value goes through concat!, so 1_000 is recorded as “1000” and 0x10 as “16”,
while a float keeps its spelling (0.50 stays “0.50”); %value uses Display instead.
A bare integer needs %:
use micromegas_tracing::prelude::*;
let n = 3_u32;
info!(rows = n, "fetched");A bare negated expression has no PropertyValue impl either:
use micromegas_tracing::prelude::*;
let n = 3_i32;
info!(delta = -n, "fetched");..set can’t be followed by other entries, and appears at most once:
use micromegas_tracing::prelude::*;
use micromegas_tracing::property_set::PropertySet;
let set = PropertySet::empty();
info!(..set, kind = "a", "message");use micromegas_tracing::prelude::*;
use micromegas_tracing::property_set::PropertySet;
let set = PropertySet::empty();
info!(..set, ..set, "message");