[−][src]Macro tracing::event
Constructs a new Event
.
The event macro is invoked with a Level
and up to 32 key-value fields.
Optionally, a format string and arguments may follow the fields; this will
be used to construct an implicit field named "message".
Examples
use tracing::{event, Level}; let data = (42, "fourty-two"); let private_data = "private"; let error = "a bad error"; event!(Level::ERROR, %error, "Received error"); event!( target: "app_events", Level::WARN, private_data, ?data, "App warning: {}", error ); event!(Level::INFO, the_answer = data.0);
Note that unlike span!
, event!
requires a value for all fields. As
events are recorded immediately when the macro is invoked, there is no
opportunity for fields to be recorded later. A trailing comma on the final
field is valid.
For example, the following does not compile:
ⓘThis example deliberately fails to compile
event!(Level::INFO, foo = 5, bad_field, bar = "hello") #}
Shorthand for field::debug
:
#[derive(Debug)] struct MyStruct { field: &'static str, } let my_struct = MyStruct { field: "Hello world!" }; // `my_struct` will be recorded using its `fmt::Debug` implementation. event!(Level::TRACE, ?my_struct);
Shorthand for field::display
:
// `my_struct.field` will be recorded using its `fmt::Display` implementation. event!(Level::TRACE, %my_struct.field);
Events may have up to 32 fields. The following will not compile:
ⓘThis example deliberately fails to compile
event!(Level::INFO, a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9, j = 10, k = 11, l = 12, m = 13, n = 14, o = 15, p = 16, q = 17, r = 18, s = 19, t = 20, u = 21, v = 22, w = 23, x = 24, y = 25, z = 26, aa = 27, bb = 28, cc = 29, dd = 30, ee = 31, ff = 32, gg = 33 );