1
+ /// Struct for implementing a simple logger
2
+ pub struct SimpleLogger ;
3
+
1
4
use chrono:: Local ;
2
5
3
- pub struct SimpleLogger ;
6
+ /// ANSI encoded text styling for bold, underline and italic
7
+ struct Format ;
8
+ #[ allow( dead_code) ]
9
+ impl Format {
10
+ const BOLD : & ' static str = "\x1b [1m" ;
11
+ const UNDERLINE : & ' static str = "\x1b [4m" ;
12
+ const ITALIC : & ' static str = "\x1b [3m" ;
13
+ }
14
+
15
+ /// ANSI encoded color scheme for text
16
+ struct Colors ;
17
+ #[ allow( dead_code) ]
18
+ impl Colors {
19
+ const VIOLET : & ' static str = "\x1b [95m" ;
20
+ const BLUE : & ' static str = "\x1b [94m" ;
21
+ const CYAN : & ' static str = "\x1b [96m" ;
22
+ const GREEN : & ' static str = "\x1b [92m" ;
23
+ const YELLOW : & ' static str = "\x1b [93m" ;
24
+ const RED : & ' static str = "\x1b [91m" ;
25
+ const END : & ' static str = "\x1b [0m" ;
26
+ const LIGHT_GREEN : & ' static str = "\x1b [32m" ;
27
+ const LIGHT_YELLOW : & ' static str = "\x1b [2;33m" ;
28
+ const LIGHT_RED : & ' static str = "\x1b [31m" ;
29
+ }
30
+
31
+ /// Struct controller for different log levels
32
+ struct Echo ;
33
+ #[ allow( dead_code) ]
34
+ impl Echo {
35
+ fn error ( msg : & String ) {
36
+ println ! ( "{}ERROR{}:{:<6}{}" , Colors :: RED , Colors :: END , "" , msg) ;
37
+ }
38
+ fn warning ( msg : & String ) {
39
+ println ! ( "{}WARNING{}:{:<4}{}" , Colors :: YELLOW , Colors :: END , "" , msg) ;
40
+ }
41
+ fn info ( msg : & String ) {
42
+ println ! ( "{}INFO{}:{:<7}{}" , Colors :: GREEN , Colors :: END , "" , msg) ;
43
+ }
44
+ fn debug ( msg : & String ) {
45
+ println ! (
46
+ "{}DEBUG{}:{:<6}{}" ,
47
+ Colors :: LIGHT_GREEN ,
48
+ Colors :: END ,
49
+ "" ,
50
+ msg
51
+ ) ;
52
+ }
53
+ fn trace ( msg : & String ) {
54
+ println ! (
55
+ "{}{}CRITICAL{}:{:<1}{}" ,
56
+ Colors :: LIGHT_GREEN ,
57
+ Format :: BOLD ,
58
+ Colors :: END ,
59
+ "" ,
60
+ msg
61
+ ) ;
62
+ }
63
+ }
4
64
5
65
/// Implementation of the `Log` trait for the `SimpleLogger` struct
6
66
///
@@ -17,15 +77,21 @@ impl log::Log for SimpleLogger {
17
77
fn log ( & self , record : & log:: Record ) {
18
78
let local_time = Local :: now ( ) ;
19
79
let formatted_time = local_time. format ( "%b-%d-%Y %I:%M:%S %p" ) ;
20
- println ! (
21
- "{} - {} - [{}:{}:{}] - {}" ,
80
+ let msg = format ! (
81
+ "{} [{}:{}:{}] {}" ,
22
82
formatted_time,
23
- record. level( ) ,
24
83
record. target( ) ,
25
84
record. file( ) . unwrap_or_default( ) ,
26
85
record. line( ) . unwrap_or_default( ) ,
27
86
record. args( )
28
87
) ;
88
+ match record. level ( ) {
89
+ log:: Level :: Debug => Echo :: debug ( & msg) ,
90
+ log:: Level :: Info => Echo :: info ( & msg) ,
91
+ log:: Level :: Warn => Echo :: warning ( & msg) ,
92
+ log:: Level :: Error => Echo :: error ( & msg) ,
93
+ log:: Level :: Trace => Echo :: trace ( & msg) ,
94
+ }
29
95
}
30
96
31
97
fn flush ( & self ) { }
0 commit comments