-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5f99d7
commit f400de7
Showing
2 changed files
with
53 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package life.offonoff.ab.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.EnableAspectJAutoProxy; | ||
|
||
@Configuration | ||
@EnableAspectJAutoProxy | ||
public class AopConfig { | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/life/offonoff/ab/web/common/aspect/LoggingAspectHandler.java
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,44 @@ | ||
package life.offonoff.ab.web.common.aspect; | ||
|
||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
@Aspect | ||
public class LoggingAspectHandler { | ||
private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName()); | ||
@Pointcut("execution(* life.offonoff.ab.web.*.*(..))") | ||
private void allControllers() { | ||
} | ||
|
||
@Around("allControllers()") | ||
public Object doReturnLogging(ProceedingJoinPoint joinPoint) throws Throwable { | ||
String methodName = joinPoint.getSignature().toShortString(); | ||
Object[] arguments = joinPoint.getArgs(); | ||
|
||
log.info("[BEFORE] METHOD={} PARAMETER={} WILL EXECUTE", methodName, arguments); | ||
|
||
Object returnedByMethod = joinPoint.proceed(arguments); | ||
|
||
log.info("[RETURNED] METHOD={} PARAMETER={} RETURNED={}", joinPoint.getSignature().toShortString(), joinPoint.getArgs(), returnedByMethod); | ||
|
||
return returnedByMethod; | ||
} | ||
|
||
@AfterThrowing(value = "allControllers()", throwing = "exception") | ||
public void doExceptionLogging(JoinPoint joinPoint, Exception exception) { | ||
String ip = Optional.ofNullable((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | ||
.map(attributes -> attributes.getRequest().getHeader("X-Real-IP")) | ||
.orElse(""); | ||
log.error("[EXCEPTION THROWN] METHOD={} PARAMETER={} THREW EXCEPTION={} | ip={}", | ||
joinPoint.getSignature().toShortString(), joinPoint.getArgs(), exception, ip); | ||
} | ||
} |