@@ -12,8 +12,8 @@ pub use log_file_writer::LogFileWriter;
12
12
use logger:: log;
13
13
pub use logger:: set_global_logger;
14
14
pub use logger:: {
15
- add_thread_local_log_tag, clear_thread_local_log_tags , with_thread_local_log_tags ,
16
- LoggerStoppedError ,
15
+ add_thread_local_log_tag, add_thread_local_log_tags_from_request , clear_thread_local_log_tags ,
16
+ with_thread_local_log_tags , LoggerStoppedError ,
17
17
} ;
18
18
use std:: fmt:: { Display , Formatter } ;
19
19
use std:: time:: SystemTime ;
@@ -83,17 +83,11 @@ pub fn debug(msg: impl Into<String>, tags: impl Into<TagList>) -> Result<(), Log
83
83
///
84
84
/// # Errors
85
85
/// Returns `Err` when the global logger has stopped.
86
- #[ allow( clippy:: needless_pass_by_value) ]
87
86
#[ allow( clippy:: module_name_repetitions) ]
88
- pub fn log_response (
89
- req : & Request ,
90
- result : Result < Response , Error > ,
91
- ) -> Result < Response , LoggerStoppedError > {
87
+ pub fn log_response ( result : Result < Response , Error > ) -> Result < Response , LoggerStoppedError > {
92
88
match result {
93
89
Ok ( response) => {
94
90
let mut tags = Vec :: new ( ) ;
95
- tags. push ( Tag :: new ( "http_method" , req. method ( ) ) ) ;
96
- tags. push ( Tag :: new ( "path" , req. url ( ) . path ( ) ) ) ;
97
91
tags. push ( Tag :: new ( "code" , response. code ) ) ;
98
92
if let Some ( body_len) = response. body . len ( ) {
99
93
tags. push ( Tag :: new ( "body_len" , body_len) ) ;
@@ -112,8 +106,6 @@ pub fn log_response(
112
106
if let Some ( backtrace) = e. backtrace {
113
107
tags. push ( Tag :: new ( "msg" , format ! ( "{backtrace:?}" ) ) ) ;
114
108
}
115
- tags. push ( Tag :: new ( "http_method" , req. method ( ) ) ) ;
116
- tags. push ( Tag :: new ( "path" , req. url ( ) . path ( ) ) ) ;
117
109
tags. push ( Tag :: new ( "code" , response. code ) ) ;
118
110
if let Some ( body_len) = response. body . len ( ) {
119
111
tags. push ( Tag :: new ( "body_len" , body_len) ) ;
@@ -123,3 +115,29 @@ pub fn log_response(
123
115
}
124
116
}
125
117
}
118
+
119
+ /// Adds thread-local log tags from the request and then calls the handler `f`.
120
+ /// When `f` does logging, the log messages will include the request id, HTTP method, and path.
121
+ /// When `f` returns, this function makes a new log event for the result
122
+ /// and sends it to the global logger.
123
+ ///
124
+ /// When the result of `f` is an [`Error`] without a response,
125
+ /// this function uses [`Response::internal_server_errror_500`] to make one.
126
+ ///
127
+ /// Returns the response.
128
+ ///
129
+ /// Clears thread-local log tags.
130
+ ///
131
+ /// # Errors
132
+ /// Returns `Err` when the global logger has stopped.
133
+ #[ allow( clippy:: module_name_repetitions) ]
134
+ pub fn log_request_and_response < F : FnOnce ( Request ) -> Result < Response , Error > > (
135
+ req : Request ,
136
+ f : F ,
137
+ ) -> Response {
138
+ clear_thread_local_log_tags ( ) ;
139
+ add_thread_local_log_tags_from_request ( & req) ;
140
+ let response = log_response ( f ( req) ) . unwrap ( ) ;
141
+ clear_thread_local_log_tags ( ) ;
142
+ response
143
+ }
0 commit comments