From 7923c52d49e9558b83bbdd2fcd6964790e2d4bf1 Mon Sep 17 00:00:00 2001 From: "semin.baek" Date: Wed, 25 Jun 2025 09:17:00 +0900 Subject: [PATCH] =?UTF-8?q?access=20log=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/config/AccessLogConfiguration.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/com/bsmlab/dfx/agent/config/AccessLogConfiguration.java diff --git a/src/main/java/com/bsmlab/dfx/agent/config/AccessLogConfiguration.java b/src/main/java/com/bsmlab/dfx/agent/config/AccessLogConfiguration.java new file mode 100644 index 0000000..f302e27 --- /dev/null +++ b/src/main/java/com/bsmlab/dfx/agent/config/AccessLogConfiguration.java @@ -0,0 +1,56 @@ +package com.bsmlab.dfx.agent.config; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.catalina.valves.AccessLogValve; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.nio.file.Paths; +import java.time.Duration; + +@Slf4j +@Component +@RequiredArgsConstructor +public class AccessLogConfiguration implements WebServerFactoryCustomizer { + private final AgentConfigReader agentConfigReader; + + /** + * Access log 설정 + * @param factory + */ + @Override + public void customize(TomcatServletWebServerFactory factory) { + AgentConfigDto agentConfigDto = this.agentConfigReader.getAgentConfigDto(); + String logFilePath = Paths.get(agentConfigDto.getLogging().getLogDirectory(), "access").toString(); + AccessLogValve accessLogValve = new AccessLogValve(); + accessLogValve.setPattern("combined"); + accessLogValve.setDirectory(logFilePath); + accessLogValve.setSuffix(".log"); + accessLogValve.setRotatable(true); + accessLogValve.setRenameOnRotate(true); + accessLogValve.setFileDateFormat("yyyy-MM-dd"); + accessLogValve.setRequestAttributesEnabled(true); + factory.addContextValves(accessLogValve); + } + + /** + * Access log 삭제. 설정 파일의 maxHistory 기준 + */ + @Scheduled(cron = "0 0 3 * * *") + public void cleanAccessLog() { + AgentConfigDto agentConfigDto = this.agentConfigReader.getAgentConfigDto(); + File logFileDirFile = new File(Paths.get(agentConfigDto.getLogging().getLogDirectory()).toString()); + File[] files = logFileDirFile.listFiles((dir, name) -> name.startsWith("access.")); + long maxHistory = agentConfigDto.getLogging().getMaxHistory(); + long cutoff = System.currentTimeMillis() - Duration.ofDays(maxHistory).toMillis(); + for(File file : files) { + if(file.lastModified() < cutoff) { + file.delete(); + } + } + } +}