En ciertas ocasiones, al desarrollar una nueva comunicación entre APIs, es útil poder hacer logging de las peticiones y respuestas que se realizan desde un cliente REST.

En esta página se enumeran algunas alternativas para pintar esta información en el log de nuestra aplicación en Spring-Boot.


Esta página es la equivalente en Spring a la WiKi Logear Mensajes In/OUT en FundeWeb.

Guía detallada

A continuación se detallan las distintas configuraciones que permiten realizar el logging de peticiones y respuestas en clientes REST.

1. Logging básico con RestTemplate

Es muy fácil de configurar pero proporciona poca información:


Configuración: 

Añadir en el fichero application.properties:

##Configuración RestTemplate para logging básico de request y response en clientes
logging.level.org.springframework.web.client.RestTemplate=DEBUG


Cliente con RestTemplate:

	final RestTemplate plantilla = new RestTemplate();
	final HttpHeaders headers = new HttpHeaders();
	headers.set( "Cabecera-test", "Valor de test" );
	headers.set( "Cabecera-test2", "Valor de test 2" );
	final HttpEntity<String> entity = new HttpEntity<>( "Body de test", headers );

	final String url = "https://fundewebjsshowcasedesa.um.es/api/v1.0/public/test/CS";

	final ResponseEntity<String> response = plantilla.exchange( url, HttpMethod.GET, entity, String.class );


Ejemplo de log de petición:

13-05-2021 13:25:44.165 DEBUG 12864 --- [nio-8080-exec-1] o.s.w.c.RestTemplate                     : HTTP GET https://fundewebjsshowcasedesa.um.es/api/v1.0/public/test/CS
13-05-2021 13:25:44.166 DEBUG 12864 --- [nio-8080-exec-1] o.s.w.c.RestTemplate                     : Accept=[text/plain, application/json, application/*+json, */*]
13-05-2021 13:25:44.166 DEBUG 12864 --- [nio-8080-exec-1] o.s.w.c.RestTemplate                     : Writing [Body de test] with org.springframework.http.converter.StringHttpMessageConverter
13-05-2021 13:25:44.197 DEBUG 12864 --- [nio-8080-exec-1] o.s.w.c.RestTemplate                     : Response 200 OK
13-05-2021 13:25:44.197 DEBUG 12864 --- [nio-8080-exec-1] o.s.w.c.RestTemplate                     : Reading to [java.lang.String] as "application/json"



2. Logging completo con Apache HttpComponents.

	<dependency>
	    <groupId>org.apache.httpcomponents</groupId>
	    <artifactId>httpclient</artifactId>
	</dependency>


logging.level.org.apache.http=DEBUG
#logging.level.org.apache.http.wire=DEBUG

final RestTemplate plantilla = new RestTemplate();
plantilla.setRequestFactory( new HttpComponentsClientHttpRequestFactory() );



13-05-2021 13:55:12.690 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.p.RequestAddCookies              : CookieSpec selected: default
13-05-2021 13:55:12.690 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.p.RequestAuthCache               : Auth cache not set in the context
13-05-2021 13:55:12.690 DEBUG 12864 --- [nio-8080-exec-4] h.i.c.PoolingHttpClientConnectionManager : Connection request: [route: {s}->https://micampusdesa.um.es:443][total available: 0; route allocated: 0 of 5; total allocated: 0 of 10]
13-05-2021 13:55:12.690 DEBUG 12864 --- [nio-8080-exec-4] h.i.c.PoolingHttpClientConnectionManager : Connection leased: [id: 7][route: {s}->https://micampusdesa.um.es:443][total available: 0; route allocated: 1 of 5; total allocated: 1 of 10]
13-05-2021 13:55:12.690 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.i.e.MainClientExec                 : Opening connection {s}->https://micampusdesa.um.es:443
13-05-2021 13:55:12.690 DEBUG 12864 --- [nio-8080-exec-4] .i.c.DefaultHttpClientConnectionOperator : Connecting to micampusdesa.um.es/155.54.228.131:443
13-05-2021 13:55:12.690 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     : Connecting socket to micampusdesa.um.es/155.54.228.131:443 with timeout 0
13-05-2021 13:55:12.692 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     : Enabled protocols: [TLSv1.2, TLSv1.1, TLSv1]
13-05-2021 13:55:12.692 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     : Enabled cipher suites:[TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, TLS_RSA_WITH_AES_256_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, TLS_DHE_DSS_WITH_AES_256_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_DSS_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
13-05-2021 13:55:12.692 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     : Starting handshake
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     : Secure session established
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     :  negotiated protocol: TLSv1.2
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     :  negotiated cipher suite: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     :  peer principal: CN=micampusdesa.um.es, OU=ATICA, O=Universidad de Murcia, L=Murcia, ST=Murcia, C=ES
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     :  peer alternative names: [micampusdesa.um.es, www.micampusdesa.um.es]
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.c.s.SSLConnectionSocketFactory     :  issuer principal: CN=GEANT OV RSA CA 4, O=GEANT Vereniging, C=NL
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] .i.c.DefaultHttpClientConnectionOperator : Connection established 155.54.67.188:58123<->155.54.228.131:443
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.i.e.MainClientExec                 : Executing request GET /consultaexpedientes/rest/v1.0/private/test/CS HTTP/1.1
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.i.e.MainClientExec                 : Target auth state: UNCHALLENGED
13-05-2021 13:55:12.698 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.i.e.MainClientExec                 : Proxy auth state: UNCHALLENGED
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> GET /consultaexpedientes/rest/v1.0/private/test/CS HTTP/1.1
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> Accept: text/plain, application/json, application/*+json, */*
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> Header: value
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> Other-Header: othervalue
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> Content-Type: text/plain;charset=ISO-8859-1
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> Host: micampusdesa.um.es
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> Connection: Keep-Alive
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> User-Agent: Apache-HttpClient/4.5.11 (Java/1.8.0_281)
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 >> Accept-Encoding: gzip,deflate
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "GET /consultaexpedientes/rest/v1.0/private/test/CS HTTP/1.1[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "Accept: text/plain, application/json, application/*+json, */*[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "Header: value[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "Other-Header: othervalue[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "Content-Type: text/plain;charset=ISO-8859-1[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "Host: micampusdesa.um.es[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "Connection: Keep-Alive[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "User-Agent: Apache-HttpClient/4.5.11 (Java/1.8.0_281)[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "Accept-Encoding: gzip,deflate[\r][\n]"
13-05-2021 13:55:12.699 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 >> "[\r][\n]"
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
13-05-2021 13:55:12.704 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "HTTP/1.1 200 [\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Server: nginx/1.19.2[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Date: Thu, 13 May 2021 11:55:12 GMT[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Content-Type: application/json[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Content-Length: 2[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Connection: keep-alive[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Vary: Origin[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Vary: Access-Control-Request-Method[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Vary: Access-Control-Request-Headers[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Vary: Origin[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Vary: Access-Control-Request-Method[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Vary: Access-Control-Request-Headers[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "X-Content-Type-Options: nosniff[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "X-XSS-Protection: 1; mode=block[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Cache-Control: no-cache, no-store, max-age=0, must-revalidate[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Pragma: no-cache[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Expires: 0[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "X-Frame-Options: SAMEORIGIN[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Access-Control-Allow-Origin: *[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Access-Control-Allow-Credentials: true[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Access-Control-Allow-Methods: GET, PUT, POST, DELETE, PATCH, OPTIONS[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Set-Cookie[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "[\r][\n]"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.wire                               : http-outgoing-7 << "CS"
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << HTTP/1.1 200 
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Server: nginx/1.19.2
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Date: Thu, 13 May 2021 11:55:12 GMT
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Content-Type: application/json
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Content-Length: 2
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Connection: keep-alive
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Vary: Origin
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Vary: Access-Control-Request-Method
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Vary: Access-Control-Request-Headers
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Vary: Origin
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Vary: Access-Control-Request-Method
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Vary: Access-Control-Request-Headers
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << X-Content-Type-Options: nosniff
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << X-XSS-Protection: 1; mode=block
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Cache-Control: no-cache, no-store, max-age=0, must-revalidate
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Pragma: no-cache
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Expires: 0
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << X-Frame-Options: SAMEORIGIN
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Access-Control-Allow-Origin: *
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Access-Control-Allow-Credentials: true
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Access-Control-Allow-Methods: GET, PUT, POST, DELETE, PATCH, OPTIONS
13-05-2021 13:55:12.705 DEBUG 12864 --- [nio-8080-exec-4] o.a.h.headers                            : http-outgoing-7 << Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Set-Cookie



Referencias:

Artículos Relacionados

Aquí aparecen artículos relacionados sobre la base de las etiquetas que usted seleccione. Haga clic para editar la macro y añadir o modificar las etiquetas.



Incidencias similares