File tree 4 files changed +14
-12
lines changed
4 files changed +14
-12
lines changed Original file line number Diff line number Diff line change @@ -184,13 +184,11 @@ async fn log_response_middleware(
184
184
185
185
let response = next. run ( request) . await ;
186
186
187
- let Some ( log_context ) = LogContext :: current ( ) else {
187
+ let Some ( stats ) = LogContext :: maybe_with ( LogContext :: stats ) else {
188
188
tracing:: error!( "Missing log context for request, this is a bug!" ) ;
189
189
return response;
190
190
} ;
191
191
192
- let stats = log_context. stats ( ) ;
193
-
194
192
let status_code = response. status ( ) ;
195
193
match status_code. as_u16 ( ) {
196
194
100 ..=399 => tracing:: info!(
Original file line number Diff line number Diff line change @@ -113,13 +113,14 @@ where
113
113
write ! ( & mut writer, "{} " , style. apply_to( metadata. name( ) ) ) ?;
114
114
}
115
115
116
- if let Some ( log_context ) = LogContext :: current ( ) {
116
+ LogContext :: maybe_with ( |log_context| {
117
117
let log_context = Style :: new ( )
118
118
. bold ( )
119
119
. force_styling ( ansi)
120
120
. apply_to ( log_context) ;
121
- write ! ( & mut writer, "{log_context} - " ) ?;
122
- }
121
+ write ! ( & mut writer, "{log_context} - " )
122
+ } )
123
+ . transpose ( ) ?;
123
124
124
125
let field_fromatter = DefaultFields :: new ( ) ;
125
126
field_fromatter. format_fields ( writer. by_ref ( ) , event) ?;
Original file line number Diff line number Diff line change @@ -76,9 +76,12 @@ impl LogContext {
76
76
}
77
77
}
78
78
79
- /// Get a copy of the current log context, if any
80
- pub fn current ( ) -> Option < Self > {
81
- CURRENT_LOG_CONTEXT . try_with ( Self :: clone) . ok ( )
79
+ /// Run a closure with the current log context, if any
80
+ pub fn maybe_with < F , R > ( f : F ) -> Option < R >
81
+ where
82
+ F : FnOnce ( & Self ) -> R ,
83
+ {
84
+ CURRENT_LOG_CONTEXT . try_with ( f) . ok ( )
82
85
}
83
86
84
87
/// Run the async function `f` with the given log context. It will wrap the
Original file line number Diff line number Diff line change @@ -789,14 +789,14 @@ impl JobTracker {
789
789
) ;
790
790
let result = job. run ( & state, context. clone ( ) ) . await ;
791
791
792
- let Some ( log_context) = LogContext :: current ( ) else {
792
+ let Some ( context_stats) =
793
+ LogContext :: maybe_with ( mas_context:: LogContext :: stats)
794
+ else {
793
795
// This should never happen, but if it does it's fine: we're recovering fine
794
796
// from panics in those tasks
795
797
panic ! ( "Missing log context, this should never happen" ) ;
796
798
} ;
797
799
798
- let context_stats = log_context. stats ( ) ;
799
-
800
800
// We log the result here so that it's attached to the right span & log context
801
801
match & result {
802
802
Ok ( ( ) ) => {
You can’t perform that action at this time.
0 commit comments