<main><divclass="width-limiter"><navclass="sub"><formclass="search-form"><span></span><divid="sidebar-button"tabindex="-1"><ahref="../../../tracing_subscriber/all.html"title="show sidebar"></a></div><inputclass="search-input"name="search"aria-label="Run search in the documentation"autocomplete="off"spellcheck="false"placeholder="Click or press ‘S’ to search, ‘?’ for more options…"type="search"><divid="help-button"tabindex="-1"><ahref="../../../help.html"title="help">?</a></div><divid="settings-menu"tabindex="-1"><ahref="../../../settings.html"title="settings"><imgwidth="22"height="22"alt="Change settings"src="../../../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><sectionid="main-content"class="content"><divclass="main-heading"><h1>Trait <ahref="../../index.html">tracing_subscriber</a>::<wbr><ahref="../index.html">fmt</a>::<wbr><ahref="index.html">format</a>::<wbr><aclass="trait"href="#">FormatEvent</a><buttonid="copy-path"title="Copy item path to clipboard"><imgsrc="../../../static.files/clipboard-7571035ce49a181d.svg"width="19"height="18"alt="Copy item path"></button></h1><spanclass="out-of-band"><aclass="src"href="../../../src/tracing_subscriber/fmt/format/mod.rs.html#195-207">source</a> · <buttonid="toggle-all-docs"title="collapse all docs">[<span>−</span>]</button></span></div><preclass="rust item-decl"><code>pub trait FormatEvent<S, N><divclass="where">where
}</code></pre><detailsclass="toggle top-doc"open><summaryclass="hideme"><span>Expand description</span></summary><divclass="docblock"><p>A type that can format a tracing <ahref="../../../tracing_core/event/struct.Event.html"title="struct tracing_core::event::Event"><code>Event</code></a> to a <ahref="struct.Writer.html"title="struct tracing_subscriber::fmt::format::Writer"><code>Writer</code></a>.</p>
<p><code>FormatEvent</code> is primarily used in the context of <ahref="../struct.Subscriber.html"title="struct tracing_subscriber::fmt::Subscriber"><code>fmt::Subscriber</code></a> or
<ahref="../struct.Layer.html"title="struct tracing_subscriber::fmt::Layer"><code>fmt::Layer</code></a>. Each time an event is dispatched to <ahref="../struct.Subscriber.html"title="struct tracing_subscriber::fmt::Subscriber"><code>fmt::Subscriber</code></a> or
<ahref="../struct.Layer.html"title="struct tracing_subscriber::fmt::Layer"><code>fmt::Layer</code></a>, the subscriber or layer
forwards it to its associated <code>FormatEvent</code> to emit a log message.</p>
<p>This trait is already implemented for function pointers with the same
<p>The following arguments are passed to <code>FormatEvent::format_event</code>:</p>
<ul>
<li>
<p>A <ahref="../struct.FmtContext.html"title="struct tracing_subscriber::fmt::FmtContext"><code>FmtContext</code></a>. This is an extension of the <ahref="../../layer/struct.Context.html"title="struct tracing_subscriber::layer::Context"><code>layer::Context</code></a> type,
which can be used for accessing stored information such as the current
span context an event occurred in.</p>
<p>In addition, <ahref="../struct.FmtContext.html"title="struct tracing_subscriber::fmt::FmtContext"><code>FmtContext</code></a> exposes access to the <ahref="../trait.FormatFields.html"title="trait tracing_subscriber::fmt::FormatFields"><code>FormatFields</code></a>
implementation that the subscriber was configured to use via the
<ahref="../struct.FmtContext.html#method.field_format"title="method tracing_subscriber::fmt::FmtContext::field_format"><code>FmtContext::field_format</code></a> method. This can be used when the
<ahref="../trait.FormatEvent.html"title="trait tracing_subscriber::fmt::FormatEvent"><code>FormatEvent</code></a> implementation needs to format the event’s fields.</p>
<p>For convenience, <ahref="../struct.FmtContext.html"title="struct tracing_subscriber::fmt::FmtContext"><code>FmtContext</code></a> also <ahref="../struct.FmtContext.html#impl-FormatFields%3C'writer%3E"title="struct tracing_subscriber::fmt::FmtContext">implements <code>FormatFields</code></a>,
forwarding to the configured <ahref="../trait.FormatFields.html"title="trait tracing_subscriber::fmt::FormatFields"><code>FormatFields</code></a> type.</p>
</li>
<li>
<p>A <ahref="struct.Writer.html"title="struct tracing_subscriber::fmt::format::Writer"><code>Writer</code></a> to which the formatted representation of the event is
written. This type implements the <ahref="https://doc.rust-lang.org/1.78.0/core/fmt/trait.Write.html"title="trait core::fmt::Write"><code>std::fmt::Write</code></a> trait, and therefore
can be used with the <ahref="https://doc.rust-lang.org/1.78.0/core/macro.write.html"title="macro core::write"><code>std::write!</code></a> and <ahref="https://doc.rust-lang.org/1.78.0/core/macro.writeln.html"title="macro core::writeln"><code>std::writeln!</code></a> macros, as well
as calling <ahref="https://doc.rust-lang.org/1.78.0/core/fmt/trait.Write.html"title="trait core::fmt::Write"><code>std::fmt::Write</code></a> methods directly.</p>
<p>The <ahref="struct.Writer.html"title="struct tracing_subscriber::fmt::format::Writer"><code>Writer</code></a> type also implements additional methods that provide
information about how the event should be formatted. The
escape codes</a> are supported by the underlying I/O writer that the event
will be written to. If this returns <code>true</code>, the formatter is permitted to
use ANSI escape codes to add colors and other text formatting to its
output. If it returns <code>false</code>, the event will be written to an output that
does not support ANSI escape codes (such as a log file), and they should
not be emitted.</p>
<p>Crates like <ahref="https://crates.io/crates/nu_ansi_term"><code>nu_ansi_term</code></a> and <ahref="https://crates.io/crates/owo-colors"><code>owo-colors</code></a> can be used to add ANSI
escape codes to formatted output.</p>
</li>
<li>
<p>The actual <ahref="../../../tracing_core/event/struct.Event.html"title="struct tracing_core::event::Event"><code>Event</code></a> to be formatted.</p>
<p>This example re-implements a simiplified version of this crate’s <ahref="struct.Full.html"title="struct tracing_subscriber::fmt::format::Full">default
<spanclass="macro">tracing::info!</span>(question = <spanclass="string">"life, the universe, and everything"</span>, <spanclass="string">"hello world"</span>);</code></pre></div>
<p>This formatter will print events like this:</p>
<divclass="example-wrap"><preclass="language-text"><code>DEBUG yak_shaving::shaver: some-span{field-on-span=foo}: started shaving yak
) -><aclass="type"href="https://doc.rust-lang.org/1.78.0/core/fmt/type.Result.html"title="type core::fmt::Result">Result</a></h4></section></summary><divclass="docblock"><p>Write a log message for <code>Event</code> in <code>Context</code> to the given <ahref="struct.Writer.html"title="struct tracing_subscriber::fmt::format::Writer"><code>Writer</code></a>.</p>