@@ -36,7 +36,7 @@ pub mod errors;
36
36
use std;
37
37
use std:: io:: Write ;
38
38
39
- // use chrono;
39
+ use chrono;
40
40
41
41
42
42
////////////////
@@ -208,7 +208,7 @@ impl Formatter {
208
208
/// logging_rs::Output::default(),
209
209
/// logging_rs::Level::default(),
210
210
/// "Some message with an {{argument}}",
211
- /// vec![("argument", "replaced value")]
211
+ /// vec![("argument", "replaced value".to_string() )]
212
212
/// );
213
213
/// ```
214
214
///
@@ -217,90 +217,75 @@ impl Formatter {
217
217
/// - [`Formatter`]
218
218
/// - [`Output`]
219
219
/// - [`Level`]
220
- pub fn format < ' a > ( & self , output : Output , level : Level , message : & ' a str , mut arguments : Vec < ( & str , & ' a str ) > ) -> String {
221
- // TODO (ElBe): Make timestamps work. None of chrono, time or humantime have the things I want
222
- let timestamp: & str = "TIMESTAMP" ;
223
-
224
- // let time = chrono::Utc::now();
225
- //let _timestamp = time.format("%Y-%m-%d %H:%M:%S").to_string(); //chrono::format::DelayedFormat<chrono::format::StrftimeItems<'_>>
226
- //let parsed = chrono::NaiveDateTime::parse_from_str(&_timestamp.to_string(), "%Y-%m-%d %H:%M:%S").expect("Bad");
227
-
228
- //let borrowed = &_timestamp;
229
-
230
- //println!("{}", _timestamp);
231
- //println!("{}", parsed.to_string().as_str());
232
-
233
- // arguments.push(("timestamp", &time.format("%Y-%m-%d %H:%M:%S").to_string()));
234
-
235
- let mut colors: Vec < ( & str , & str ) > = vec ! [
220
+ pub fn format < ' a > ( & self , output : Output , level : Level , message : & ' a str , mut arguments : Vec < ( & str , String ) > ) -> String {
221
+ let mut colors: Vec < ( & str , String ) > = vec ! [
236
222
// Formatting codes
237
- ( "end" , "\x1b [0m" ) ,
238
- ( "bold" , "\x1b [1m" ) ,
239
- ( "italic" , "\x1b [3m" ) ,
240
- ( "underline" , "\x1b [4m" ) ,
241
- ( "overline" , "\x1b [53m" ) ,
223
+ ( "end" , "\x1b [0m" . to_string ( ) ) ,
224
+ ( "bold" , "\x1b [1m" . to_string ( ) ) ,
225
+ ( "italic" , "\x1b [3m" . to_string ( ) ) ,
226
+ ( "underline" , "\x1b [4m" . to_string ( ) ) ,
227
+ ( "overline" , "\x1b [53m" . to_string ( ) ) ,
242
228
243
229
// Foreground colors
244
- ( "color.black" , "\x1b [30m" ) ,
245
- ( "color.red" , "\x1b [31m" ) ,
246
- ( "color.green" , "\x1b [32m" ) ,
247
- ( "color.yellow" , "\x1b [33m" ) ,
248
- ( "color.blue" , "\x1b [34m" ) ,
249
- ( "color.magenta" , "\x1b [35m" ) ,
250
- ( "color.cyan" , "\x1b [36m" ) ,
251
- ( "color.white" , "\x1b [37m" ) ,
230
+ ( "color.black" , "\x1b [30m" . to_string ( ) ) ,
231
+ ( "color.red" , "\x1b [31m" . to_string ( ) ) ,
232
+ ( "color.green" , "\x1b [32m" . to_string ( ) ) ,
233
+ ( "color.yellow" , "\x1b [33m" . to_string ( ) ) ,
234
+ ( "color.blue" , "\x1b [34m" . to_string ( ) ) ,
235
+ ( "color.magenta" , "\x1b [35m" . to_string ( ) ) ,
236
+ ( "color.cyan" , "\x1b [36m" . to_string ( ) ) ,
237
+ ( "color.white" , "\x1b [37m" . to_string ( ) ) ,
252
238
253
239
// Bright foreground colors
254
- ( "color.bright_black" , "\x1b [90m" ) ,
255
- ( "color.bright_red" , "\x1b [91m" ) ,
256
- ( "color.bright_green" , "\x1b [92m" ) ,
257
- ( "color.bright_yellow" , "\x1b [93m" ) ,
258
- ( "color.bright_blue" , "\x1b [94m" ) ,
259
- ( "color.bright_magenta" , "\x1b [95m" ) ,
260
- ( "color.bright_cyan" , "\x1b [96m" ) ,
261
- ( "color.bright_white" , "\x1b [97m" ) ,
240
+ ( "color.bright_black" , "\x1b [90m" . to_string ( ) ) ,
241
+ ( "color.bright_red" , "\x1b [91m" . to_string ( ) ) ,
242
+ ( "color.bright_green" , "\x1b [92m" . to_string ( ) ) ,
243
+ ( "color.bright_yellow" , "\x1b [93m" . to_string ( ) ) ,
244
+ ( "color.bright_blue" , "\x1b [94m" . to_string ( ) ) ,
245
+ ( "color.bright_magenta" , "\x1b [95m" . to_string ( ) ) ,
246
+ ( "color.bright_cyan" , "\x1b [96m" . to_string ( ) ) ,
247
+ ( "color.bright_white" , "\x1b [97m" . to_string ( ) ) ,
262
248
263
249
// Background colors
264
- ( "back.black" , "\x1b [40m" ) ,
265
- ( "back.red" , "\x1b [41m" ) ,
266
- ( "back.green" , "\x1b [42m" ) ,
267
- ( "back.yellow" , "\x1b [43m" ) ,
268
- ( "back.blue" , "\x1b [44m" ) ,
269
- ( "back.magenta" , "\x1b [45m" ) ,
270
- ( "back.cyan" , "\x1b [46m" ) ,
271
- ( "back.white" , "\x1b [47m" ) ,
250
+ ( "back.black" , "\x1b [40m" . to_string ( ) ) ,
251
+ ( "back.red" , "\x1b [41m" . to_string ( ) ) ,
252
+ ( "back.green" , "\x1b [42m" . to_string ( ) ) ,
253
+ ( "back.yellow" , "\x1b [43m" . to_string ( ) ) ,
254
+ ( "back.blue" , "\x1b [44m" . to_string ( ) ) ,
255
+ ( "back.magenta" , "\x1b [45m" . to_string ( ) ) ,
256
+ ( "back.cyan" , "\x1b [46m" . to_string ( ) ) ,
257
+ ( "back.white" , "\x1b [47m" . to_string ( ) ) ,
272
258
273
259
// Bright background colors
274
- ( "back.bright_black" , "\x1b [100m" ) ,
275
- ( "back.bright_red" , "\x1b [101m" ) ,
276
- ( "back.bright_green" , "\x1b [102m" ) ,
277
- ( "back.bright_yellow" , "\x1b [103m" ) ,
278
- ( "back.bright_blue" , "\x1b [104m" ) ,
279
- ( "back.bright_magenta" , "\x1b [105m" ) ,
280
- ( "back.bright_cyan" , "\x1b [106m" ) ,
281
- ( "back.bright_white" , "\x1b [107m" ) ,
260
+ ( "back.bright_black" , "\x1b [100m" . to_string ( ) ) ,
261
+ ( "back.bright_red" , "\x1b [101m" . to_string ( ) ) ,
262
+ ( "back.bright_green" , "\x1b [102m" . to_string ( ) ) ,
263
+ ( "back.bright_yellow" , "\x1b [103m" . to_string ( ) ) ,
264
+ ( "back.bright_blue" , "\x1b [104m" . to_string ( ) ) ,
265
+ ( "back.bright_magenta" , "\x1b [105m" . to_string ( ) ) ,
266
+ ( "back.bright_cyan" , "\x1b [106m" . to_string ( ) ) ,
267
+ ( "back.bright_white" , "\x1b [107m" . to_string ( ) ) ,
282
268
] ;
283
269
284
- let level_string: ( & str , & str ) = ( "level" , match level {
270
+ let level_string: ( & str , String ) = ( "level" , match level {
285
271
Level :: DEBUG => "DEBUG" ,
286
272
Level :: INFO => "INFO" ,
287
273
Level :: WARN => "WARNING" ,
288
274
Level :: ERROR => "ERROR" ,
289
275
Level :: FATAL => "FATAL" ,
290
276
Level :: MESSAGE => "MESSAGE"
291
- } ) ;
292
- let colored_level_string: ( & str , & str ) = ( "level" , match level {
277
+ } . to_string ( ) ) ;
278
+ let colored_level_string: ( & str , String ) = ( "level" , match level {
293
279
Level :: DEBUG => "DEBUG" ,
294
280
Level :: INFO => "{{color.blue}}INFO{{end}}" ,
295
281
Level :: WARN => "{{color.yellow}}WARNING{{end}}" ,
296
282
Level :: ERROR => "{{color.red}}ERROR{{end}}" ,
297
283
Level :: FATAL => "{{color.red}}FATAL{{end}}" ,
298
284
Level :: MESSAGE => "{{color.blue}}MESSAGE{{end}}"
299
- } ) ;
285
+ } . to_string ( ) ) ;
300
286
301
- arguments. push ( ( "message" , message) ) ;
302
- arguments. push ( ( "timestamp" , timestamp) ) ;
303
- //arguments.push(("timestamp", chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string().to_owned().as_str()));
287
+ arguments. push ( ( "message" , message. to_string ( ) ) ) ;
288
+ arguments. push ( ( "timestamp" , chrono:: Local :: now ( ) . format ( & self . timestamp_format ) . to_string ( ) ) ) ;
304
289
305
290
let mut result: String = match output {
306
291
Output :: STDOUT | Output :: STDERR => {
@@ -316,7 +301,7 @@ impl Formatter {
316
301
arguments. append ( & mut colors) ;
317
302
318
303
for ( key, value) in arguments {
319
- result = result. replace ( ( "{{" . to_owned ( ) + key + "}}" ) . as_str ( ) , value) ;
304
+ result = result. replace ( ( "{{" . to_owned ( ) + key + "}}" ) . as_str ( ) , & value) ;
320
305
}
321
306
322
307
return result. clone ( ) ;
@@ -428,7 +413,7 @@ impl Logger {
428
413
/// - [`Level`]
429
414
pub fn log ( & self , message : & str , level : Level , path : & str ) {
430
415
for writable in self . writable_list . clone ( ) {
431
- let formatted: String = self . formatter . format ( writable. clone ( ) , level, message, vec ! [ ( "path" , path) ] ) ;
416
+ let formatted: String = self . formatter . format ( writable. clone ( ) , level, message, vec ! [ ( "path" , path. to_string ( ) ) ] ) ;
432
417
433
418
match writable {
434
419
Output :: STDOUT => println ! ( "{}" , formatted) ,
0 commit comments