Skip to content

Commit

Permalink
Merge pull request #39 from hanghae-plus-2nd/develop
Browse files Browse the repository at this point in the history
feat : 로깅 MDC 설정
  • Loading branch information
zzangoobrother authored Oct 25, 2023
2 parents b7a2499 + eae4e2c commit bf0f23d
Show file tree
Hide file tree
Showing 22 changed files with 156 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import hanghae.four.taxiservice.domain.pay.PaymentCommand
import hanghae.four.taxiservice.domain.pay.PaymentService
import hanghae.four.taxiservice.domain.taxi.TaxiService
import hanghae.four.taxiservice.domain.taxi.call.CallService
import hanghae.four.taxiservice.util.annotations.Facade
import hanghae.four.taxiservice.global.annotations.Facade
import org.springframework.transaction.annotation.Transactional

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package hanghae.four.taxiservice.application.taxi
import hanghae.four.taxiservice.domain.taxi.RegisterTaxi
import hanghae.four.taxiservice.domain.taxi.TaxiService
import hanghae.four.taxiservice.domain.taxi.call.driver.DriverService
import hanghae.four.taxiservice.util.annotations.Facade
import hanghae.four.taxiservice.global.annotations.Facade

@Facade
class TaxiFacade(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import hanghae.four.taxiservice.domain.taxi.call.CallCommand
import hanghae.four.taxiservice.domain.taxi.call.CallDetailInfo
import hanghae.four.taxiservice.domain.taxi.call.CallResult
import hanghae.four.taxiservice.domain.taxi.call.CallService
import hanghae.four.taxiservice.util.annotations.Facade
import hanghae.four.taxiservice.domain.taxi.call.CallingClients
import hanghae.four.taxiservice.global.annotations.Facade

@Facade
class CallFacade(
Expand All @@ -23,4 +24,8 @@ class CallFacade(
fun getCallDetailInfo(taxiId: Long): CallDetailInfo {
return callService.getCallDetailInfo(taxiId)
}

fun getCallingClients(): CallingClients {
return callService.getCallingClients()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package hanghae.four.taxiservice.domain.taxi.call
interface CallReader {
fun getById(callId: Long): Call
fun findCall(callId: Long): Call

fun findCallingClients(): List<Call>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.springframework.retry.annotation.Backoff
import org.springframework.retry.annotation.Recover
import org.springframework.retry.annotation.Retryable
import org.springframework.stereotype.Service
import java.util.stream.Collectors

@Service
class CallService(
Expand Down Expand Up @@ -46,4 +47,8 @@ class CallService(
fun findCall(callId: Long): Call {
return callReader.findCall(callId)
}

fun getCallingClients(): CallingClients {
return CallingClients(callReader.findCallingClients().stream().map { it.userId }.collect(Collectors.toList()))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hanghae.four.taxiservice.domain.taxi.call

data class CallingClients(
val clientIdList: List<Long>?,
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hanghae.four.taxiservice.util.annotations
package hanghae.four.taxiservice.global.annotations

import org.springframework.stereotype.Component

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hanghae.four.taxiservice.global.config

import hanghae.four.taxiservice.global.decorator.LoggingTaskDecorator
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.task.TaskExecutor
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

@Configuration
class AsyncConfig {
@Bean
fun taskExecutor(): TaskExecutor {
val taskExecutor = ThreadPoolTaskExecutor()
taskExecutor.corePoolSize = 20
taskExecutor.maxPoolSize = 50
taskExecutor.queueCapacity = 200
taskExecutor.setTaskDecorator(LoggingTaskDecorator())

return taskExecutor
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hanghae.four.taxiservice.util.config
package hanghae.four.taxiservice.global.config

import org.springframework.context.annotation.Configuration
import org.springframework.retry.annotation.EnableRetry
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hanghae.four.taxiservice.global.decorator

import org.slf4j.MDC
import org.springframework.core.task.TaskDecorator

class LoggingTaskDecorator : TaskDecorator {
override fun decorate(runnable: Runnable): Runnable {
val context = MDC.getCopyOfContextMap()
return Runnable {
MDC.setContextMap(context)
runnable.run()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hanghae.four.taxiservice.global.filter

import org.slf4j.MDC
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component
import java.util.UUID
import javax.servlet.Filter
import javax.servlet.FilterChain
import javax.servlet.ServletRequest
import javax.servlet.ServletResponse

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
class MdcLoggingFilter : Filter {
override fun doFilter(request: ServletRequest?, response: ServletResponse?, chain: FilterChain?) {
val uuid = UUID.randomUUID()
MDC.put("request_id", uuid.toString())
chain?.doFilter(request, response)
MDC.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package hanghae.four.taxiservice.infrastructure.pay

import hanghae.four.taxiservice.domain.pay.Payment
import hanghae.four.taxiservice.domain.pay.PaymentStore
import hanghae.four.taxiservice.util.annotations.Store
import hanghae.four.taxiservice.global.annotations.Store

@Store
class PaymentStoreImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package hanghae.four.taxiservice.infrastructure.taxi.call

import hanghae.four.taxiservice.domain.taxi.call.Call
import hanghae.four.taxiservice.domain.taxi.call.CallReader
import hanghae.four.taxiservice.util.annotations.Reader
import hanghae.four.taxiservice.global.annotations.Reader

@Reader
class CallReaderImpl(
Expand All @@ -15,4 +15,8 @@ class CallReaderImpl(
override fun findCall(callId: Long): Call {
return callRepository.getBy(callId)
}

override fun findCallingClients(): List<Call> {
return callRepository.findByStatus(Call.CallStatus.WAITING)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ import org.springframework.data.repository.findByIdOrNull

fun CallRepository.getBy(id: Long) = findByIdOrNull(id) ?: throw CallNotFoundException()

interface CallRepository : JpaRepository<Call, Long>
interface CallRepository : JpaRepository<Call, Long> {
fun findByStatus(status: Call.CallStatus): List<Call>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package hanghae.four.taxiservice.infrastructure.taxi.call

import hanghae.four.taxiservice.domain.taxi.call.Call
import hanghae.four.taxiservice.domain.taxi.call.CallStore
import hanghae.four.taxiservice.util.annotations.Store
import hanghae.four.taxiservice.global.annotations.Store

@Store
class CallStoreImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ class CallApiController(
val detailInfo = callFacade.getCallDetailInfo(callId)
return ResponseEntity.ok(callMapper.mapToCallDetailResponse(detailInfo))
}

@GetMapping("/api/v1/call/clients")
fun getCallingClients(): ResponseEntity<CallingClientsResponse> {
val callingClients = callFacade.getCallingClients()
return ResponseEntity.ok(callMapper.mapToCallingClientsResponse(callingClients))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ data class CallDetailResponse(
val destination: String,
val fare: Int,
)

data class CallingClientsResponse(
val clientIdList: List<Long>?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import hanghae.four.taxiservice.domain.taxi.Taxi
import hanghae.four.taxiservice.domain.taxi.call.CallCommand
import hanghae.four.taxiservice.domain.taxi.call.CallDetailInfo
import hanghae.four.taxiservice.domain.taxi.call.CallResult
import hanghae.four.taxiservice.domain.taxi.call.CallingClients
import hanghae.four.taxiservice.domain.taxi.call.toDriverData
import hanghae.four.taxiservice.domain.taxi.call.toResponse
import hanghae.four.taxiservice.domain.taxi.call.toTaxiData
Expand Down Expand Up @@ -34,4 +35,8 @@ class CallApiMapper {
fun mapToCallDetailResponse(detailInfo: CallDetailInfo): CallDetailResponse? {
return detailInfo.toResponse()
}

fun mapToCallingClientsResponse(callingClients: CallingClients): CallingClientsResponse? {
return CallingClientsResponse(callingClients.clientIdList)
}
}
31 changes: 31 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<configuration>
<springProfile name="prod">
<appender name="taxiCloudwatchAppender"
class="ca.pjer.logback.AwsLogsAppender">
<layout>
<pattern>[%thread] [%date] [%level] [%file:%line] - %msg%n</pattern>
</layout>
<logGroupName>/ecs/hanghea-taxi</logGroupName>
<logStreamUuidPrefix>cloudwatch-log-example-</logStreamUuidPrefix>
<logRegion>ap-northeast-2</logRegion>
<maxBatchLogEvents>50</maxBatchLogEvents>
<maxFlushTimeMillis>30000</maxFlushTimeMillis>
<maxBlockTimeMillis>5000</maxBlockTimeMillis>
<retentionTimeDays>0</retentionTimeDays>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
<level>WARN</level>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} [%thread] [%5level] %logger{35}[%method:%line] %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="taxiCloudwatchAppender"/>
</root>
<root level="WARN">
<appender-ref ref="taxiCloudwatchAppender"/>
</root>
</springProfile>
</configuration>
2 changes: 1 addition & 1 deletion src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} [%thread] [%5level] %logger{35}[%method:%line] %m%n</pattern>
<pattern>%d{HH:mm:ss.SSS} [%thread] [request_id = %X{request_id}] [%5level] %logger{35}[%method:%line] %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package hanghae.four.taxiservice.unit.domain.bookmark

import hanghae.four.taxiservice.domain.bookmark.Bookmark
import hanghae.four.taxiservice.domain.bookmark.BookmarkCommand
import hanghae.four.taxiservice.domain.bookmark.BookmarkReader
import hanghae.four.taxiservice.domain.bookmark.BookmarkService
import hanghae.four.taxiservice.domain.bookmark.BookmarkStore
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
@DisplayName("Unit: BookmarkApiTest")
internal class BookmarkServiceTest {
@Autowired

private val bookmarkReader: BookmarkReader = mockk<BookmarkReader>()
private val bookmarkStore: BookmarkStore = mockk<BookmarkStore>()
private lateinit var bookmarkService: BookmarkService

@BeforeEach
fun setUp() {
bookmarkService = BookmarkService(bookmarkReader, bookmarkStore)
}

@Test
@DisplayName("북마크 등록 정상 처리")
fun `북마크 등록 성공 정상 처리`() {
Expand All @@ -26,6 +36,8 @@ internal class BookmarkServiceTest {
longitude = 127.044529
)

every { bookmarkStore.store(any()) } returns Bookmark(1L, 1234L, "서울시청", 37.501952, 127.044529)

val registerBookmarkResult = bookmarkService.register(bookmarkCommand)

assertEquals(1234L, registerBookmarkResult.clientId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ internal class FakeCallRepository : CallStore, CallReader {
override fun findCall(callId: Long): Call {
return callStore.find { it.id == callId } ?: throw CallNotFoundException()
}

override fun findCallingClients(): List<Call> {
return emptyList<Call>()
}
}

0 comments on commit bf0f23d

Please sign in to comment.