<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metaname="generator"content="rustdoc"><metaname="description"content="A type that can format a tracing `Event` to a `Writer`."><title>FormatEvent in tracing_subscriber::fmt::format - Rust</title><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../../../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../../../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../../../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../../../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../../../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><linkrel="stylesheet"href="../../../static.files/normalize-76eba96aa4d2e634.css"><linkrel="stylesheet"href="../../../static.files/rustdoc-5bc39a1768837dd0.css"><metaname="rustdoc-vars"data-root-path="../../../"data-static-root-path="../../../static.files/"data-current-crate="tracing_subscriber"data-themes=""data-resource-suffix=""data-rustdoc-version="1.77.2 (25ef9e3d8 2024-04-09)"data-channel="1.77.2"data-search-js="search-dd67cee4cfa65049.js"data-settings-js="settings-4313503d2e1961c2.js"><scriptsrc="../../../static.files/storage-4c98445ec4002617.js"></script><scriptdefersrc="sidebar-items.js"></script><scriptdefersrc="../../../static.files/main-48f368f3872407c8.js"></script><noscript><linkrel="stylesheet"href="../../../static.files/noscript-04d5337699b92874.css"></noscript><linkrel="alternate icon"type="image/png"href="../../../static.files/favicon-16x16-8b506e7a72182f1c.png"><linkrel="alternate icon"type="image/png"href="../../../static.files/favicon-32x32-422f7d1d52889060.png"><linkrel="icon"type="image/svg+xml"href="../../../static.files/favicon-2c020d218678b618.svg"></head><bodyclass="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><navclass="mobile-topbar"><buttonclass="sidebar-menu-toggle"title="show sidebar"></button><aclass="logo-container"href="../../../tracing_subscriber/index.html"><imgsrc="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png"alt=""></a></nav><navclass="sidebar"><divclass="sidebar-crate"><aclass="logo-container"href="../../../tracing_subscriber/index.html"><imgsrc="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png"alt="logo"></a><h2><ahref="../../../tracing_subscriber/index.html">tracing_subscriber</a><spanclass="version">0.3.18</span></h2></div><h2class="location"><ahref="#">FormatEvent</a></h2><divclass="sidebar-elems"><section><h3><ahref="#required-methods">Required Methods</a></h3><ulclass="block"><li><ahref="#tymethod.format_event">format_event</a></li></ul><h3><ahref="#foreign-impls">Implementations on Foreign Types</a></h3><ulclass="block"><li><ahref="#impl-FormatEvent%3CS,+N%3E-for-fn(%26FmtContext%3C'b,+S,+N%3E,+Writer%3C'c%3E,+%26Event%3C'e%3E)+-%3E+Result%3C(),+Error%3E">fn(ctx: &FmtContext<'_, S, N>, _: Writer<'_>, _: &Event<'_>) -> Result</a></li></ul><h3><ahref="#implementors">Implementors</a></h3></section><h2><ahref="index.html">In tracing_subscriber::fmt::format</a></h2></div></nav><divclass="sidebar-resizer"></div>
<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.77.2/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.77.2/core/macro.write.html"title="macro core::write"><code>std::write!</code></a> and <ahref="https://doc.rust-lang.org/1.77.2/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.77.2/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.77.2/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>