我必须在 SpringBoot 的一系列控制器中将日志记录添加到方法中 有没有办法在不添加到方法的情况下进行日志记录
I have to add logging to a method in a series of controllers in SpringBoot Is there a way to do the logging without adding to the method
我有一系列微服务,每个控制器都有一个方法
@SneakyThrows
@PostMapping(value = "/getData", produces = { "application/json" })
public GetDataResponse getData(@RequestBody GetDataRequest data) {
}
我可以向每个控制器添加日志记录,以获取与调用每个控制器相同的数据。
@SneakyThrows
@PostMapping(value = "application/<controllerApplicationName>/getData", produces = { "application/json" })
public GetDataResponse getData(@RequestBody GetDataRequest data) {
log.info("From client "+data.getClientId()
}
有没有办法让springboot来处理这个logging,这样我就不需要在每个controller和new controller中都添加logging了
是的,有办法做到这一点,您可以将 Spring AOP 添加到您的项目中,
AOP 是一种代理机制,它将拦截对 spring 托管 classes 中方法的调用。但它仅适用于 public 方法。
有了这个,您不需要在每个方法中都编写日志记录代码,对于您的整个应用程序,一个 class 就可以工作。
这是启动的示例代码。 (并在线参考其教程)
package com.demo.survey.aop;
import javax.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(* com.demo.controllers.*.*(..))")
private void forControllerPackage() {}
@Pointcut("execution(* com.demo.serviceImpl.*.*(..))")
private void forServiceImplPackage() {}
@Pointcut("forControllerPackage() || forServiceImplPackage()")
private void applyPointCut() {}
// Will get called before your method gets executed
@Before("execution(* com.demo.controllers.demoController.addData(..)))")
public void beforeAddQuestion(JoinPoint jt) {
MethodSignature methodSig = (MethodSignature) jt.getSignature();
System.out.println("methodSig = "+methodSig);
Object[] args = jt.getArgs();
for(Object tempArgs : args) {
System.out.println("tempArgs = "+tempArgs);
}
}
// @Around:- This will get called before and after your method runs.
@Around("applyPointCut()")
public Object logExceptions(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result = null;
//display Method Signature
MethodSignature methodSig = (MethodSignature) proceedingJoinPoint.getSignature();
try {
// execute the method
// do something before proceeding
System.out.println("methodSig = "+methodSig);
Object[] args = proceedingJoinPoint.getArgs();
for(Object tempArgs : args) {
// Arguments which are being passed to method
System.out.println("tempArgs = "+tempArgs);
}
result = proceedingJoinPoint.proceed();
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
// Here are the 2 dependencies that you will need
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
</dependency>
我有一系列微服务,每个控制器都有一个方法
@SneakyThrows
@PostMapping(value = "/getData", produces = { "application/json" })
public GetDataResponse getData(@RequestBody GetDataRequest data) {
}
我可以向每个控制器添加日志记录,以获取与调用每个控制器相同的数据。
@SneakyThrows
@PostMapping(value = "application/<controllerApplicationName>/getData", produces = { "application/json" })
public GetDataResponse getData(@RequestBody GetDataRequest data) {
log.info("From client "+data.getClientId()
}
有没有办法让springboot来处理这个logging,这样我就不需要在每个controller和new controller中都添加logging了
是的,有办法做到这一点,您可以将 Spring AOP 添加到您的项目中, AOP 是一种代理机制,它将拦截对 spring 托管 classes 中方法的调用。但它仅适用于 public 方法。
有了这个,您不需要在每个方法中都编写日志记录代码,对于您的整个应用程序,一个 class 就可以工作。
这是启动的示例代码。 (并在线参考其教程)
package com.demo.survey.aop;
import javax.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(* com.demo.controllers.*.*(..))")
private void forControllerPackage() {}
@Pointcut("execution(* com.demo.serviceImpl.*.*(..))")
private void forServiceImplPackage() {}
@Pointcut("forControllerPackage() || forServiceImplPackage()")
private void applyPointCut() {}
// Will get called before your method gets executed
@Before("execution(* com.demo.controllers.demoController.addData(..)))")
public void beforeAddQuestion(JoinPoint jt) {
MethodSignature methodSig = (MethodSignature) jt.getSignature();
System.out.println("methodSig = "+methodSig);
Object[] args = jt.getArgs();
for(Object tempArgs : args) {
System.out.println("tempArgs = "+tempArgs);
}
}
// @Around:- This will get called before and after your method runs.
@Around("applyPointCut()")
public Object logExceptions(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result = null;
//display Method Signature
MethodSignature methodSig = (MethodSignature) proceedingJoinPoint.getSignature();
try {
// execute the method
// do something before proceeding
System.out.println("methodSig = "+methodSig);
Object[] args = proceedingJoinPoint.getArgs();
for(Object tempArgs : args) {
// Arguments which are being passed to method
System.out.println("tempArgs = "+tempArgs);
}
result = proceedingJoinPoint.proceed();
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
// Here are the 2 dependencies that you will need
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
</dependency>