vegemite4me
vegemite4me

Reputation: 6856

Feign Client: How to log the server name which the request was sent to?

I am using a Feign Client to call a REST endpoint with success, and have logging turned on to FULL. This is helpfully shows me the request sent and the response received. However, I cannot see which server the request was sent to. It only shows me that it was POSTed to http://foo-service which is the name of the service, not the name of the server.

How can I log which server name this request was sent to?

This is what I see in the logs:

c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] ---> POST http://foo-service/some-endoint HTTP/1.1
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Accept: application/json
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Content-Type: application/json
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Content-Length: 15
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] 
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] {"name":"John"}
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] ---> END HTTP (15-byte body)
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] <--- HTTP/1.1 200 (8ms)
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Transfer-Encoding: chunked
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] X-Application-Context: fooService:9006
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Date: Tue, 10 Oct 2017 09:25:36 GMT
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Content-Type: application/json;charset=UTF-8
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] 
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] {"result":"Hello John"}
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] <--- END HTTP (23-byte body)

To turn on logging, I declare this bean:

@Bean
public feign.Logger.Level feignLoggerLevel() {
    return Logger.Level.FULL;
}

We are using spring-cloud-netflix-core:1.1.0.M4

Upvotes: 1

Views: 4263

Answers (2)

Manish
Manish

Reputation: 1775

Enable logging via logback and set log level of debug or these classes

<logger name="com.netflix.loadbalancer.BaseLoadBalancer" level="DEBUG"/>
<logger name="com.netflix.loadbalancer.LoadBalancerContext" level="DEBUG"/>
<logger name="com.netflix.loadbalancer.reactive.LoadBalancerCommand" level="DEBUG"/>

I can see where each request is going (server:port) and request failed on which server

DEBUG [          c.a.m.c.f.CommonProducerClient][72] - [CommonProducerClient#findAllEmployee] ---> GET http://COMMON-PRODUCER/allemployee HTTP/1.1
DEBUG [    c.n.loadbalancer.LoadBalancerContext][492] - COMMON-PRODUCER using LB returned Server: localhost:7001 for request http:///allemployee
DEBUG [      c.n.l.reactive.LoadBalancerCommand][314] - Got error java.net.ConnectException: Connection refused: connect when executed on server localhost:7001
DEBUG [      c.n.l.reactive.LoadBalancerCommand][314] - Got error java.net.ConnectException: Connection refused: connect when executed on server localhost:7001
DEBUG [      c.n.l.reactive.LoadBalancerCommand][314] - Got error java.net.ConnectException: Connection refused: connect when executed on server localhost:7001
DEBUG [    c.n.loadbalancer.LoadBalancerContext][492] - COMMON-PRODUCER using LB returned Server: localhost:7003 for request http:///allemployee
DEBUG [          c.a.m.c.f.CommonProducerClient][72] - [CommonProducerClient#findAllEmployee] <--- HTTP/1.1 200 (4010ms)

From Logs its clear, first request went to port 7001, connection refused there, second request goes to 7003, all good there.

Upvotes: 0

Kevin Davis
Kevin Davis

Reputation: 1293

You will need to provide your own Logger to achieve this. The default Logger is very simple and logs the host before the request is sent. When using Ribbon the information will be available with the Response. If you override the logAndRebufferResponse method, the response.url property will be the entire url submitted, including host name.

protected Response logAndRebufferResponse(
    String configKey, 
    Level logLevel, 
    Response response,
    long elapsedTime) throws IOException {

    /* ask the response for the request and log the uri */
    log(response.request.url());
}

Upvotes: 2

Related Questions