-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add logging * feat: save log file * chore: edit gitignore * chore: edit gitignore * fix: post api search
- Loading branch information
Showing
7 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
HELP.md | ||
.gradle | ||
logs/ | ||
build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
!**/src/main/**/build/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.kert.config; | ||
|
||
import org.aspectj.lang.annotation.AfterThrowing; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.stereotype.Component; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
@Aspect | ||
@Component | ||
public class LoggingAspect { | ||
|
||
|
||
|
||
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class); | ||
|
||
@Pointcut("within(@org.springframework.web.bind.annotation.RestController *) || within(@org.springframework.stereotype.Controller *)") | ||
public void controllerMethods() {} | ||
|
||
|
||
@Before("controllerMethods()") | ||
public void logBeforeRequest() { | ||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); | ||
String clientIp = getClientIpAddress(request); | ||
String uri = request.getRequestURI(); | ||
String method = request.getMethod(); | ||
logger.info("Request - Method: {}, URI: {}, Client IP: {}", method, uri, clientIp); | ||
} | ||
|
||
@AfterThrowing(pointcut = "controllerMethods()", throwing = "exception") | ||
public void logAfterThrowing(Exception exception) { | ||
logger.error("An error occurred: {}", exception.getMessage()); | ||
} | ||
|
||
// 클라이언트 IP를 확인하는 메서드 | ||
private String getClientIpAddress(HttpServletRequest request) { | ||
String clientIp = request.getHeader("X-Forwarded-For"); | ||
if (clientIp == null || clientIp.isEmpty() || "unknown".equalsIgnoreCase(clientIp)) { | ||
clientIp = request.getHeader("X-Real-IP"); | ||
} | ||
if (clientIp == null || clientIp.isEmpty() || "unknown".equalsIgnoreCase(clientIp)) { | ||
clientIp = request.getRemoteAddr(); | ||
} else { | ||
clientIp = clientIp.split(",")[0]; // 여러 IP 값이 있을 경우 첫 번째 값만 사용 | ||
} | ||
return clientIp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters