55//! about the contents of this module and can use the `Formatter` like an ordinary
66//! [`Write`].
77//!
8+ //! # Formatting log records
9+ //!
10+ //! The format used to print log records can be customised using the [`Builder.format`]
11+ //! method.
12+ //! Custom formats can apply different color and weight to printed values using
13+ //! [`Style`] builders.
14+ //!
15+ //! ```
16+ //! use std::io::Write;
17+ //! use env_logger::fmt::Color;
18+ //!
19+ //! let mut builder = env_logger::Builder::new();
20+ //!
21+ //! builder.format(|buf, record| {
22+ //! let mut level_style = buf.style();
23+ //!
24+ //! level_style.set_color(Color::Red).set_bold(true);
25+ //!
26+ //! writeln!(buf, "{}: {}",
27+ //! level_style.value(record.level()),
28+ //! record.args())
29+ //! });
30+ //! ```
31+ //!
832//! [`Formatter`]: struct.Formatter.html
33+ //! [`Style`]: struct.Style.html
34+ //! [`Builder.format`]: ../struct.Builder.html#method.format
935//! [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
1036
1137use std:: io:: prelude:: * ;
@@ -111,7 +137,16 @@ pub struct StyledValue<'a, T> {
111137 value : T ,
112138}
113139
114- /// Log target, either stdout or stderr.
140+ /// An [RFC3339] formatted timestamp.
141+ ///
142+ /// The timestamp implements [`Display`] and can be written to a [`Formatter`].
143+ ///
144+ /// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
145+ /// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html
146+ /// [`Formatter`]: struct.Formatter.html
147+ pub struct Timestamp ( DateTime < Utc > ) ;
148+
149+ /// Log target, either `stdout` or `stderr`.
115150#[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
116151pub enum Target {
117152 /// Logs will be sent to standard output.
@@ -131,7 +166,7 @@ impl Default for Target {
131166pub enum WriteStyle {
132167 /// Try to print styles, but don't force the issue.
133168 Auto ,
134- /// Always print styles.
169+ /// Try very hard to print styles.
135170 Always ,
136171 /// Never print styles.
137172 Never ,
@@ -143,35 +178,48 @@ impl Default for WriteStyle {
143178 }
144179}
145180
181+ /// A terminal target with color awareness.
146182pub ( crate ) struct Writer ( BufferWriter ) ;
147183
184+ /// A builder for a terminal writer.
185+ ///
186+ /// The target and style choice can be configured before building.
148187pub ( crate ) struct Builder {
149188 target : Target ,
150189 write_style : WriteStyle ,
151190}
152191
153192impl Builder {
193+ /// Initialize the writer builder with defaults.
154194 pub fn new ( ) -> Self {
155195 Builder {
156196 target : Default :: default ( ) ,
157197 write_style : Default :: default ( ) ,
158198 }
159199 }
160200
201+ /// Set the target to write to.
161202 pub fn target ( & mut self , target : Target ) -> & mut Self {
162203 self . target = target;
163204 self
164205 }
165206
207+ /// Parses a style choice string.
208+ ///
209+ /// See the [Disabling colors] section for more details.
210+ ///
211+ /// [Disabling colors]: ../index.html#disabling-colors
166212 pub fn parse ( & mut self , write_style : & str ) -> & mut Self {
167213 self . write_style ( parse_write_style ( write_style) )
168214 }
169215
216+ /// Whether or not to print style characters when writing.
170217 pub fn write_style ( & mut self , write_style : WriteStyle ) -> & mut Self {
171218 self . write_style = write_style;
172219 self
173220 }
174221
222+ /// Build a terminal writer.
175223 pub fn build ( & mut self ) -> Writer {
176224 let color_choice = match self . write_style {
177225 WriteStyle :: Auto => ColorChoice :: Auto ,
@@ -304,15 +352,6 @@ impl Style {
304352 }
305353}
306354
307- /// An [RFC3339] formatted timestamp.
308- ///
309- /// The timestamp implements [`Display`] and can be written to a [`Formatter`].
310- ///
311- /// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
312- /// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html
313- /// [`Formatter`]: struct.Formatter.html
314- pub struct Timestamp ( DateTime < Utc > ) ;
315-
316355impl Formatter {
317356 pub ( crate ) fn new ( writer : & Writer ) -> Self {
318357 Formatter {
0 commit comments