@@ -6,6 +6,7 @@ use log::{Level, LevelFilter, Log, Metadata, Record};
6
6
use std:: error:: Error ;
7
7
use std:: fs:: { File , OpenOptions } ;
8
8
use std:: io:: Write ;
9
+ use std:: path:: MAIN_SEPARATOR ;
9
10
use std:: sync:: { Arc , Mutex } ;
10
11
11
12
pub fn setup_logger ( ) -> Result < ( ) , Box < dyn Error > > {
@@ -60,16 +61,20 @@ impl Log for Logger {
60
61
let timestamp = now. format ( "%Y-%m-%d %H:%M:%S%.3f" ) ;
61
62
62
63
let file_info = match ( record. file ( ) , record. line ( ) ) {
63
- ( Some ( file) , Some ( line) ) => format ! ( "{}:{}" , file, line) ,
64
+ ( Some ( file) , Some ( line) ) => format ! ( "{}:{}" , strip_src_prefix ( file) . to_string ( ) , line) ,
64
65
_ => String :: from ( "unknown" ) ,
65
66
} ;
66
67
68
+ // Define fixed widths
67
69
let log_line = format ! (
68
- "[{} {} {}] - {}" ,
70
+ "[{:<width$ } {:<level_width$ } {:<file_info_width$}] {}" ,
69
71
timestamp,
70
72
record. level( ) ,
71
73
file_info,
72
- record. args( )
74
+ record. args( ) ,
75
+ width = 23 ,
76
+ level_width = 5 ,
77
+ file_info_width = 23
73
78
) ;
74
79
75
80
if self . enabled {
@@ -84,7 +89,7 @@ impl Log for Logger {
84
89
Level :: Error => {
85
90
eprintln ! ( "{}" , record. args( ) ) ;
86
91
}
87
- _ => ( )
92
+ _ => ( ) ,
88
93
}
89
94
}
90
95
@@ -93,3 +98,8 @@ impl Log for Logger {
93
98
file. flush ( ) . unwrap ( ) ;
94
99
}
95
100
}
101
+
102
+ fn strip_src_prefix ( file_path : & str ) -> & str {
103
+ let prefix = format ! ( "src{}" , MAIN_SEPARATOR ) ;
104
+ file_path. strip_prefix ( & prefix) . unwrap_or ( file_path)
105
+ }
0 commit comments