File tree Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,8 @@ mod prelude {
38
38
fn query ( & self ) -> IndexMap < String , String > ;
39
39
fn wants_json ( & self ) -> bool ;
40
40
fn query_with_params ( & self , params : IndexMap < String , String > ) -> String ;
41
+
42
+ fn log_metadata < V : std:: fmt:: Display > ( & mut self , key : & ' static str , value : V ) ;
41
43
}
42
44
43
45
impl < ' a > RequestUtils for dyn Request + ' a {
@@ -76,6 +78,10 @@ mod prelude {
76
78
. finish ( ) ;
77
79
format ! ( "?{}" , query_string)
78
80
}
81
+
82
+ fn log_metadata < V : std:: fmt:: Display > ( & mut self , key : & ' static str , value : V ) {
83
+ crate :: middleware:: log_request:: add_custom_metadata ( self , key, value) ;
84
+ }
79
85
}
80
86
}
81
87
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ mod ember_index_rewrite;
25
25
mod ensure_well_formed_500;
26
26
mod head;
27
27
mod log_connection_pool_status;
28
- mod log_request;
28
+ pub mod log_request;
29
29
mod require_user_agent;
30
30
mod static_or_continue;
31
31
Original file line number Diff line number Diff line change @@ -42,6 +42,22 @@ impl Handler for LogRequests {
42
42
}
43
43
}
44
44
45
+ struct CustomMetadata {
46
+ entries : Vec < ( & ' static str , String ) > ,
47
+ }
48
+
49
+ pub fn add_custom_metadata < V : Display > ( req : & mut dyn Request , key : & ' static str , value : V ) {
50
+ if let Some ( metadata) = req. mut_extensions ( ) . find_mut :: < CustomMetadata > ( ) {
51
+ metadata. entries . push ( ( key, value. to_string ( ) ) ) ;
52
+ } else {
53
+ let mut metadata = CustomMetadata {
54
+ entries : Vec :: new ( ) ,
55
+ } ;
56
+ metadata. entries . push ( ( key, value. to_string ( ) ) ) ;
57
+ req. mut_extensions ( ) . insert ( metadata) ;
58
+ }
59
+ }
60
+
45
61
struct RequestLine < ' r > {
46
62
req : & ' r dyn Request ,
47
63
res : & ' r Result < Response > ,
@@ -66,6 +82,12 @@ impl Display for RequestLine<'_> {
66
82
line. add_field ( "status" , status) ?;
67
83
line. add_quoted_field ( "user_agent" , request_header ( self . req , "User-Agent" ) ) ?;
68
84
85
+ if let Some ( metadata) = self . req . extensions ( ) . find :: < CustomMetadata > ( ) {
86
+ for ( key, value) in & metadata. entries {
87
+ line. add_quoted_field ( key, value) ?;
88
+ }
89
+ }
90
+
69
91
if let Some ( len) = self . req . extensions ( ) . find :: < u64 > ( ) {
70
92
line. add_field ( "metadata_length" , len) ?;
71
93
}
You can’t perform that action at this time.
0 commit comments