@@ -7,6 +7,7 @@ use std::{
77 fmt:: { self , Display as _} ,
88 future:: Future ,
99 marker:: PhantomData ,
10+ ops:: { Bound , RangeBounds } ,
1011 pin:: Pin ,
1112 rc:: Rc ,
1213 task:: { Context , Poll } ,
@@ -23,7 +24,7 @@ use time::{format_description::well_known::Rfc3339, OffsetDateTime};
2324
2425use crate :: {
2526 body:: { BodySize , MessageBody } ,
26- http:: header:: HeaderName ,
27+ http:: { header:: HeaderName , StatusCode } ,
2728 service:: { ServiceRequest , ServiceResponse } ,
2829 Error , Result ,
2930} ;
@@ -89,6 +90,7 @@ struct Inner {
8990 exclude : HashSet < String > ,
9091 exclude_regex : RegexSet ,
9192 log_target : Cow < ' static , str > ,
93+ status_range : ( Bound < StatusCode > , Bound < StatusCode > ) ,
9294}
9395
9496impl Logger {
@@ -99,6 +101,10 @@ impl Logger {
99101 exclude : HashSet :: new ( ) ,
100102 exclude_regex : RegexSet :: empty ( ) ,
101103 log_target : Cow :: Borrowed ( module_path ! ( ) ) ,
104+ status_range : (
105+ Bound :: Included ( StatusCode :: from_u16 ( 100 ) . unwrap ( ) ) ,
106+ Bound :: Included ( StatusCode :: from_u16 ( 999 ) . unwrap ( ) ) ,
107+ ) ,
102108 } ) )
103109 }
104110
@@ -121,6 +127,13 @@ impl Logger {
121127 self
122128 }
123129
130+ /// Set a range of status to include in the logging
131+ pub fn status_range < R : RangeBounds < StatusCode > > ( mut self , status : R ) -> Self {
132+ let inner = Rc :: get_mut ( & mut self . 0 ) . unwrap ( ) ;
133+ inner. status_range = ( status. start_bound ( ) . cloned ( ) , status. end_bound ( ) . cloned ( ) ) ;
134+ self
135+ }
136+
124137 /// Sets the logging target to `target`.
125138 ///
126139 /// By default, the log target is `module_path!()` of the log call location. In our case, that
@@ -242,6 +255,10 @@ impl Default for Logger {
242255 exclude : HashSet :: new ( ) ,
243256 exclude_regex : RegexSet :: empty ( ) ,
244257 log_target : Cow :: Borrowed ( module_path ! ( ) ) ,
258+ status_range : (
259+ Bound :: Included ( StatusCode :: from_u16 ( 100 ) . unwrap ( ) ) ,
260+ Bound :: Included ( StatusCode :: from_u16 ( 999 ) . unwrap ( ) ) ,
261+ ) ,
245262 } ) )
246263 }
247264}
@@ -306,6 +323,7 @@ where
306323 LoggerResponse {
307324 fut : self . service . call ( req) ,
308325 format : None ,
326+ status_range : self . inner . status_range ,
309327 time : OffsetDateTime :: now_utc ( ) ,
310328 log_target : Cow :: Borrowed ( "" ) ,
311329 _phantom : PhantomData ,
@@ -321,6 +339,7 @@ where
321339 LoggerResponse {
322340 fut : self . service . call ( req) ,
323341 format : Some ( format) ,
342+ status_range : self . inner . status_range ,
324343 time : now,
325344 log_target : self . inner . log_target . clone ( ) ,
326345 _phantom : PhantomData ,
@@ -339,6 +358,7 @@ pin_project! {
339358 fut: S :: Future ,
340359 time: OffsetDateTime ,
341360 format: Option <Format >,
361+ status_range: ( Bound <StatusCode >, Bound <StatusCode >) ,
342362 log_target: Cow <' static , str >,
343363 _phantom: PhantomData <B >,
344364 }
@@ -363,22 +383,26 @@ where
363383 debug ! ( "Error in response: {:?}" , error) ;
364384 }
365385
366- let res = if let Some ( ref mut format) = this. format {
367- // to avoid polluting all the Logger types with the body parameter we swap the body
368- // out temporarily since it's not usable in custom response functions anyway
386+ let res = if this. status_range . contains ( & res. status ( ) ) {
387+ if let Some ( ref mut format) = this. format {
388+ // to avoid polluting all the Logger types with the body parameter we swap the body
389+ // out temporarily since it's not usable in custom response functions anyway
369390
370- let ( req, res) = res. into_parts ( ) ;
371- let ( res, body) = res. into_parts ( ) ;
391+ let ( req, res) = res. into_parts ( ) ;
392+ let ( res, body) = res. into_parts ( ) ;
372393
373- let temp_res = ServiceResponse :: new ( req, res. map_into_boxed_body ( ) ) ;
394+ let temp_res = ServiceResponse :: new ( req, res. map_into_boxed_body ( ) ) ;
374395
375- for unit in & mut format. 0 {
376- unit. render_response ( & temp_res) ;
377- }
396+ for unit in & mut format. 0 {
397+ unit. render_response ( & temp_res) ;
398+ }
378399
379- // re-construct original service response
380- let ( req, res) = temp_res. into_parts ( ) ;
381- ServiceResponse :: new ( req, res. set_body ( body) )
400+ // re-construct original service response
401+ let ( req, res) = temp_res. into_parts ( ) ;
402+ ServiceResponse :: new ( req, res. set_body ( body) )
403+ } else {
404+ res
405+ }
382406 } else {
383407 res
384408 } ;
0 commit comments