File tree Expand file tree Collapse file tree 4 files changed +11
-12
lines changed
Expand file tree Collapse file tree 4 files changed +11
-12
lines changed Original file line number Diff line number Diff line change @@ -184,13 +184,11 @@ async fn log_response_middleware(
184184
185185 let response = next. run ( request) . await ;
186186
187- let Some ( log_context ) = LogContext :: current ( ) else {
187+ let Some ( stats ) = LogContext :: maybe_with ( LogContext :: stats ) else {
188188 tracing:: error!( "Missing log context for request, this is a bug!" ) ;
189189 return response;
190190 } ;
191191
192- let stats = log_context. stats ( ) ;
193-
194192 let status_code = response. status ( ) ;
195193 match status_code. as_u16 ( ) {
196194 100 ..=399 => tracing:: info!(
Original file line number Diff line number Diff line change @@ -113,13 +113,13 @@ where
113113 write ! ( & mut writer, "{} " , style. apply_to( metadata. name( ) ) ) ?;
114114 }
115115
116- if let Some ( log_context ) = LogContext :: current ( ) {
116+ LogContext :: maybe_with ( |log_context| {
117117 let log_context = Style :: new ( )
118118 . bold ( )
119119 . force_styling ( ansi)
120120 . apply_to ( log_context) ;
121- write ! ( & mut writer, "{log_context} - " ) ? ;
122- }
121+ write ! ( & mut writer, "{log_context} - " )
122+ } ) . transpose ( ) ? ;
123123
124124 let field_fromatter = DefaultFields :: new ( ) ;
125125 field_fromatter. format_fields ( writer. by_ref ( ) , event) ?;
Original file line number Diff line number Diff line change @@ -76,9 +76,12 @@ impl LogContext {
7676 }
7777 }
7878
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 ( )
8285 }
8386
8487 /// 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,12 @@ impl JobTracker {
789789 ) ;
790790 let result = job. run ( & state, context. clone ( ) ) . await ;
791791
792- let Some ( log_context ) = LogContext :: current ( ) else {
792+ let Some ( context_stats ) = LogContext :: maybe_with ( mas_context :: LogContext :: stats ) else {
793793 // This should never happen, but if it does it's fine: we're recovering fine
794794 // from panics in those tasks
795795 panic ! ( "Missing log context, this should never happen" ) ;
796796 } ;
797797
798- let context_stats = log_context. stats ( ) ;
799-
800798 // We log the result here so that it's attached to the right span & log context
801799 match & result {
802800 Ok ( ( ) ) => {
You can’t perform that action at this time.
0 commit comments