택시짱의 개발 노트

Spring Cloud OpenFeign의 Logging 방법 본문

spring

Spring Cloud OpenFeign의 Logging 방법

택시짱 2023. 7. 7. 09:57

개요

- MSA 환경에서 다른 server로 요청 보내야할때 spring cloud openfeign을 이용하여 정보를 얻고 있는데 종종 의도한 동작을 하지 않을때가 있다. 예를들면 open feign에 설정한 timeout 을 초과 했을때, 또는 인증 문제, 등등

 

open feign에 logging을 남기기 전에는 내가 작성한 코드가 문제인지 open feign에서 문제 인지 단정하기 어려웠다.. 그래서 open feign에 logging을 달기 시작했다.

 

1. FeignConfiguration 작성

import feign.Logger
import org.springframework.cloud.openfeign.EnableFeignClients
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@EnableFeignClients
class FeignConfiguration{
    @Bean
    fun feignLoggerLevel(): Logger.Level {
        return feign.Logger.Level.FULL
    }
}

 

2. Log를 남길 FeignClient Annotaion에 Configuration 등록

- 만약에 한개의 FeignConfiguration를 전역으로 사용한다면 추가하지 않아도 됩니다. )

package com.taxijjang.notification.application.port.out

import com.fasterxml.jackson.databind.ObjectMapper
import com.quriously.epictower.notification.application.configure.FeignConfiguration
import org.springframework.cloud.openfeign.FeignClient

@FeignClient(name = "ncloud", url = "https://sens.apigw.ntruss.com", configuration = [FeignConfiguration::class])
interface NCloudSmsFeignClient {
    @PostMapping(
        "/sms/v2/services/{serviceId}/messages",
        consumes = ["application/json; charset=utf-8"],
        produces = ["application/json; charset=utf-8"]
    )
    fun sendMessage(
        @RequestHeader headers: HttpHeaders,
        @PathVariable("serviceId") serviceId: String,
        @RequestBody body: String,
    ): String
}

 

3. application.yml 파일에 Log를 남길 FeignClient 경로 지정

- 저는 local 환경에서만 log를 보려고 on-profie: local 일때 설정 했습니다.

spring:
  config:
    activate:
      on-profile: local
logging:
  level:
    package com.taxijjang.notification.application.port.out.NCloudSmsFeignClient: DEBUG

 

4. 결과

- request, response를 명확하게 확인이 가능

[NCloudSmsFeignClient#sendMessage] ---> POST https://sens.apigw.ntruss.com/sms/v2/services/ncp%3Asms%3Akr%3A260252380873%3Aqube/messages HTTP/1.1
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] Accept: application/json; charset=utf-8
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] Content-Length: 199
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] Content-Type: application/json; charset=utf-8
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] x-ncp-apigw-signature-v2: {삭제처리}
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] x-ncp-apigw-timestamp: 1688122515681
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] x-ncp-iam-access-key: {삭제처리}
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] 
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] {"type":"SMS","contentType":"COMM","countryCode":"82","from":"{삭제처리}","content":"인증번호는 123456 입니다.","messages":[{"content":"인증번호는 123456 입니다.","to":"{삭제처리}"}]}
2023-06-30 19:55:15.698 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] ---> END HTTP (199-byte body)
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] <--- HTTP/1.1 401 Unauthorized (107ms)
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] connection: keep-alive
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] content-length: 111
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] content-type: application/json; charset=utf-8
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] date: Fri, 30 Jun 2023 10:55:15 GMT
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] server: nginx
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] x-ncp-apigw-response-origin: APIGW
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] x-ncp-trace-id: 32dj4cos3cp1o34dhp75hmcp1i
2023-06-30 19:55:15.806 DEBUG 29853 --- [nio-8081-exec-1] c.q.e.n.a.port.out.NCloudSmsFeignClient  : [NCloudSmsFeignClient#sendMessage] <--- END HTTP (0-byte body)
반응형
Comments