Skip to content

Commit aca535d

Browse files
committed
Add documentation for OptFormatter
1 parent 12ec0e6 commit aca535d

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

spdlog/src/formatter/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//! The easiest way to make a custom formatter is to build a pattern, see
1111
//! [Compile-time and runtime pattern
1212
//! formatter](#compile-time-and-runtime-pattern-formatter) below. If pattern
13-
//! isn't flexible enough for you, you need to implement [`Formatter`] trait for
14-
//! your own formatter struct. See the implementation of [`FullFormatter`] and
15-
//! [./examples] directory for examples.
13+
//! isn't enough for you, take a look at [`OptFormatter`] which allows you to
14+
//! opt-out fields compared to [`FullFormatter`], or implement [`Formatter`]
15+
//! trait for your own formatter struct for better flexibility. See the
16+
//! implementation of [./examples] directory for examples.
1617
//!
1718
//! # Compile-time and runtime pattern formatter
1819
//!

spdlog/src/formatter/opt_formatter.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,72 @@ use crate::{
55
Error, Record, StringBuf, __EOL,
66
};
77

8+
/// A formatter that fields can be opt-in / opt-out.
89
///
10+
/// It enables all fields by default, formatting results are identical to
11+
/// [`FullFormatter`] (in this case, use the latter for optimal performance).
12+
///
13+
/// By disabling fields, corresponding information can be removed from formatted
14+
/// results.
15+
///
16+
/// [`FullFormatter`]: crate::formatter::FullFormatter
17+
///
18+
/// ## Examples
19+
///
20+
/// ```
21+
/// use spdlog::formatter::OptFormatter;
22+
/// # use spdlog::info;
23+
#[doc = include_str!(concat!(env!("OUT_DIR"), "/test_utils/common_for_doc_test.rs"))]
24+
/// #
25+
///
26+
/// let formatter = OptFormatter::builder()
27+
/// .time(false)
28+
/// .source_location(false)
29+
/// .build();
30+
/// // ... Setting up sinks with the formatter
31+
/// # let (doctest, sink) = test_utils::echo_logger_from_formatter(formatter, None);
32+
/// info!(logger: doctest, "Interesting log message");
33+
/// # assert_eq!(
34+
/// # sink.clone_string().replace("\r", ""),
35+
/// /* Output */ "[info] Interesting log message\n"
36+
/// # );
37+
///
38+
/// let formatter = OptFormatter::builder()
39+
/// .time(false)
40+
/// .level(false)
41+
/// .source_location(false)
42+
/// .build();
43+
/// // ... Setting up sinks with the formatter
44+
/// # let (doctest, sink) = test_utils::echo_logger_from_formatter(formatter, None);
45+
/// info!(logger: doctest, "Interesting log message");
46+
/// # assert_eq!(
47+
/// # sink.clone_string().replace("\r", ""),
48+
/// /* Output */ "Interesting log message\n"
49+
/// # );
50+
/// ```
951
#[derive(Clone)]
1052
pub struct OptFormatter {
1153
options: FormattingOptions,
1254
}
1355

1456
impl OptFormatter {
57+
/// Gets a builder of `OptFormatter` with default parameters:
58+
///
59+
/// | Parameter | Default Value |
60+
/// |-------------------|---------------|
61+
/// | [time] | `true` |
62+
/// | [logger_name] | `true` |
63+
/// | [level] | `true` |
64+
/// | [source_location] | `true` |
65+
/// | [kv] | `true` |
66+
/// | [eol] | `true` |
67+
///
68+
/// [time]: OptFormatterBuilder::time
69+
/// [logger_name]: OptFormatterBuilder::logger_name
70+
/// [level]: OptFormatterBuilder::level
71+
/// [source_location]: OptFormatterBuilder::source_location
72+
/// [kv]: OptFormatterBuilder::kv
73+
/// [eol]: OptFormatterBuilder::eol
1574
#[must_use]
1675
pub fn builder() -> OptFormatterBuilder {
1776
OptFormatterBuilder(FormattingOptions {
@@ -113,39 +172,61 @@ impl Formatter for OptFormatter {
113172
}
114173
}
115174

175+
#[allow(missing_docs)]
116176
pub struct OptFormatterBuilder(FormattingOptions);
117177

118178
impl OptFormatterBuilder {
179+
/// Specify whether to enable time field.
180+
///
181+
/// Example of this field: `[2022-11-02 09:23:12.263]`
119182
#[must_use]
120183
pub fn time(&mut self, value: bool) -> &mut Self {
121184
self.0.time = value;
122185
self
123186
}
124187

188+
/// Specify whether to enable logger name field.
189+
///
190+
/// Example of this field: `[logger-name]`
125191
#[must_use]
126192
pub fn logger_name(&mut self, value: bool) -> &mut Self {
127193
self.0.logger_name = value;
128194
self
129195
}
130196

197+
/// Specify whether to enable level field.
198+
///
199+
/// Note that disabling this field will also remove the style from the
200+
/// formatted result.
201+
///
202+
/// Example of this field: <code>[<font color="#0DBC79">info</font>]</code>
131203
#[must_use]
132204
pub fn level(&mut self, value: bool) -> &mut Self {
133205
self.0.level = value;
134206
self
135207
}
136208

209+
/// Specify whether to enable source location field.
210+
///
211+
/// Example of this field: `[mod::path, src/main.rs:4]`
137212
#[must_use]
138213
pub fn source_location(&mut self, value: bool) -> &mut Self {
139214
self.0.source_location = value;
140215
self
141216
}
142217

218+
/// Specify whether to enable kv field.
219+
///
220+
/// Example of this field: `{ key1=value1 key2=value2 }`
143221
#[must_use]
144222
pub fn kv(&mut self, value: bool) -> &mut Self {
145223
self.0.kv = value;
146224
self
147225
}
148226

227+
/// Specify whether to enable eol field.
228+
///
229+
/// Example of this field: `\n` or `\r\n` on Windows.
149230
#[must_use]
150231
pub fn eol(&mut self, value: bool) -> &mut Self {
151232
self.0.eol = value;

0 commit comments

Comments
 (0)