@@ -393,32 +393,33 @@ where
393
393
debug ! ( "Error in response: {:?}" , error) ;
394
394
}
395
395
396
- let res = if this. status_range . contains ( & res. status ( ) ) {
397
- if let Some ( ref mut format) = this. format {
398
- // to avoid polluting all the Logger types with the body parameter we swap the body
399
- // out temporarily since it's not usable in custom response functions anyway
396
+ let mut format = if this. status_range . contains ( & res. status ( ) ) {
397
+ this. format . take ( )
398
+ } else {
399
+ None
400
+ } ;
400
401
401
- let ( req, res) = res. into_parts ( ) ;
402
- let ( res, body) = res. into_parts ( ) ;
402
+ let res = if let Some ( ref mut format) = format {
403
+ // to avoid polluting all the Logger types with the body parameter we swap the body
404
+ // out temporarily since it's not usable in custom response functions anyway
403
405
404
- let temp_res = ServiceResponse :: new ( req, res. map_into_boxed_body ( ) ) ;
406
+ let ( req, res) = res. into_parts ( ) ;
407
+ let ( res, body) = res. into_parts ( ) ;
405
408
406
- for unit in & mut format. 0 {
407
- unit. render_response ( & temp_res) ;
408
- }
409
+ let temp_res = ServiceResponse :: new ( req, res. map_into_boxed_body ( ) ) ;
409
410
410
- // re-construct original service response
411
- let ( req, res) = temp_res. into_parts ( ) ;
412
- ServiceResponse :: new ( req, res. set_body ( body) )
413
- } else {
414
- res
411
+ for unit in & mut format. 0 {
412
+ unit. render_response ( & temp_res) ;
415
413
}
414
+
415
+ // re-construct original service response
416
+ let ( req, res) = temp_res. into_parts ( ) ;
417
+ ServiceResponse :: new ( req, res. set_body ( body) )
416
418
} else {
417
419
res
418
420
} ;
419
421
420
422
let time = * this. time ;
421
- let format = this. format . take ( ) ;
422
423
let log_target = this. log_target . clone ( ) ;
423
424
424
425
Poll :: Ready ( Ok ( res. map_body ( move |_, body| StreamLog {
@@ -779,7 +780,13 @@ mod tests {
779
780
header:: HeaderValue :: from_static ( "ACTIX-WEB" ) ,
780
781
) )
781
782
. to_srv_request ( ) ;
782
- let _res = srv. call ( req) . await ;
783
+ capture_logger:: begin_capture ( ) ;
784
+ // The log is executed on drop, so the result need to be dropped
785
+ let _ = srv. call ( req) . await ;
786
+ let log = capture_logger:: pop_captured ( ) . unwrap ( ) ;
787
+ assert ! ( log. message( ) . contains( "ttt" ) ) ;
788
+ assert ! ( log. message( ) . contains( "ACTIX-WEB" ) ) ;
789
+ capture_logger:: end_capture ( ) ;
783
790
}
784
791
785
792
#[ actix_rt:: test]
@@ -805,6 +812,54 @@ mod tests {
805
812
let _res = srv. call ( req) . await . unwrap ( ) ;
806
813
}
807
814
815
+ #[ actix_rt:: test]
816
+ async fn test_logger_status_range_include ( ) {
817
+ let srv = |req : ServiceRequest | {
818
+ ok ( req. into_response ( HttpResponse :: build ( StatusCode :: OK ) . finish ( ) ) )
819
+ } ;
820
+ let logger = Logger :: new ( "%{User-Agent}i test_included %s" ) . statuses ( StatusCode :: OK ..) ;
821
+
822
+ let srv = logger. new_transform ( srv. into_service ( ) ) . await . unwrap ( ) ;
823
+
824
+ let req = TestRequest :: default ( )
825
+ . insert_header ( (
826
+ header:: USER_AGENT ,
827
+ header:: HeaderValue :: from_static ( "ACTIX-WEB" ) ,
828
+ ) )
829
+ . to_srv_request ( ) ;
830
+ capture_logger:: begin_capture ( ) ;
831
+ // The log is executed on drop, so the result need to be dropped
832
+ let _ = srv. call ( req) . await ;
833
+ let log = capture_logger:: pop_captured ( ) . unwrap ( ) ;
834
+ assert ! ( log. message( ) . contains( "200" ) ) ;
835
+ assert ! ( log. message( ) . contains( "ACTIX-WEB" ) ) ;
836
+ capture_logger:: end_capture ( ) ;
837
+ }
838
+
839
+ #[ actix_rt:: test]
840
+ async fn test_logger_status_range_exclude ( ) {
841
+ let srv = |req : ServiceRequest | {
842
+ ok ( req. into_response ( HttpResponse :: build ( StatusCode :: OK ) . finish ( ) ) )
843
+ } ;
844
+ let logger =
845
+ Logger :: new ( "%{User-Agent}i test_excluded %s" ) . statuses ( StatusCode :: BAD_REQUEST ..) ;
846
+
847
+ let srv = logger. new_transform ( srv. into_service ( ) ) . await . unwrap ( ) ;
848
+
849
+ let req = TestRequest :: default ( )
850
+ . insert_header ( (
851
+ header:: USER_AGENT ,
852
+ header:: HeaderValue :: from_static ( "ACTIX-WEB" ) ,
853
+ ) )
854
+ . to_srv_request ( ) ;
855
+ capture_logger:: begin_capture ( ) ;
856
+ // The log is executed on drop, so the result need to be dropped
857
+ let _ = srv. call ( req) . await ;
858
+ let log = capture_logger:: pop_captured ( ) ;
859
+ assert ! ( log. is_none( ) ) ;
860
+ capture_logger:: end_capture ( ) ;
861
+ }
862
+
808
863
#[ actix_rt:: test]
809
864
async fn test_escape_percent ( ) {
810
865
let mut format = Format :: new ( "%%{r}a" ) ;
0 commit comments