2
2
use log:: Level ;
3
3
use log:: LevelFilter ;
4
4
5
- pub use chrono:: offset:: { FixedOffset , Local , Offset , TimeZone , Utc } ;
6
5
use std:: borrow:: Cow ;
7
6
#[ cfg( feature = "termcolor" ) ]
8
7
use termcolor:: Color ;
8
+ pub use time:: { format_description:: FormatItem , macros:: format_description, UtcOffset } ;
9
9
10
10
#[ derive( Debug , Clone , Copy ) ]
11
11
/// Padding to be used for logging the level
@@ -51,6 +51,13 @@ pub enum ThreadLogMode {
51
51
Both ,
52
52
}
53
53
54
+ #[ derive( Debug , Clone ) ]
55
+ pub ( crate ) enum TimeFormat {
56
+ Rfc2822 ,
57
+ Rfc3339 ,
58
+ Custom ( & ' static [ time:: format_description:: FormatItem < ' static > ] ) ,
59
+ }
60
+
54
61
/// Configuration for the Loggers
55
62
///
56
63
/// All loggers print the message in the following form:
@@ -72,9 +79,8 @@ pub struct Config {
72
79
pub ( crate ) target : LevelFilter ,
73
80
pub ( crate ) target_padding : TargetPadding ,
74
81
pub ( crate ) location : LevelFilter ,
75
- pub ( crate ) time_format : Cow < ' static , str > ,
76
- pub ( crate ) time_offset : FixedOffset ,
77
- pub ( crate ) time_local : bool ,
82
+ pub ( crate ) time_format : TimeFormat ,
83
+ pub ( crate ) time_offset : UtcOffset ,
78
84
pub ( crate ) filter_allow : Cow < ' static , [ Cow < ' static , str > ] > ,
79
85
pub ( crate ) filter_ignore : Cow < ' static , [ Cow < ' static , str > ] > ,
80
86
#[ cfg( feature = "termcolor" ) ]
@@ -167,34 +173,68 @@ impl ConfigBuilder {
167
173
self
168
174
}
169
175
170
- /// Set time chrono [strftime] format string.
176
+ /// Sets the time format to a custom representation.
177
+ ///
178
+ /// The easiest way to satisfy the static lifetime of the argument is to directly use the
179
+ /// re-exported [`time::macros::format_description`] macro.
180
+ ///
181
+ /// *Note*: The default time format is "[hour]:[minute]:[second]".
182
+ ///
183
+ /// The syntax for the format_description macro can be found in the
184
+ /// [`time` crate book](https://time-rs.github.io/book/api/format-description.html).
171
185
///
172
- /// [strftime]: https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers
173
- pub fn set_time_format_str ( & mut self , time_format : & ' static str ) -> & mut ConfigBuilder {
174
- self . 0 . time_format = Cow :: Borrowed ( time_format) ;
186
+ /// # Usage
187
+ ///
188
+ /// ```
189
+ /// # use simplelog::{ConfigBuilder, format_description};
190
+ /// let config = ConfigBuilder::new()
191
+ /// .set_time_format_custom(format_description!("[hour]:[minute]:[second].[subsecond]"))
192
+ /// .build();
193
+ /// ```
194
+ pub fn set_time_format_custom (
195
+ & mut self ,
196
+ time_format : & ' static [ FormatItem < ' static > ] ,
197
+ ) -> & mut ConfigBuilder {
198
+ self . 0 . time_format = TimeFormat :: Custom ( time_format) ;
175
199
self
176
200
}
177
201
178
- /// Set time chrono [strftime] format string.
179
- ///
180
- /// [strftime]: https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers
181
- pub fn set_time_format ( & mut self , time_format : String ) -> & mut ConfigBuilder {
182
- self . 0 . time_format = Cow :: Owned ( time_format) ;
202
+ /// Set time format string to use rfc2822.
203
+ pub fn set_time_format_rfc2822 ( & mut self ) -> & mut ConfigBuilder {
204
+ self . 0 . time_format = TimeFormat :: Rfc2822 ;
183
205
self
184
206
}
185
207
186
- /// Set offset used for logging time (default is 0)
187
- pub fn set_time_offset ( & mut self , time_offset : FixedOffset ) -> & mut ConfigBuilder {
188
- self . 0 . time_offset = time_offset ;
208
+ /// Set time format string to use rfc3339.
209
+ pub fn set_time_format_rfc3339 ( & mut self ) -> & mut ConfigBuilder {
210
+ self . 0 . time_format = TimeFormat :: Rfc3339 ;
189
211
self
190
212
}
191
213
192
- /// set if you log in local timezone or UTC (default is UTC)
193
- pub fn set_time_to_local ( & mut self , local : bool ) -> & mut ConfigBuilder {
194
- self . 0 . time_local = local ;
214
+ /// Set offset used for logging time (default is UTC)
215
+ pub fn set_time_offset ( & mut self , offset : UtcOffset ) -> & mut ConfigBuilder {
216
+ self . 0 . time_offset = offset ;
195
217
self
196
218
}
197
219
220
+ /// Sets the offset used to the current local time offset
221
+ /// (overriding values previously set by [`ConfigBuilder::set_time_offset`]).
222
+ ///
223
+ /// This function may fail if the offset cannot be determined soundly.
224
+ /// This may be the case, when the program is multi-threaded by the time of calling this function.
225
+ /// One can opt-out of this behavior by setting `RUSTFLAGS="--cfg unsound_local_offset"`.
226
+ /// Doing so is not recommended, completely untested and may cause unexpected segfaults.
227
+ #[ cfg( feature = "local-offset" ) ]
228
+ pub fn set_time_offset_to_local ( & mut self ) -> Result < & mut ConfigBuilder , & mut ConfigBuilder > {
229
+ match UtcOffset :: current_local_offset ( ) {
230
+ Ok ( offset) => {
231
+ self . 0 . time_offset = offset;
232
+ Ok ( self )
233
+ }
234
+ Err ( _) => Err ( self ) ,
235
+ }
236
+ }
237
+
198
238
/// set if you want to write colors in the logfile (default is Off)
199
239
#[ cfg( feature = "ansi_term" ) ]
200
240
pub fn set_write_log_enable_colors ( & mut self , local : bool ) -> & mut ConfigBuilder {
@@ -284,9 +324,8 @@ impl Default for Config {
284
324
target : LevelFilter :: Debug ,
285
325
target_padding : TargetPadding :: Off ,
286
326
location : LevelFilter :: Trace ,
287
- time_format : Cow :: Borrowed ( "%H:%M:%S" ) ,
288
- time_offset : FixedOffset :: east ( 0 ) ,
289
- time_local : false ,
327
+ time_format : TimeFormat :: Custom ( format_description ! ( "[hour]:[minute]:[second]" ) ) ,
328
+ time_offset : UtcOffset :: UTC ,
290
329
filter_allow : Cow :: Borrowed ( & [ ] ) ,
291
330
filter_ignore : Cow :: Borrowed ( & [ ] ) ,
292
331
write_log_enable_colors : false ,
0 commit comments