193 lines
19 KiB
HTML
193 lines
19 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="API documentation for the Rust `env_logger` crate."><meta name="keywords" content="rust, rustlang, rust-lang, env_logger"><title>env_logger - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="https://www.rust-lang.org/static/images/favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../env_logger/index.html'><div class='logo-container'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo'></div></a><p class='location'>Crate env_logger</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all env_logger's items</p></a><div class="block items"><ul><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'env_logger', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../src/env_logger/lib.rs.html#11-1173' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>env_logger</a></span></h1><div class='docblock'><p>A simple logger configured via environment variables which writes
|
||
to stdout or stderr, for use with the logging facade exposed by the
|
||
<a href="https://docs.rs/log/"><code>log</code> crate</a>.</p>
|
||
<h2 id="example" class="section-header"><a href="#example">Example</a></h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="attribute">#[<span class="ident">macro_use</span>]</span> <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">log</span>;
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">env_logger</span>;
|
||
|
||
<span class="kw">use</span> <span class="ident">log</span>::<span class="ident">Level</span>;
|
||
|
||
<span class="kw">fn</span> <span class="ident">main</span>() {
|
||
<span class="ident">env_logger</span>::<span class="ident">init</span>();
|
||
|
||
<span class="macro">debug</span><span class="macro">!</span>(<span class="string">"this is a debug {}"</span>, <span class="string">"message"</span>);
|
||
<span class="macro">error</span><span class="macro">!</span>(<span class="string">"this is printed by default"</span>);
|
||
|
||
<span class="kw">if</span> <span class="macro">log_enabled</span><span class="macro">!</span>(<span class="ident">Level</span>::<span class="ident">Info</span>) {
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">3</span> <span class="op">*</span> <span class="number">4</span>; <span class="comment">// expensive computation</span>
|
||
<span class="macro">info</span><span class="macro">!</span>(<span class="string">"the answer was: {}"</span>, <span class="ident">x</span>);
|
||
}
|
||
}</pre></div>
|
||
<p>Assumes the binary is <code>main</code>:</p>
|
||
<pre><code class="language-{.bash}">$ RUST_LOG=error ./main
|
||
[2017-11-09T02:12:24Z ERROR main] this is printed by default
|
||
</code></pre>
|
||
<pre><code class="language-{.bash}">$ RUST_LOG=info ./main
|
||
[2017-11-09T02:12:24Z ERROR main] this is printed by default
|
||
[2017-11-09T02:12:24Z INFO main] the answer was: 12
|
||
</code></pre>
|
||
<pre><code class="language-{.bash}">$ RUST_LOG=debug ./main
|
||
[2017-11-09T02:12:24Z DEBUG main] this is a debug message
|
||
[2017-11-09T02:12:24Z ERROR main] this is printed by default
|
||
[2017-11-09T02:12:24Z INFO main] the answer was: 12
|
||
</code></pre>
|
||
<p>You can also set the log level on a per module basis:</p>
|
||
<pre><code class="language-{.bash}">$ RUST_LOG=main=info ./main
|
||
[2017-11-09T02:12:24Z ERROR main] this is printed by default
|
||
[2017-11-09T02:12:24Z INFO main] the answer was: 12
|
||
</code></pre>
|
||
<p>And enable all logging:</p>
|
||
<pre><code class="language-{.bash}">$ RUST_LOG=main ./main
|
||
[2017-11-09T02:12:24Z DEBUG main] this is a debug message
|
||
[2017-11-09T02:12:24Z ERROR main] this is printed by default
|
||
[2017-11-09T02:12:24Z INFO main] the answer was: 12
|
||
</code></pre>
|
||
<p>If the binary name contains hyphens, you will need to replace
|
||
them with underscores:</p>
|
||
<pre><code class="language-{.bash}">$ RUST_LOG=my_app ./my-app
|
||
[2017-11-09T02:12:24Z DEBUG my_app] this is a debug message
|
||
[2017-11-09T02:12:24Z ERROR my_app] this is printed by default
|
||
[2017-11-09T02:12:24Z INFO my_app] the answer was: 12
|
||
</code></pre>
|
||
<p>This is because Rust modules and crates cannot contain hyphens
|
||
in their name, although <code>cargo</code> continues to accept them.</p>
|
||
<p>See the documentation for the <a href="https://docs.rs/log/"><code>log</code> crate</a> for more
|
||
information about its API.</p>
|
||
<h2 id="enabling-logging" class="section-header"><a href="#enabling-logging">Enabling logging</a></h2>
|
||
<p>Log levels are controlled on a per-module basis, and by default all logging
|
||
is disabled except for <code>error!</code>. Logging is controlled via the <code>RUST_LOG</code>
|
||
environment variable. The value of this environment variable is a
|
||
comma-separated list of logging directives. A logging directive is of the
|
||
form:</p>
|
||
<pre><code class="language-text">path::to::module=level
|
||
</code></pre>
|
||
<p>The path to the module is rooted in the name of the crate it was compiled
|
||
for, so if your program is contained in a file <code>hello.rs</code>, for example, to
|
||
turn on logging for this file you would use a value of <code>RUST_LOG=hello</code>.
|
||
Furthermore, this path is a prefix-search, so all modules nested in the
|
||
specified module will also have logging enabled.</p>
|
||
<p>The actual <code>level</code> is optional to specify. If omitted, all logging will
|
||
be enabled. If specified, it must be one of the strings <code>debug</code>, <code>error</code>,
|
||
<code>info</code>, <code>warn</code>, or <code>trace</code>.</p>
|
||
<p>As the log level for a module is optional, the module to enable logging for
|
||
is also optional. If only a <code>level</code> is provided, then the global log
|
||
level for all modules is set to this value.</p>
|
||
<p>Some examples of valid values of <code>RUST_LOG</code> are:</p>
|
||
<ul>
|
||
<li><code>hello</code> turns on all logging for the 'hello' module</li>
|
||
<li><code>info</code> turns on all info logging</li>
|
||
<li><code>hello=debug</code> turns on debug logging for 'hello'</li>
|
||
<li><code>hello,std::option</code> turns on hello, and std's option logging</li>
|
||
<li><code>error,hello=warn</code> turn on global error logging and also warn for hello</li>
|
||
</ul>
|
||
<h2 id="filtering-results" class="section-header"><a href="#filtering-results">Filtering results</a></h2>
|
||
<p>A <code>RUST_LOG</code> directive may include a regex filter. The syntax is to append <code>/</code>
|
||
followed by a regex. Each message is checked against the regex, and is only
|
||
logged if it matches. Note that the matching is done after formatting the
|
||
log string but before adding any logging meta-data. There is a single filter
|
||
for all modules.</p>
|
||
<p>Some examples:</p>
|
||
<ul>
|
||
<li><code>hello/foo</code> turns on all logging for the 'hello' module where the log
|
||
message includes 'foo'.</li>
|
||
<li><code>info/f.o</code> turns on all info logging where the log message includes 'foo',
|
||
'f1o', 'fao', etc.</li>
|
||
<li><code>hello=debug/foo*foo</code> turns on debug logging for 'hello' where the log
|
||
message includes 'foofoo' or 'fofoo' or 'fooooooofoo', etc.</li>
|
||
<li><code>error,hello=warn/[0-9]scopes</code> turn on global error logging and also
|
||
warn for hello. In both cases the log message must include a single digit
|
||
number followed by 'scopes'.</li>
|
||
</ul>
|
||
<h2 id="capturing-logs-in-tests" class="section-header"><a href="#capturing-logs-in-tests">Capturing logs in tests</a></h2>
|
||
<p>Records logged during <code>cargo test</code> will not be captured by the test harness by default.
|
||
The <a href="struct.Builder.html#method.is_test"><code>Builder::is_test</code></a> method can be used in unit tests to ensure logs will be captured:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span>
|
||
<span class="kw">mod</span> <span class="ident">tests</span> {
|
||
<span class="kw">fn</span> <span class="ident">init</span>() {
|
||
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="ident">env_logger</span>::<span class="ident">builder</span>().<span class="ident">is_test</span>(<span class="bool-val">true</span>).<span class="ident">try_init</span>();
|
||
}
|
||
|
||
<span class="attribute">#[<span class="ident">test</span>]</span>
|
||
<span class="kw">fn</span> <span class="ident">it_works</span>() {
|
||
<span class="ident">init</span>();
|
||
|
||
<span class="macro">info</span><span class="macro">!</span>(<span class="string">"This record will be captured by `cargo test`"</span>);
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">2</span>, <span class="number">1</span> <span class="op">+</span> <span class="number">1</span>);
|
||
}
|
||
}</pre></div>
|
||
<p>Enabling test capturing comes at the expense of color and other style support
|
||
and may have performance implications.</p>
|
||
<h2 id="disabling-colors" class="section-header"><a href="#disabling-colors">Disabling colors</a></h2>
|
||
<p>Colors and other styles can be configured with the <code>RUST_LOG_STYLE</code>
|
||
environment variable. It accepts the following values:</p>
|
||
<ul>
|
||
<li><code>auto</code> (default) will attempt to print style characters, but don't force the issue.
|
||
If the console isn't available on Windows, or if TERM=dumb, for example, then don't print colors.</li>
|
||
<li><code>always</code> will always print style characters even if they aren't supported by the terminal.
|
||
This includes emitting ANSI colors on Windows if the console API is unavailable.</li>
|
||
<li><code>never</code> will never print style characters.</li>
|
||
</ul>
|
||
<h2 id="tweaking-the-default-format" class="section-header"><a href="#tweaking-the-default-format">Tweaking the default format</a></h2>
|
||
<p>Parts of the default format can be excluded from the log output using the <a href="struct.Builder.html"><code>Builder</code></a>.
|
||
The following example excludes the timestamp from the log output:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="ident">env_logger</span>::<span class="ident">builder</span>()
|
||
.<span class="ident">default_format_timestamp</span>(<span class="bool-val">false</span>)
|
||
.<span class="ident">init</span>();</pre></div>
|
||
<h3 id="stability-of-the-default-format" class="section-header"><a href="#stability-of-the-default-format">Stability of the default format</a></h3>
|
||
<p>The default format won't optimise for long-term stability, and explicitly makes no
|
||
guarantees about the stability of its output across major, minor or patch version
|
||
bumps during <code>0.x</code>.</p>
|
||
<p>If you want to capture or interpret the output of <code>env_logger</code> programmatically
|
||
then you should use a custom format.</p>
|
||
<h3 id="using-a-custom-format" class="section-header"><a href="#using-a-custom-format">Using a custom format</a></h3>
|
||
<p>Custom formats can be provided as closures to the <a href="struct.Builder.html"><code>Builder</code></a>.
|
||
These closures take a [<code>Formatter</code>] and <code>log::Record</code> as arguments:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">Write</span>;
|
||
|
||
<span class="ident">env_logger</span>::<span class="ident">builder</span>()
|
||
.<span class="ident">format</span>(<span class="op">|</span><span class="ident">buf</span>, <span class="ident">record</span><span class="op">|</span> {
|
||
<span class="macro">writeln</span><span class="macro">!</span>(<span class="ident">buf</span>, <span class="string">"{}: {}"</span>, <span class="ident">record</span>.<span class="ident">level</span>(), <span class="ident">record</span>.<span class="ident">args</span>())
|
||
})
|
||
.<span class="ident">init</span>();</pre></div>
|
||
<p>See the <a href="fmt/index.html"><code>fmt</code></a> module for more details about custom formats.</p>
|
||
<h2 id="specifying-defaults-for-environment-variables" class="section-header"><a href="#specifying-defaults-for-environment-variables">Specifying defaults for environment variables</a></h2>
|
||
<p><code>env_logger</code> can read configuration from environment variables.
|
||
If these variables aren't present, the default value to use can be tweaked with the <a href="struct.Env.html"><code>Env</code></a> type.
|
||
The following example defaults to log <code>warn</code> and above if the <code>RUST_LOG</code> environment variable
|
||
isn't set:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">env_logger</span>::<span class="ident">Env</span>;
|
||
|
||
<span class="ident">env_logger</span>::<span class="ident">from_env</span>(<span class="ident">Env</span>::<span class="ident">default</span>().<span class="ident">default_filter_or</span>(<span class="string">"warn"</span>)).<span class="ident">init</span>();</pre></div>
|
||
</div><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
|
||
<table><tr class='module-item'><td><a class="mod" href="filter/index.html" title='env_logger::filter mod'>filter</a></td><td class='docblock-short'><p>Filtering for log records.</p>
|
||
</td></tr><tr class='module-item'><td><a class="mod" href="fmt/index.html" title='env_logger::fmt mod'>fmt</a></td><td class='docblock-short'><p>Formatting for log records.</p>
|
||
</td></tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
||
<table><tr class='module-item'><td><a class="struct" href="struct.Builder.html" title='env_logger::Builder struct'>Builder</a></td><td class='docblock-short'><p><code>Builder</code> acts as builder for initializing a <code>Logger</code>.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Env.html" title='env_logger::Env struct'>Env</a></td><td class='docblock-short'><p>Set of environment variables to configure from.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Logger.html" title='env_logger::Logger struct'>Logger</a></td><td class='docblock-short'><p>The env logger.</p>
|
||
</td></tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
|
||
<table><tr class='module-item'><td><a class="enum" href="enum.Target.html" title='env_logger::Target enum'>Target</a></td><td class='docblock-short'><p>Log target, either <code>stdout</code> or <code>stderr</code>.</p>
|
||
</td></tr><tr class='module-item'><td><a class="enum" href="enum.WriteStyle.html" title='env_logger::WriteStyle enum'>WriteStyle</a></td><td class='docblock-short'><p>Whether or not to print styles to the target.</p>
|
||
</td></tr></table><h2 id='constants' class='section-header'><a href="#constants">Constants</a></h2>
|
||
<table><tr class='module-item'><td><a class="constant" href="constant.DEFAULT_FILTER_ENV.html" title='env_logger::DEFAULT_FILTER_ENV constant'>DEFAULT_FILTER_ENV</a></td><td class='docblock-short'><p>The default name for the environment variable to read filters from.</p>
|
||
</td></tr><tr class='module-item'><td><a class="constant" href="constant.DEFAULT_WRITE_STYLE_ENV.html" title='env_logger::DEFAULT_WRITE_STYLE_ENV constant'>DEFAULT_WRITE_STYLE_ENV</a></td><td class='docblock-short'><p>The default name for the environment variable to read style preferences from.</p>
|
||
</td></tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
|
||
<table><tr class='module-item'><td><a class="fn" href="fn.builder.html" title='env_logger::builder fn'>builder</a></td><td class='docblock-short'><p>Create a new builder with the default environment variables.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.from_env.html" title='env_logger::from_env fn'>from_env</a></td><td class='docblock-short'><p>Create a builder from the given environment variables.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.init.html" title='env_logger::init fn'>init</a></td><td class='docblock-short'><p>Initializes the global logger with an env logger.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.init_from_env.html" title='env_logger::init_from_env fn'>init_from_env</a></td><td class='docblock-short'><p>Initializes the global logger with an env logger from the given environment
|
||
variables.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.try_init.html" title='env_logger::try_init fn'>try_init</a></td><td class='docblock-short'><p>Attempts to initialize the global logger with an env logger.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.try_init_from_env.html" title='env_logger::try_init_from_env fn'>try_init_from_env</a></td><td class='docblock-short'><p>Attempts to initialize the global logger with an env logger from the given
|
||
environment variables.</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../";window.currentCrate = "env_logger";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |