@@ -31,29 +31,45 @@ impl LogMiddleware {
31
31
) -> crate :: Result {
32
32
let path = ctx. uri ( ) . path ( ) . to_owned ( ) ;
33
33
let method = ctx. method ( ) . to_string ( ) ;
34
- log:: trace!( "IN => {} {}" , method, path) ;
34
+ log:: info!( "<-- Request received" , {
35
+ method: method,
36
+ path: path,
37
+ } ) ;
35
38
let start = std:: time:: Instant :: now ( ) ;
36
39
match next. run ( ctx) . await {
37
40
Ok ( res) => {
38
41
let status = res. status ( ) ;
39
- log:: info!(
40
- "{} {} {} {}ms" ,
41
- method,
42
- path,
43
- status,
44
- start. elapsed( ) . as_millis( )
45
- ) ;
42
+ if status. is_server_error ( ) {
43
+ log:: error!( "--> Response sent" , {
44
+ method: method,
45
+ path: path,
46
+ status: status as u16 ,
47
+ duration: format!( "{}ms" , start. elapsed( ) . as_millis( ) ) ,
48
+ } ) ;
49
+ } else if status. is_client_error ( ) {
50
+ log:: warn!( "--> Response sent" , {
51
+ method: method,
52
+ path: path,
53
+ status: status as u16 ,
54
+ duration: format!( "{}ms" , start. elapsed( ) . as_millis( ) ) ,
55
+ } ) ;
56
+ } else {
57
+ log:: info!( "--> Response sent" , {
58
+ method: method,
59
+ path: path,
60
+ status: status as u16 ,
61
+ duration: format!( "{}ms" , start. elapsed( ) . as_millis( ) ) ,
62
+ } ) ;
63
+ }
46
64
Ok ( res)
47
65
}
48
66
Err ( err) => {
49
- let msg = err. to_string ( ) ;
50
- log:: error!(
51
- "{} {} {} {}ms" ,
52
- msg,
53
- method,
54
- path,
55
- start. elapsed( ) . as_millis( )
56
- ) ;
67
+ log:: error!( "{}" , err. to_string( ) , {
68
+ method: method,
69
+ path: path,
70
+ status: err. status( ) as u16 ,
71
+ duration: format!( "{}ms" , start. elapsed( ) . as_millis( ) ) ,
72
+ } ) ;
57
73
Err ( err)
58
74
}
59
75
}
0 commit comments