Skip to content

Commit

Permalink
feat: ExceptionHandler 로깅 디버그일 때 출력, 각 컴포넌트에서 로깅하도록 변경 (#62) (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 authored Dec 30, 2023
1 parent 365b127 commit d5d8c1a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kr.galaxyhub.sc.api.common
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.servlet.http.HttpServletRequest
import kr.galaxyhub.sc.common.exception.GalaxyhubException
import kr.galaxyhub.sc.common.support.LogLevel
import org.springframework.beans.TypeMismatchException
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
Expand Down Expand Up @@ -53,12 +52,7 @@ class ExceptionHandler : ResponseEntityExceptionHandler() {
e: GalaxyhubException,
request: HttpServletRequest,
): ResponseEntity<ApiResponse<Unit>> {
when (e.logLevel) {
LogLevel.ERROR -> log.error(e) { "[🔴ERROR] - (${request.method} ${request.requestURI})" }
LogLevel.WARN -> log.warn(e) { "[🟠WARN] - (${request.method} ${request.requestURI})" }
LogLevel.INFO -> log.info(e) { "[🔵INFO] - (${request.method} ${request.requestURI})" }
LogLevel.DEBUG -> log.debug(e) { "[🟢DEBUG] - (${request.method} ${request.requestURI})" }
}
log.debug(e) { "[🟢DEBUG] - (${request.method} ${request.requestURI})" }
return ResponseEntity(ApiResponse.error(e.message!!), e.httpStatus)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package kr.galaxyhub.sc.common.exception

import kr.galaxyhub.sc.common.support.LogLevel
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

class BadRequestException(
message: String,
httpStatus: HttpStatusCode = HttpStatus.BAD_REQUEST,
logLevel: LogLevel = LogLevel.INFO,
) : GalaxyhubException(message, httpStatus, logLevel)
) : GalaxyhubException(message, httpStatus)
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package kr.galaxyhub.sc.common.exception

import kr.galaxyhub.sc.common.support.LogLevel
import org.springframework.http.HttpStatusCode

sealed class GalaxyhubException(
message: String,
val httpStatus: HttpStatusCode,
val logLevel: LogLevel,
) : RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package kr.galaxyhub.sc.common.exception

import kr.galaxyhub.sc.common.support.LogLevel
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

class InternalServerError(
message: String,
httpStatus: HttpStatusCode = HttpStatus.INTERNAL_SERVER_ERROR,
logLevel: LogLevel = LogLevel.WARN,
) : GalaxyhubException(message, httpStatus, logLevel)
) : GalaxyhubException(message, httpStatus)
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package kr.galaxyhub.sc.common.exception

import kr.galaxyhub.sc.common.support.LogLevel
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

class NotFoundException(
message: String,
httpStatus: HttpStatusCode = HttpStatus.NOT_FOUND,
logLevel: LogLevel = LogLevel.DEBUG,
) : GalaxyhubException(message, httpStatus, logLevel)
) : GalaxyhubException(message, httpStatus)
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package kr.galaxyhub.sc.common.exception

import kr.galaxyhub.sc.common.support.LogLevel
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

class UnauthorizedException(
message: String,
httpStatus: HttpStatusCode = HttpStatus.UNAUTHORIZED,
logLevel: LogLevel = LogLevel.INFO,
) : GalaxyhubException(message, httpStatus, logLevel)
) : GalaxyhubException(message, httpStatus)
9 changes: 0 additions & 9 deletions src/main/kotlin/kr/galaxyhub/sc/common/support/LogLevel.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kr.galaxyhub.sc.crawler.application

import io.github.oshai.kotlinlogging.KotlinLogging
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.AfterThrowing
import org.aspectj.lang.annotation.Aspect
import org.springframework.stereotype.Component

private val log = KotlinLogging.logger {}

@Component
@Aspect
class CrawlerLogAop {

@AfterThrowing(pointcut = "this(Crawler) && execution(* crawling(String))", throwing = "ex")
fun loggingException(joinPoint: JoinPoint, ex: Exception) {
log.warn { "크롤링 중 예외가 발생했습니다. message: ${ex.message} class: ${joinPoint.target}, url: ${joinPoint.args[0]}" }
}
}
17 changes: 11 additions & 6 deletions src/main/kotlin/kr/galaxyhub/sc/crawler/config/CrawlerConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ class CrawlerConfig(
fun crawlers(): Crawlers {
return Crawlers(
listOf(
EngineeringCrawler(
objectMapper = objectMapper,
documentProvider = documentProvider(),
contentParser = markdownHtmlParser(),
introductionParser = plainHtmlParser(),
)
engineeringCrawler(),
)
)
}

@Bean
fun engineeringCrawler(): EngineeringCrawler {
return EngineeringCrawler(
objectMapper = objectMapper,
documentProvider = documentProvider(),
contentParser = markdownHtmlParser(),
introductionParser = plainHtmlParser(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.jsoup.nodes.Element
*
* 글의 내용은 g-narrative-group 태그안의 g-article 태그의 body 속성으로 나타납니다.
*/
class EngineeringCrawler(
open class EngineeringCrawler(
private val objectMapper: ObjectMapper,
private val documentProvider: DocumentProvider,
private val contentParser: HtmlParser,
Expand Down

0 comments on commit d5d8c1a

Please sign in to comment.