@@ -22,7 +22,7 @@ use crate::Append;
22
22
use crate :: Diagnostic ;
23
23
use crate :: Filter ;
24
24
25
- /// Creates a new empty [`Builder `] instance for configuring log dispatching.
25
+ /// Creates a new empty [`LoggerBuilder `] instance for configuring log dispatching.
26
26
///
27
27
/// # Examples
28
28
///
@@ -33,11 +33,11 @@ use crate::Filter;
33
33
/// .dispatch(|d| d.append(append::Stderr::default()))
34
34
/// .apply();
35
35
/// ```
36
- pub fn builder ( ) -> Builder {
37
- Builder :: new ( )
36
+ pub fn builder ( ) -> LoggerBuilder {
37
+ LoggerBuilder :: new ( )
38
38
}
39
39
40
- /// Creates a [`Builder `] with a default [`append::Stdout`] appender and an [`env_filter`](https://crates.io/crates/env_filter)
40
+ /// Creates a [`LoggerBuilder `] with a default [`append::Stdout`] appender and an [`env_filter`](https://crates.io/crates/env_filter)
41
41
/// respecting `RUST_LOG`.
42
42
///
43
43
/// # Examples
@@ -46,14 +46,14 @@ pub fn builder() -> Builder {
46
46
/// logforth::stdout().apply();
47
47
/// log::error!("This error will be logged to stdout.");
48
48
/// ```
49
- pub fn stdout ( ) -> Builder {
49
+ pub fn stdout ( ) -> LoggerBuilder {
50
50
builder ( ) . dispatch ( |d| {
51
51
d. filter ( EnvFilter :: from_default_env ( ) )
52
52
. append ( append:: Stdout :: default ( ) )
53
53
} )
54
54
}
55
55
56
- /// Creates a [`Builder `] with a default [`append::Stderr`] appender and an [`env_filter`](https://crates.io/crates/env_filter)
56
+ /// Creates a [`LoggerBuilder `] with a default [`append::Stderr`] appender and an [`env_filter`](https://crates.io/crates/env_filter)
57
57
/// respecting `RUST_LOG`.
58
58
///
59
59
/// # Examples
@@ -62,7 +62,7 @@ pub fn stdout() -> Builder {
62
62
/// logforth::stderr().apply();
63
63
/// log::info!("This info will be logged to stderr.");
64
64
/// ```
65
- pub fn stderr ( ) -> Builder {
65
+ pub fn stderr ( ) -> LoggerBuilder {
66
66
builder ( ) . dispatch ( |d| {
67
67
d. filter ( EnvFilter :: from_default_env ( ) )
68
68
. append ( append:: Stderr :: default ( ) )
@@ -80,25 +80,19 @@ pub fn stderr() -> Builder {
80
80
/// .dispatch(|d| d.append(append::Stdout::default()))
81
81
/// .apply();
82
82
/// ```
83
- #[ must_use = "call `apply` to set the global logger" ]
83
+ #[ must_use = "call `apply` to set the global logger or `build` to construct a logger instance " ]
84
84
#[ derive( Debug ) ]
85
- pub struct Builder {
85
+ pub struct LoggerBuilder {
86
86
// stashed dispatches
87
87
dispatches : Vec < Dispatch > ,
88
-
89
- // default to trace - we need this because the global default is OFF
90
- max_level : LevelFilter ,
91
88
}
92
89
93
- impl Builder {
90
+ impl LoggerBuilder {
94
91
fn new ( ) -> Self {
95
- Builder {
96
- dispatches : vec ! [ ] ,
97
- max_level : LevelFilter :: Trace ,
98
- }
92
+ LoggerBuilder { dispatches : vec ! [ ] }
99
93
}
100
94
101
- /// Registers a new dispatch with the [`Builder `].
95
+ /// Registers a new dispatch with the [`LoggerBuilder `].
102
96
///
103
97
/// # Examples
104
98
///
@@ -117,27 +111,28 @@ impl Builder {
117
111
self
118
112
}
119
113
120
- /// Sets the global maximum log level. Default to [`LevelFilter::Trace`].
121
- ///
122
- /// This will be passed to `log::set_max_level()`.
114
+ /// Build the [`Logger`].
123
115
///
124
116
/// # Examples
125
117
///
126
118
/// ```
127
- /// logforth::builder()
128
- /// .max_level(log::LevelFilter::Warn)
129
- /// .apply();
119
+ /// let l = logforth::builder().build();
120
+ /// log::error!(logger: l, "Hello error!");
130
121
/// ```
131
- pub fn max_level ( mut self , max_level : LevelFilter ) -> Self {
132
- self . max_level = max_level;
133
- self
122
+ pub fn build ( self ) -> Logger {
123
+ Logger :: new ( self . dispatches )
134
124
}
135
125
136
126
/// Sets up the global logger with all the configured dispatches.
137
127
///
138
128
/// This should be called early in the execution of a Rust program. Any log events that occur
139
129
/// before initialization will be ignored.
140
130
///
131
+ /// This will set the global maximum log level to [`LevelFilter::Trace`]. To override this,
132
+ /// call [`log::set_max_level`] after this function. Alternatively, you can obtain a [`Logger`]
133
+ /// instance by calling [`LoggerBuilder::build`], and then call [`log::set_boxed_logger`]
134
+ /// manually.
135
+ ///
141
136
/// # Errors
142
137
///
143
138
/// Returns an error if a global logger has already been set.
@@ -151,9 +146,9 @@ impl Builder {
151
146
/// }
152
147
/// ```
153
148
pub fn try_apply ( self ) -> Result < ( ) , log:: SetLoggerError > {
154
- let logger = Logger :: new ( self . dispatches ) ;
149
+ let logger = self . build ( ) ;
155
150
log:: set_boxed_logger ( Box :: new ( logger) ) ?;
156
- log:: set_max_level ( self . max_level ) ;
151
+ log:: set_max_level ( LevelFilter :: Trace ) ;
157
152
Ok ( ( ) )
158
153
}
159
154
@@ -162,6 +157,11 @@ impl Builder {
162
157
/// This function will panic if it is called more than once, or if another library has already
163
158
/// initialized a global logger.
164
159
///
160
+ /// This function will set the global maximum log level to [`LevelFilter::Trace`]. To override
161
+ /// this, call [`log::set_max_level`] after this function. Alternatively, you can obtain a
162
+ /// [`Logger`] instance by calling [`LoggerBuilder::build`], and then call
163
+ /// [`log::set_boxed_logger`] manually.
164
+ ///
165
165
/// # Panics
166
166
///
167
167
/// Panics if the global logger has already been set.
0 commit comments