mentat/docs/apis/rust/env_logger/index.html

355 lines
19 KiB
HTML
Raw Normal View History

2018-06-21 14:44:35 +00:00
<!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>
<link rel="shortcut icon" href="http://www.rust-lang.org/favicon.ico">
</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">&#9776;</div>
<a href='../env_logger/index.html'><img src='http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a>
<p class='location'>Crate env_logger</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#reexports">Re-exports</a></li><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</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 js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content"><h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>env_logger</a></span><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'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../src/env_logger/lib.rs.html#11-1099' title='goto source code'>[src]</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>
<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">&quot;this is a debug {}&quot;</span>, <span class="string">&quot;message&quot;</span>);
<span class="macro">error</span><span class="macro">!</span>(<span class="string">&quot;this is printed by default&quot;</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">&quot;the answer was: {}&quot;</span>, <span class="ident">x</span>);
}
}</pre>
<p>Assumes the binary is <code>main</code>:</p>
<pre><code class="language-{.bash}">$ RUST_LOG=error ./main
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
</code></pre>
<pre><code class="language-{.bash}">$ RUST_LOG=info ./main
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
INFO: 2017-11-09T02:12:24Z: main: the answer was: 12
</code></pre>
<pre><code class="language-{.bash}">$ RUST_LOG=debug ./main
DEBUG: 2017-11-09T02:12:24Z: main: this is a debug message
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
INFO: 2017-11-09T02:12:24Z: 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
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
INFO: 2017-11-09T02:12:24Z: main: the answer was: 12
</code></pre>
<p>And enable all logging:</p>
<pre><code class="language-{.bash}">$ RUST_LOG=main ./main
DEBUG: 2017-11-09T02:12:24Z: main: this is a debug message
ERROR: 2017-11-09T02:12:24Z: main: this is printed by default
INFO: 2017-11-09T02:12:24Z: 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
DEBUG: 2017-11-09T02:12:24Z: my_app: this is a debug message
ERROR: 2017-11-09T02:12:24Z: my_app: this is printed by default
INFO: 2017-11-09T02:12:24Z: 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="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 excluding the timestamp from the log output:</p>
<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">Builder</span>::<span class="ident">from_default_env</span>()
.<span class="ident">default_format_timestamp</span>(<span class="bool-val">false</span>)
.<span class="ident">init</span>();
<span class="macro">debug</span><span class="macro">!</span>(<span class="string">&quot;this is a debug {}&quot;</span>, <span class="string">&quot;message&quot;</span>);
<span class="macro">error</span><span class="macro">!</span>(<span class="string">&quot;this is printed by default&quot;</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">&quot;the answer was: {}&quot;</span>, <span class="ident">x</span>);
}
}</pre>
</div><h2 id='reexports' class='section-header'><a href="#reexports">Re-exports</a></h2>
<table><tr><td><code>pub use self::fmt::<a class="enum" href="../env_logger/fmt/enum.Target.html" title="enum env_logger::fmt::Target">Target</a>;</code></td></tr><tr><td><code>pub use self::fmt::<a class="enum" href="../env_logger/fmt/enum.WriteStyle.html" title="enum env_logger::fmt::WriteStyle">WriteStyle</a>;</code></td></tr><tr><td><code>pub use self::fmt::<a class="enum" href="../env_logger/fmt/enum.Color.html" title="enum env_logger::fmt::Color">Color</a>;</code></td></tr><tr><td><code>pub use self::fmt::<a class="struct" href="../env_logger/fmt/struct.Formatter.html" title="struct env_logger::fmt::Formatter">Formatter</a>;</code></td></tr></table><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='mod env_logger::filter'>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='mod env_logger::fmt'>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='struct env_logger::Builder'>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='struct env_logger::Env'>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='struct env_logger::Logger'>Logger</a></td>
<td class='docblock-short'>
<p>The env logger.</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='constant env_logger::DEFAULT_FILTER_ENV'>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='constant env_logger::DEFAULT_WRITE_STYLE_ENV'>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.init.html"
title='fn env_logger::init'>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='fn env_logger::init_from_env'>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='fn env_logger::try_init'>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='fn env_logger::try_init_from_env'>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>
<aside id="help" class="hidden">
<div>
<h1 class="hidden">Help</h1>
<div class="shortcuts">
<h2>Keyboard Shortcuts</h2>
<dl>
<dt><kbd>?</kbd></dt>
<dd>Show this help dialog</dd>
<dt><kbd>S</kbd></dt>
<dd>Focus the search field</dd>
<dt><kbd></kbd></dt>
<dd>Move up in search results</dd>
<dt><kbd></kbd></dt>
<dd>Move down in search results</dd>
<dt><kbd></kbd></dt>
<dd>Switch tab</dd>
<dt><kbd>&#9166;</kbd></dt>
<dd>Go to active search result</dd>
<dt><kbd>+</kbd></dt>
<dd>Expand all sections</dd>
<dt><kbd>-</kbd></dt>
<dd>Collapse all sections</dd>
</dl>
</div>
<div class="infos">
<h2>Search Tricks</h2>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>type</code>, <code>macro</code>,
and <code>const</code>.
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code> or <code>* -> vec</code>)
</p>
</div>
</div>
</aside>
<script>
window.rootPath = "../";
window.currentCrate = "env_logger";
</script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>