85 lines
13 KiB
HTML
85 lines
13 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="`Span` and `Event` key-value data."><title>tracing_core::field - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="tracing_core" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.css"></noscript><link rel="icon" href="https://raw.githubusercontent.com/tokio-rs/tracing/main/assets/favicon.ico"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Module field</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../tracing_core/index.html"><img src="https://raw.githubusercontent.com/tokio-rs/tracing/main/assets/logo-type.png" alt="logo"></a><h2><a href="../../tracing_core/index.html">tracing_<wbr>core</a><span class="version">0.1.36</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Module field</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#values-and-subscribers" title="`Value`s and `Subscriber`s"><code>Value</code>s and <code>Subscriber</code>s</a></li><li><a href="#using-valuable" title="Using `valuable`">Using <code>valuable</code></a></li></ul><h3><a href="#structs">Module Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#traits" title="Traits">Traits</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="../index.html">In crate tracing_<wbr>core</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">tracing_core</a></div><h1>Module <span>field</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/tracing_core/field.rs.html#1-1372">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><code>Span</code> and <code>Event</code> key-value data.</p>
|
||
<p>Spans and events may be annotated with key-value data, known as <em>fields</em>.
|
||
These fields consist of a mapping from a key (corresponding to a <code>&str</code> but
|
||
represented internally as an array index) to a <a href="trait.Value.html" title="trait tracing_core::field::Value"><code>Value</code></a>.</p>
|
||
<h2 id="values-and-subscribers"><a class="doc-anchor" href="#values-and-subscribers">§</a><code>Value</code>s and <code>Subscriber</code>s</h2>
|
||
<p><code>Subscriber</code>s consume <code>Value</code>s as fields attached to <a href="../span/index.html" title="mod tracing_core::span">span</a>s or <a href="../struct.Event.html" title="struct tracing_core::Event"><code>Event</code></a>s.
|
||
The set of field keys on a given span or event is defined on its <a href="../struct.Metadata.html" title="struct tracing_core::Metadata"><code>Metadata</code></a>.
|
||
When a span is created, it provides <a href="../span/struct.Attributes.html" title="struct tracing_core::span::Attributes"><code>Attributes</code></a> to the <code>Subscriber</code>’s
|
||
<a href="../trait.Subscriber.html#tymethod.new_span" title="method tracing_core::Subscriber::new_span"><code>new_span</code></a> method, containing any fields whose values were provided when
|
||
the span was created; and may call the <code>Subscriber</code>’s <a href="../span/struct.Record.html" title="struct tracing_core::span::Record"><code>record</code></a> method
|
||
with additional <a href="../span/struct.Record.html" title="struct tracing_core::span::Record"><code>Record</code></a>s if values are added for more of its fields.
|
||
Similarly, the <a href="../struct.Event.html" title="struct tracing_core::Event"><code>Event</code></a> type passed to the subscriber’s <a href="../struct.Event.html" title="struct tracing_core::Event"><code>event</code></a> method
|
||
will contain any fields attached to each event.</p>
|
||
<p><code>tracing</code> represents values as either one of a set of Rust primitives
|
||
(<code>i64</code>, <code>u64</code>, <code>f64</code>, <code>i128</code>, <code>u128</code>, <code>bool</code>, and <code>&str</code>) or using a
|
||
<code>fmt::Display</code> or <code>fmt::Debug</code> implementation. <code>Subscriber</code>s are provided
|
||
these primitive value types as <code>dyn Value</code> trait objects.</p>
|
||
<p>These trait objects can be formatted using <code>fmt::Debug</code>, but may also be
|
||
recorded as typed data by calling the <a href="trait.Value.html#tymethod.record" title="method tracing_core::field::Value::record"><code>Value::record</code></a> method on these
|
||
trait objects with a <em>visitor</em> implementing the <a href="trait.Visit.html" title="trait tracing_core::field::Visit"><code>Visit</code></a> trait. This trait
|
||
represents the behavior used to record values of various types. For example,
|
||
an implementation of <code>Visit</code> might record integers by incrementing counters
|
||
for their field names rather than printing them.</p>
|
||
<h2 id="using-valuable"><a class="doc-anchor" href="#using-valuable">§</a>Using <code>valuable</code></h2>
|
||
<p><code>tracing</code>’s <a href="trait.Value.html" title="trait tracing_core::field::Value"><code>Value</code></a> trait is intentionally minimalist: it supports only a small
|
||
number of Rust primitives as typed values, and only permits recording
|
||
user-defined types with their <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug"><code>fmt::Debug</code></a> or <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display"><code>fmt::Display</code></a>
|
||
implementations. However, there are some cases where it may be useful to record
|
||
nested values (such as arrays, <code>Vec</code>s, or <code>HashMap</code>s containing values), or
|
||
user-defined <code>struct</code> and <code>enum</code> types without having to format them as
|
||
unstructured text.</p>
|
||
<p>To address <code>Value</code>’s limitations, <code>tracing</code> offers experimental support for
|
||
the <a href="https://crates.io/crates/valuable"><code>valuable</code></a> crate, which provides object-safe inspection of structured
|
||
values. User-defined types can implement the [<code>valuable::Valuable</code>] trait,
|
||
and be recorded as a <code>tracing</code> field by calling their <a href="valuable::Valuable::as_value"><code>as_value</code></a> method.
|
||
If the <a href="../trait.Subscriber.html" title="trait tracing_core::Subscriber"><code>Subscriber</code></a> also supports the <code>valuable</code> crate, it can
|
||
then visit those types fields as structured values using <code>valuable</code>.</p>
|
||
<pre class="ignore" style="white-space:normal;font:inherit;">
|
||
<strong>Note</strong>: <code>valuable</code> support is an
|
||
<a href = "../index.html#unstable-features">unstable feature</a>. See
|
||
the documentation on unstable features for details on how to enable it.
|
||
</pre>
|
||
<p>For example:</p>
|
||
|
||
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="comment">// Derive `Valuable` for our types:
|
||
</span><span class="kw">use </span>valuable::Valuable;
|
||
|
||
<span class="attr">#[derive(Clone, Debug, Valuable)]
|
||
</span><span class="kw">struct </span>User {
|
||
name: String,
|
||
age: u32,
|
||
address: Address,
|
||
}
|
||
|
||
<span class="attr">#[derive(Clone, Debug, Valuable)]
|
||
</span><span class="kw">struct </span>Address {
|
||
country: String,
|
||
city: String,
|
||
street: String,
|
||
}
|
||
|
||
<span class="kw">let </span>user = User {
|
||
name: <span class="string">"Arwen Undomiel"</span>.to_string(),
|
||
age: <span class="number">3000</span>,
|
||
address: Address {
|
||
country: <span class="string">"Middle Earth"</span>.to_string(),
|
||
city: <span class="string">"Rivendell"</span>.to_string(),
|
||
street: <span class="string">"leafy lane"</span>.to_string(),
|
||
},
|
||
};
|
||
|
||
<span class="comment">// Recording `user` as a `valuable::Value` will allow the `tracing` subscriber
|
||
// to traverse its fields as a nested, typed structure:
|
||
</span><span class="macro">tracing::info!</span>(current_user = user.as_value());</code></pre></div>
|
||
<p>Alternatively, the [<code>valuable()</code>] function may be used to convert a type
|
||
implementing <a href="https://crates.io/crates/valuable"><code>Valuable</code></a> into a <code>tracing</code> field value.</p>
|
||
<p>When the <code>valuable</code> feature is enabled, the <a href="trait.Visit.html" title="trait tracing_core::field::Visit"><code>Visit</code></a> trait will include an
|
||
optional <a href="Visit::record_value"><code>record_value</code></a> method. <code>Visit</code> implementations that wish to
|
||
record <code>valuable</code> values can implement this method with custom behavior.
|
||
If a visitor does not implement <code>record_value</code>, the [<code>valuable::Value</code>] will
|
||
be forwarded to the visitor’s <a href="trait.Visit.html#tymethod.record_debug" title="method tracing_core::field::Visit::record_debug"><code>record_debug</code></a> method.</p>
|
||
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.DebugValue.html" title="struct tracing_core::field::DebugValue">Debug<wbr>Value</a></dt><dd>A <code>Value</code> which serializes as a string using <code>fmt::Debug</code>.</dd><dt><a class="struct" href="struct.DisplayValue.html" title="struct tracing_core::field::DisplayValue">Display<wbr>Value</a></dt><dd>A <code>Value</code> which serializes using <code>fmt::Display</code>.</dd><dt><a class="struct" href="struct.Empty.html" title="struct tracing_core::field::Empty">Empty</a></dt><dd>An empty field.</dd><dt><a class="struct" href="struct.Field.html" title="struct tracing_core::field::Field">Field</a></dt><dd>An opaque key allowing <em>O</em>(1) access to a field in a <code>Span</code>’s key-value
|
||
data.</dd><dt><a class="struct" href="struct.FieldSet.html" title="struct tracing_core::field::FieldSet">Field<wbr>Set</a></dt><dd>Describes the fields present on a span.</dd><dt><a class="struct" href="struct.Iter.html" title="struct tracing_core::field::Iter">Iter</a></dt><dd>An iterator over a set of fields.</dd><dt><a class="struct" href="struct.ValueSet.html" title="struct tracing_core::field::ValueSet">Value<wbr>Set</a></dt><dd>A set of fields and values for a span.</dd></dl><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><dl class="item-table"><dt><a class="trait" href="trait.Value.html" title="trait tracing_core::field::Value">Value</a></dt><dd>A field value of an erased type.</dd><dt><a class="trait" href="trait.Visit.html" title="trait tracing_core::field::Visit">Visit</a></dt><dd>Visits typed values.</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.debug.html" title="fn tracing_core::field::debug">debug</a></dt><dd>Wraps a type implementing <code>fmt::Debug</code> as a <code>Value</code> that can be
|
||
recorded using its <code>Debug</code> implementation.</dd><dt><a class="fn" href="fn.display.html" title="fn tracing_core::field::display">display</a></dt><dd>Wraps a type implementing <code>fmt::Display</code> as a <code>Value</code> that can be
|
||
recorded using its <code>Display</code> implementation.</dd></dl></section></div></main></body></html> |