From ba75301617e0f1afe734ee15096fcd70fda81291 Mon Sep 17 00:00:00 2001 From: seokjin8678 Date: Sat, 30 Dec 2023 15:56:04 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=81=AC=EB=A1=A4=EB=9F=AC=20=EB=A1=9C?= =?UTF-8?q?=EA=B9=85=20AOP=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sc/crawler/application/CrawlerLogAop.kt | 19 +++++++++++++++++++ .../sc/crawler/config/CrawlerConfig.kt | 17 +++++++++++------ .../sc/crawler/infra/EngineeringCrawler.kt | 2 +- 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/kr/galaxyhub/sc/crawler/application/CrawlerLogAop.kt diff --git a/src/main/kotlin/kr/galaxyhub/sc/crawler/application/CrawlerLogAop.kt b/src/main/kotlin/kr/galaxyhub/sc/crawler/application/CrawlerLogAop.kt new file mode 100644 index 0000000..02073c8 --- /dev/null +++ b/src/main/kotlin/kr/galaxyhub/sc/crawler/application/CrawlerLogAop.kt @@ -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]}" } + } +} diff --git a/src/main/kotlin/kr/galaxyhub/sc/crawler/config/CrawlerConfig.kt b/src/main/kotlin/kr/galaxyhub/sc/crawler/config/CrawlerConfig.kt index c5d227a..3a1fde5 100644 --- a/src/main/kotlin/kr/galaxyhub/sc/crawler/config/CrawlerConfig.kt +++ b/src/main/kotlin/kr/galaxyhub/sc/crawler/config/CrawlerConfig.kt @@ -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(), + ) + } } diff --git a/src/main/kotlin/kr/galaxyhub/sc/crawler/infra/EngineeringCrawler.kt b/src/main/kotlin/kr/galaxyhub/sc/crawler/infra/EngineeringCrawler.kt index 503720c..3bdf671 100644 --- a/src/main/kotlin/kr/galaxyhub/sc/crawler/infra/EngineeringCrawler.kt +++ b/src/main/kotlin/kr/galaxyhub/sc/crawler/infra/EngineeringCrawler.kt @@ -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,