aboutsummaryrefslogtreecommitdiff
path: root/log/index.html
blob: 327f216372683b8979506dec0222d16c01f85c97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!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 `log` crate.">
    <meta name="keywords" content="rust, rustlang, rust-lang, log">

    <title>log - Rust</title>

    <link rel="stylesheet" type="text/css" href="../main.css">

    <link rel="shortcut icon" href="http://www.rust-lang.org/favicon.ico">
    
</head>
<body class="rustdoc">
    <!--[if lte IE 8]>
    <div class="warning">
        This old browser is unsupported and will most likely display funky
        things.
    </div>
    <![endif]-->

    

    <section class="sidebar">
        <a href='../log/index.html'><img src='http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='' width='100'></a>
        <p class='location'></p><script>window.sidebarCurrent = {name: 'log', ty: 'mod', relpath: '../'};</script>
    </section>

    <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 mod">
<h1 class='fqn'><span class='in-band'>Crate <a class='mod' href=''>log</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 id='src-0' class='srclink' href='../src/log/lib.rs.html#11-773' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>A lightweight logging facade.</p>

<p>A logging facade provides a single logging API that abstracts over the
actual logging implementation. Libraries can use the logging API provided
by this crate, and the consumer of those libraries can choose the logging
framework that is most suitable for its use case.</p>

<p>If no logging implementation is selected, the facade falls back to a &quot;noop&quot;
implementation that ignores all log messages. The overhead in this case
is very small - just an integer load, comparison and jump.</p>

<p>A log request consists of a target, a level, and a body. A target is a
string which defaults to the module path of the location of the log
request, though that default may be overridden. Logger implementations
typically use the target to filter requests based on some user
configuration.</p>

<h1 id="use" class='section-header'><a
                           href="#use">Use</a></h1>
<h2 id="in-libraries" class='section-header'><a
                           href="#in-libraries">In libraries</a></h2>
<p>Libraries should link only to the <code>log</code> crate, and use the provided
macros to log whatever information will be useful to downstream consumers.</p>

<h3 id="examples" class='section-header'><a
                           href="#examples">Examples</a></h3><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'>pub</span> <span class='kw'>fn</span> <span class='ident'>shave_the_yak</span>(<span class='ident'>yak</span>: <span class='kw-2'>&amp;</span><span class='ident'>Yak</span>) {
    <span class='macro'>info</span><span class='macro'>!</span>(<span class='ident'>target</span>: <span class='string'>&quot;yak_events&quot;</span>, <span class='string'>&quot;Commencing yak shaving for {:?}&quot;</span>, <span class='ident'>yak</span>);

    <span class='kw'>loop</span> {
        <span class='kw'>match</span> <span class='ident'>find_a_razor</span>() {
            <span class='prelude-val'>Ok</span>(<span class='ident'>razor</span>) <span class='op'>=&gt;</span> {
                <span class='macro'>info</span><span class='macro'>!</span>(<span class='string'>&quot;Razor located: {}&quot;</span>, <span class='ident'>razor</span>);
                <span class='ident'>yak</span>.<span class='ident'>shave</span>(<span class='ident'>razor</span>);
                <span class='kw'>break</span>;
            }
            <span class='prelude-val'>Err</span>(<span class='ident'>err</span>) <span class='op'>=&gt;</span> {
                <span class='macro'>warn</span><span class='macro'>!</span>(<span class='string'>&quot;Unable to locate a razor: {}, retrying&quot;</span>, <span class='ident'>err</span>);
            }
        }
    }
}
</pre>

<h2 id="in-executables" class='section-header'><a
                           href="#in-executables">In executables</a></h2>
<p>Executables should chose a logging framework and initialize it early in the
runtime of the program. Logging frameworks will typically include a
function to do this. Any log messages generated before the framework is
initialized will be ignored.</p>

<p>The executable itself may use the <code>log</code> crate to log as well.</p>

<h3 id="warning" class='section-header'><a
                           href="#warning">Warning</a></h3>
<p>The logging system may only be initialized once.</p>

<h3 id="examples-1" class='section-header'><a
                           href="#examples-1">Examples</a></h3><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'>my_logger</span>;

<span class='kw'>fn</span> <span class='ident'>main</span>() {
    <span class='ident'>my_logger</span>::<span class='ident'>init</span>();

    <span class='macro'>info</span><span class='macro'>!</span>(<span class='string'>&quot;starting up&quot;</span>);

    <span class='comment'>// ...</span>
}
</pre>

<h1 id="logger-implementations" class='section-header'><a
                           href="#logger-implementations">Logger implementations</a></h1>
<p>Loggers implement the <code>Log</code> trait. Here&#39;s a very basic example that simply
logs all messages at the <code>Error</code>, <code>Warn</code> or <code>Info</code> levels to stdout:</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>extern</span> <span class='kw'>crate</span> <span class='ident'>log</span>;

<span class='kw'>use</span> <span class='ident'>log</span>::{<span class='ident'>LogRecord</span>, <span class='ident'>LogLevel</span>, <span class='ident'>LogMetadata</span>};

<span class='kw'>struct</span> <span class='ident'>SimpleLogger</span>;

<span class='kw'>impl</span> <span class='ident'>log</span>::<span class='ident'>Log</span> <span class='kw'>for</span> <span class='ident'>SimpleLogger</span> {
    <span class='kw'>fn</span> <span class='ident'>enabled</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>, <span class='ident'>metadata</span>: <span class='kw-2'>&amp;</span><span class='ident'>LogMetadata</span>) <span class='op'>-&gt;</span> <span class='ident'>bool</span> {
        <span class='ident'>metadata</span>.<span class='ident'>level</span>() <span class='op'>&lt;=</span> <span class='ident'>LogLevel</span>::<span class='ident'>Info</span>
    }

    <span class='kw'>fn</span> <span class='ident'>log</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>, <span class='ident'>record</span>: <span class='kw-2'>&amp;</span><span class='ident'>LogRecord</span>) {
        <span class='kw'>if</span> <span class='self'>self</span>.<span class='ident'>enabled</span>(<span class='ident'>record</span>.<span class='ident'>metadata</span>()) {
            <span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{} - {}&quot;</span>, <span class='ident'>record</span>.<span class='ident'>level</span>(), <span class='ident'>record</span>.<span class='ident'>args</span>());
        }
    }
}
</pre>

<p>Loggers are installed by calling the <code>set_logger</code> function. It takes a
closure which is provided a <code>MaxLogLevel</code> token and returns a <code>Log</code> trait
object. The <code>MaxLogLevel</code> token controls the global maximum log level. The
logging facade uses this as an optimization to improve performance of log
messages at levels that are disabled. In the case of our example logger,
we&#39;ll want to set the maximum log level to <code>Info</code>, since we ignore any
<code>Debug</code> or <code>Trace</code> level log messages. A logging framework should provide a
function that wraps a call to <code>set_logger</code>, handling initialization of the
logger:</p>
<pre class='rust rust-example-rendered'>
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>init</span>() <span class='op'>-&gt;</span> <span class='prelude-ty'>Result</span><span class='op'>&lt;</span>(), <span class='ident'>SetLoggerError</span><span class='op'>&gt;</span> {
    <span class='ident'>log</span>::<span class='ident'>set_logger</span>(<span class='op'>|</span><span class='ident'>max_log_level</span><span class='op'>|</span> {
        <span class='ident'>max_log_level</span>.<span class='ident'>set</span>(<span class='ident'>LogLevelFilter</span>::<span class='ident'>Info</span>);
        <span class='ident'>Box</span>::<span class='ident'>new</span>(<span class='ident'>SimpleLogger</span>)
    })
}
</pre>
</div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
<table>
                    <tr class=' module-item'>
                        <td><a class='macro' href='macro.debug!.html'
                               title='log::debug!'>debug!</a></td>
                        <td class='docblock short'>
                             <p>Logs a message at the debug level.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='macro' href='macro.error!.html'
                               title='log::error!'>error!</a></td>
                        <td class='docblock short'>
                             <p>Logs a message at the error level.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='macro' href='macro.info!.html'
                               title='log::info!'>info!</a></td>
                        <td class='docblock short'>
                             <p>Logs a message at the info level.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='macro' href='macro.log!.html'
                               title='log::log!'>log!</a></td>
                        <td class='docblock short'>
                             <p>The standard logging macro.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='macro' href='macro.log_enabled!.html'
                               title='log::log_enabled!'>log_enabled!</a></td>
                        <td class='docblock short'>
                             <p>Determines if a message logged at the specified level in that module will
be logged.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='macro' href='macro.trace!.html'
                               title='log::trace!'>trace!</a></td>
                        <td class='docblock short'>
                             <p>Logs a message at the trace level.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='macro' href='macro.warn!.html'
                               title='log::warn!'>warn!</a></td>
                        <td class='docblock short'>
                             <p>Logs a message at the warn level.</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.LogLocation.html'
                               title='log::LogLocation'>LogLocation</a></td>
                        <td class='docblock short'>
                             <p>The location of a log message.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='struct' href='struct.LogMetadata.html'
                               title='log::LogMetadata'>LogMetadata</a></td>
                        <td class='docblock short'>
                             <p>Metadata about a log message.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='struct' href='struct.LogRecord.html'
                               title='log::LogRecord'>LogRecord</a></td>
                        <td class='docblock short'>
                             <p>The &quot;payload&quot; of a log message.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='struct' href='struct.MaxLogLevelFilter.html'
                               title='log::MaxLogLevelFilter'>MaxLogLevelFilter</a></td>
                        <td class='docblock short'>
                             <p>A token providing read and write access to the global maximum log level
filter.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='struct' href='struct.SetLoggerError.html'
                               title='log::SetLoggerError'>SetLoggerError</a></td>
                        <td class='docblock short'>
                             <p>The type returned by <code>set_logger</code> if <code>set_logger</code> has already been called.</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.LogLevel.html'
                               title='log::LogLevel'>LogLevel</a></td>
                        <td class='docblock short'>
                             <p>An enum representing the available verbosity levels of the logging framework</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='enum' href='enum.LogLevelFilter.html'
                               title='log::LogLevelFilter'>LogLevelFilter</a></td>
                        <td class='docblock short'>
                             <p>An enum representing the available verbosity level filters of the logging
framework.</p>

                        </td>
                    </tr>
                </table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table>
                    <tr class=' module-item'>
                        <td><a class='trait' href='trait.Log.html'
                               title='log::Log'>Log</a></td>
                        <td class='docblock short'>
                             <p>A trait encapsulating the operations required of a logger</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.max_log_level.html'
                               title='log::max_log_level'>max_log_level</a></td>
                        <td class='docblock short'>
                             <p>Returns the current maximum log level.</p>

                        </td>
                    </tr>
                
                    <tr class=' module-item'>
                        <td><a class='fn' href='fn.set_logger.html'
                               title='log::set_logger'>set_logger</a></td>
                        <td class='docblock short'>
                             <p>Sets the global logger.</p>

                        </td>
                    </tr>
                </table></section>
    <section id='search' class="content hidden"></section>

    <section class="footer"></section>

    <div id="help" class="hidden">
        <div class="shortcuts">
            <h1>Keyboard shortcuts</h1>
            <dl>
                <dt>?</dt>
                <dd>Show this help dialog</dd>
                <dt>S</dt>
                <dd>Focus the search field</dd>
                <dt>&larrb;</dt>
                <dd>Move up in search results</dd>
                <dt>&rarrb;</dt>
                <dd>Move down in search results</dd>
                <dt>&#9166;</dt>
                <dd>Go to active search result</dd>
            </dl>
        </div>
        <div class="infos">
            <h1>Search tricks</h1>
            <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>typedef</code> (or
                <code>tdef</code>).
            </p>
            <p>
                Search functions by type signature (e.g.
                <code>vec -> usize</code>)
            </p>
        </div>
    </div>

    

    <script>
        window.rootPath = "../";
        window.currentCrate = "log";
        window.playgroundUrl = "";
    </script>
    <script src="../jquery.js"></script>
    <script src="../main.js"></script>
    
    <script async src="../search-index.js"></script>
</body>
</html>